为什么搜索会忽略同义词



我想搜索一个短语并获得所有结果(包括同义词结果(。

我配置我的索引如下:

"settings": {
"index": {
"analysis": {
"filter": {
"my_graph_synonyms": {
"type": "synonym_graph",
"synonyms": [
"Cosmos, Universe",
]
}
},
"analyzer": {
"my_search_time_analyzer": {
"tokenizer": "standard",
"filter": [
"lowercase",
"stemmer",
"my_graph_synonyms"
]
}
}
}
}
},
"mappings": {
"properties": {
"content": {
"type": "text",
"analyzer": "standard",
"search_analyzer": "my_search_time_analyzer"
}
}
}

我在索引中添加了两个文档:

PUT demo_idx/_doc/1
{
"content": "Cosmos A Spacetime Odyssey is a 2014 American science documentary television series."
}
PUT demo_idx/_doc/2
{
"content": "Universe A Spacetime Odyssey is a 2014 American science documentary television series."
}

我运行以下搜索:

"query": {
"bool": {
"must":
[{
"match": {
"content": {
"query": "Cosmos",
}
}
}]
}
} 

我本想得到2个结果(按照同义词(,但我只得到了一个。

如何运行搜索查询(同时使用同义词(并获得2个结果?

这是由于stemmer过滤器造成的,如果您删除它并再次索引您的数据,它将向您返回两个文档,

您可以使用analyze API来检查分析器生成的令牌,并且您可以看到它在令牌下面生成的Cosmos

{
"tokens": [
{
"token": "univers", // Note this
"start_offset": 0,
"end_offset": 6,
"type": "SYNONYM",
"position": 0
},
{
"token": "cosmo",
"start_offset": 0,
"end_offset": 6,
"type": "<ALPHANUM>",
"position": 0
}
]
}

而在索引时使用的standard标记化器为Universe创建Universe而不进行词尾处理,因此它与search_analyzer生成的搜索词不匹配。

最新更新