我如何搜索文档与他们的同义词在Elasticsearch?



我有一个包含一些文档的索引。这些文档的字段为name。但是现在,我的文件可以有几个名字。一个文档可以有多少个名字是不确定的。一个文档只能有一个名称,或者一个文档可以有10个名称。

问题是,如何组织我的索引,文档和查询,然后按不同的名称搜索1个文档?

例如,有一个文档的名称是:"automobile";automobil";无论何时我查询其中一个名字,我都应该得到这个文档。我能不能创建一个这些名字的数组然后建立一个查询来搜索每个名字?或者有更合适的方法。

;

我感觉你在找同义词之类的东西?

<标题>

解决方案在下面的例子中,我用一个特定的文本分析器创建一个索引。

此分析器将automobile,automobil自動車作为相同的令牌处理。

PUT /74472994
{
"settings": {
"index": {
"analysis": {
"analyzer": {
"synonym": {
"tokenizer": "standard",
"filter": ["synonym" ]
}
},
"filter": {
"synonym": {
"type": "synonym",
"synonyms": [ "automobile, automobil, 自動車" ]
}
}
}
}
},
"mappings": {
"properties": {
"name":{
"type": "text",
"analyzer": "synonym"
}
}
}
}
POST /74472994/_doc
{
"name": "automobile"
}

允许我执行以下请求:

GET /74472994/_search
{
"query": {
"match": {
"name": "automobil"
}
}
}
GET /74472994/_search
{
"query": {
"match": {
"name": "自動車"
}
}
}

总是得到:

{
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.7198386,
"hits": [
{
"_index": "74472994",
"_id": "ROfyhoQBcn6Q8d0DlI_z",
"_score": 1.7198386,
"_source": {
"name": "automobile"
}
}
]
}
}

相关内容

最新更新