这是我的设置:
{ "countries": { "aliases": {}, "mappings": { "country": { "properties": { "countryName": { "type": "string" } } } }, "settings": { "index": { "creation_date": "1472140045116", "analysis": { "filter": { "synonym": { "ignore_case": "true", "type": "synonym", "synonyms_path": "synonym.txt" } }, "analyzer": { "synonym": { "filter": [ "synonym" ], "tokenizer": "whitespace" } } }, "number_of_shards": "5", "number_of_replicas": "1", "uuid": "7-fKyD9aR2eG3BwUNdadXA", "version": { "created": "2030599" } } }, "warmers": {} } }
我的synonym.txt文件在config主elasticsearch文件夹内的文件夹中。
synonym.txt
config
elasticsearch
这是我的查询:
query: { query_string: { fields: ["countryName"], default_operator: "AND", query: searchInput, analyzer: "synonym" } }
中的字眼synonym.txt是:美国 ,美国,美国 。
所以这行不通。有趣的是,搜索正常,但当我在文件中输入任何单词时 除外synonym.txt。因此,例如,当我通常在搜索中键入 我们 的内容时,就会得到结果。有了这个分析仪, 我们 什么都不会给我。
我已经完成close并连接open到ES服务器,但仍然无法正常工作。
close
open
编辑
一个示例document:
document
{ "_index": "countries", "_type": "country", "_id": "57aabeb80057405968de152b", "_score": 1, "_source": { "countryName": "United States" }
的示例searchInput(这来自前端):
searchInput
united states
编辑#2 :
这是我更新的索引配置文件:
{ "countries": { "aliases": {}, "mappings": {}, "settings": { "index": { "number_of_shards": "5", "creation_date": "1472219634083", "analysis": { "filter": { "synonym": { "ignore_case": "true", "type": "synonym", "synonyms_path": "synonym.txt" } }, "analyzer": { "synonym": { "filter": [ "synonym" ], "tokenizer": "whitespace" } } }, "country": { "properties": { "countryName": { "type": "string", "analyzer": "synonym" }, "number_of_replicas": "1", "uuid": "50ZwpIVFTqeD_rJxlmd59Q", "version": { "created": "2030599" } } }, "warmers": {} } } } }
当我尝试添加文档并在所述文档上进行搜索时,synonym分析仪对我不起作用。
synonym
编辑#3
以下是索引中的2个文档:
{ "took": 3, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 2, "max_score": 1, "hits": [{ "_index": "stocks", "_type": "stock", "_id": "2", "_score": 1, "_source": { "countryName": "United States" } }, { "_index": "stocks", "_type": "stock", "_id": "1", "_score": 1, "_source": { "countryName": "Canada" } }] } }
您已经接近了,但是我建议您通读文档中的本节,以更好地了解此功能。
解决方案:
PUT /countries { "mappings": { "country": { "properties": { "countryName": { "type": "string", "analyzer": "synonym" } } } }, "settings": { "analysis": { "filter": { "synonym": { "ignore_case": "true", "type": "synonym", "synonyms_path": "synonym.txt" } }, "analyzer": { "synonym": { "filter": [ "lowercase", "synonym" ], "tokenizer": "whitespace" } } } } }
您需要删除索引,然后使用上面的映射再次创建索引。然后使用以下查询:
"query": { "query_string": { "fields": [ "countryName" ], "default_operator": "AND", "query": "united states" } }