当我尝试输入sat时,我有2个商品名称为“satiny”,它没有出现。
elasticsearch - Elasticsearch自动完成无法正常工作-LMLPHP

但是,当我键入“sati”时,首先出现“satiny”,然后出现其他产品,然后出现第二个“satiny”产品。
elasticsearch - Elasticsearch自动完成无法正常工作-LMLPHP

我已经实现了模糊搜索,Edge-ngram,同义词搜索。

这是我的索引:

{"settings": {
    "index": {
      "analysis": {
        "filter": {
          "synonym" : {
                    "type": "synonym",
                    "synonyms_path": "analysis/synonym.txt"
                        }

        },
        "analyzer":
            {
              "synonym" : {
                    "tokenizer" : "whitespace",
                    "filter" : ["lowercase",
                    "synonym"]
                },

          "keyword_analyzer": {
            "filter": [
              "lowercase",
              "asciifolding",
              "trim"
            ],
            "char_filter": [],
            "type": "custom",
            "tokenizer": "keyword"
          },
          "edge_ngram_analyzer": {
            "filter": [
              "lowercase"
            ],
            "tokenizer": "edge_ngram_tokenizer"
          },
          "edge_ngram_search_analyzer": {
            "tokenizer": "lowercase"
          }
            },

        "tokenizer": {
          "edge_ngram_tokenizer": {
            "type": "edge_ngram",
            "min_gram": 1,
            "max_gram": 25,
            "token_chars": [
              "letter"
            ]
          }

  },
    "mappings": {
            "properties" : {
            "firebaseId":{
            "type":"text"
            },
                "name" : {
                   "type" : "text",
                   "analyzer" : "synonym"

                },
                "name_auto" : {
                "type": "text",

    "fields": {
      "edgengram": {
        "type": "text",
        "analyzer": "edge_ngram_analyzer",
        "search_analyzer": "edge_ngram_search_analyzer"
      },
      "completion": {
        "type": "completion"
      }

    }
                },

                "category_name" : {
                            "type": "text",
                                "fields": {
                                  "keyword": {
                                    "type": "keyword"
                            }
                }
                },
                "storeName" : {
                    "type": "keyword"
                },
                "sku" : {
                    "type" : "text"
                },
                "price" : {
                    "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword"
                            }
                }
                },
                "magento_id" : {
                    "type" : "text"
                },
                "seller_id" : {
                    "type" : "text"
                },
                "square_item_id" : {
                    "type" : "text"
                },
                "square_variation_id" : {
                    "type" : "text"
                },
                "typeId" : {
                    "type" : "text"
                }
            }
    }
}
}
}
}

这是我的查询:
{
    "from": 0,
    "size": 50,
    "query": {
        "bool": {
            "must": [{
                "bool": {
                    "should": [{
                        "match_phrase": {
                            "name_auto.edgengram": "sati"
                        }
                    }, {
                        "match_phrase": {
                            "name_auto": "sati"
                        }
                    }, {
                        "match": {
                            "name_auto": "sati"
                        }
                    }, {
                        "match": {
                            "category_name": {
                                "query": "sati",
                                "analyzer": "synonym"
                            }
                        }
                    }, {
                        "match_phrase": {
                            "category_name": "sati"
                        }
                    }, {
                        "match": {
                            "name": {
                                "query": "sati",
                                "fuzziness": 2,
                                "prefix_length": 1
                            }
                        }
                    }]
                }
            }]
        }
    }
}

当我键入“sat”时,edge-ngram应该首先启动并向我显示名为“satiny”的产品,然后模糊搜索实现应启动并向我显示具有“sol”或“set”的产品

我得到了部分解决,但我仍然希望所有带有“木炭”的产品名称都首先出现。

elasticsearch - Elasticsearch自动完成无法正常工作-LMLPHP

最佳答案

问题似乎是您的mappings部分未正确位于settings旁边的顶层。它位于settings.index.analysis内部,因此没有正确定义任何字段。这将有助于正确格式化JSON。

像这样创建索引,它应该可以工作:

{
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "synonym": {
            "tokenizer": "whitespace",
            "filter": [
              "lowercase"
            ]
          },
          "keyword_analyzer": {
            "filter": [
              "lowercase",
              "asciifolding",
              "trim"
            ],
            "char_filter": [],
            "type": "custom",
            "tokenizer": "keyword"
          },
          "edge_ngram_analyzer": {
            "filter": [
              "lowercase"
            ],
            "tokenizer": "edge_ngram_tokenizer"
          },
          "edge_ngram_search_analyzer": {
            "tokenizer": "lowercase"
          }
        },
        "tokenizer": {
          "edge_ngram_tokenizer": {
            "type": "edge_ngram",
            "min_gram": 1,
            "max_gram": 25,
            "token_chars": [
              "letter"
            ]
          }
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "firebaseId": {
        "type": "text"
      },
      "name": {
        "type": "text",
        "analyzer": "synonym"
      },
      "name_auto": {
        "type": "text",
        "fields": {
          "edgengram": {
            "type": "text",
            "analyzer": "edge_ngram_analyzer",
            "search_analyzer": "edge_ngram_search_analyzer"
          },
          "completion": {
            "type": "completion"
          }
        }
      },
      "category_name": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      },
      "storeName": {
        "type": "keyword"
      },
      "sku": {
        "type": "text"
      },
      "price": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      },
      "magento_id": {
        "type": "text"
      },
      "seller_id": {
        "type": "text"
      },
      "square_item_id": {
        "type": "text"
      },
      "square_variation_id": {
        "type": "text"
      },
      "typeId": {
        "type": "text"
      }
    }
  }
}

08-27 02:58