我有以下文件:
南非
北非
我想从以下位置检索我的“南非”文档:
非洲
(a)南非
(b)非洲
(c)
我定义了以下过滤器和分析器:
POST test_index
{
"settings": {
"analysis": {
"filter": {
"synonym_filter": {
"type": "synonym",
"synonyms": [
"south,s",
"north,n"
]
},
"shingle_filter": {
"type": "shingle",
"min_shingle_size": 2,
"max_shingle_size": 3,
"token_separator": ""
}
},
"analyzer": {
"my_shingle": {
"type": "custom",
"tokenizer": "standard",
"filter": ["shingle_filter"]
},
"my_shingle_synonym": {
"type": "custom",
"tokenizer": "standard",
"filter": ["shingle_filter", "synonym_filter"]
},
"my_synonym_shingle": {
"type": "custom",
"tokenizer": "standard",
"filter": ["synonym_filter", "shingle_filter"]
}
}
}
},
"mappings": {}
}
1) my_shingle south africa
将被索引为 south
, southafrica
, africa
2) my_shingle_synonym south africa
将被索引为 south
, s
, southafrica
, 非洲
3) my_synonym_shingle south africa
将被索引为 south
, souths
, southsafrica
, s
, 非洲
, 非洲
所以
(1)我会找到b
(2)我会找到a,b
(3)我会找到a,c
我希望 south africa
被索引为:south
、s
、southafrica
、safrica
, 非洲
最佳答案
您不必必须根据您的要求输出所有可能的标记。您的问题可以通过在 multi fields 上使用不同的分析器来解决。 .
您可以像这样定义所需字段的映射
。
"mappings": {
"your_mapping": {
"properties": {
"name": {
"type": "string",
"analyzer": "my_shingle",
"fields": {
"synonym": {
"type": "string",
"analyzer": "my_synonym_shingle"
}
}
}
}
}
}
要索引的示例文档
PUT test_index/your_mapping/1
{
"name" : "south africa"
}
然后您将使用 wildcard expression 查询名称字段的所有变体.
GET test_index/your_mapping/_search
{
"query": {
"query_string": {
"fields": [
"name*"
],
"query": "safrica"
}
}
}
关于Elasticsearch 使用带同义词的 shingle 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40681178/