同义词分析器不起作用 [英] Synonym analyzer not working
问题描述
这是我的设置:
{
"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
文件位于主 elasticsearch
文件夹内的 config
文件夹中.
My synonym.txt
file is in the config
folder inside the main elasticsearch
folder.
这是我的查询:
query: {
query_string: {
fields: ["countryName"],
default_operator: "AND",
query: searchInput,
analyzer: "synonym"
}
}
synonym.txt 中的单词是:美国,美国.
The words in synonym.txt
are: us, u.s., united states.
所以这不起作用.有趣的是,当我在 synonym.txt
文件中输入任何单词时,搜索工作正常, 除外.因此,例如,当我通常在搜索中键入我们时,我会得到结果.使用此分析仪,我们不会给我任何东西.
So this doesn't work. What's interesting is that search works as normal, except for when I enter any of the words in the synonym.txt
file. So for example, when I usually type in us into the search, I would get results. With this analyzer, us doesn't give me anything.
我已经对我的ES服务器进行了 close
和 open
,但仍然无法正常工作.
I've done close
and open
to my ES server, and still it doesn't work.
编辑
文档
的示例:
{
"_index": "countries",
"_type": "country",
"_id": "57aabeb80057405968de152b",
"_score": 1,
"_source": {
"countryName": "United States"
}
searchInput
的示例(来自前端):
Example of searchInput
(this is coming from the front-end):
美国
编辑#2 :
这是我更新的索引配置文件:
Here is my updated index config file:
{
"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": {}
}
}
}
}
当我尝试添加文档并对这些文档进行搜索时,同义词
分析器对我不起作用.
When I try adding documents, and doing a search on said documents, the synonym
analyzer does not work for me.
编辑#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"
}
}]
}
}
推荐答案
You are close, but I suggest reading thoroughly this section from the documentation to understand better this functionality.
作为解决方案:
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"
}
}
}
}
}
您需要删除索引,然后使用上面的映射重新创建索引.然后使用以下查询:
You need to delete the index and create it again with the mapping above. Then use this query:
"query": {
"query_string": {
"fields": [
"countryName"
],
"default_operator": "AND",
"query": "united states"
}
}
这篇关于同义词分析器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!