查询 Synomys ElasticSearch

Query Synomys ElasticSearch

我用这种方式创建了一个带有同义词的新索引:

PUT /test_index2

{
"settings": {
    "index" : {
        "analysis" : {
            "filter" : {
                "synonym" : {
                    "type" : "synonym",
                    "synonyms" : [
                        "mezzo,centro"
                    ]
                }
            }
        }
    }
}

}

当我尝试这个查询时:

{
   "query": 
     {
        "multi_match": 
            {
               "query": "centro",
               "fields": ["content"],
                "analyzer": "synonym"
            }
      }
  }

Kibana 给我这个错误:

  [multi_match] analyzer [synonym] not found

我对 elastic 不是很有经验你能帮我吗?

您需要创建一个利用同义词过滤器的自定义分析器

{
"settings": {
    "index" : {
        "analysis" : {
            "analyzer": {                          <-- add this
                "synonym_analyzer": {
                   "type": "custom",
                   "filter": ["synonym"],
                   "tokenizer": "keyword"
                }
            },
            "filter" : {
                "synonym" : {
                    "type" : "synonym",
                    "synonyms" : [
                        "mezzo,centro"
                    ]
                }
            }
        }
    }
}

然后您可以在查询中使用它

{
   "query": 
     {
        "multi_match": 
            {
               "query": "centro",
               "fields": ["content"],
                "analyzer": "synonym_analyzer"   <-- change this
            }
      }
  }