【发布时间】:2021-07-04 03:06:59
【问题描述】:
说明
我正在尝试使用包括停用词在内的多词同义词进行查询。 我们先举个例子来说明。
我已将以下文档放入索引中。
- 富
- 酒吧
- 富吧
- 酒吧的富人
- 脸书
查询{"query":{"match":{"test":{"query":"foo of bar"}}}} 的预期结果是返回文档:
- 富吧
- 酒吧的富人
- 脸书
配置
在这个例子中,我有 2 个过滤器:
- stop:将移除标记 of
- synonym_graph:处理同义词fb、foo bar、foo of bar
映射
{
"properties": {
"test": {
"type": "text",
"analyzer": "test_index_analyzer",
"search_analyzer": "test_search_analyzer"
}
}
设置
{
"settings" : {
"index": {
"number_of_shards": 1,
"number_of_replicas": 0,
"analysis": {
"analyzer": {
"test_index_analyzer": {
"type": "custom",
"tokenizer": "whitespace",
"filter": [
"english_stop"
]
},
"test_search_analyzer": {
"type": "custom",
"tokenizer": "whitespace",
"filter": [
"english_stop",
"english_syn"
]
}
},
"filter": {
"english_stop": {
"type": "stop",
"stopwords": "_english_",
"ignore_case": true,
"remove_trailing": false
},
"english_syn": {
"type": "synonym_graph",
"synonyms": [
"fb,foo of bar",
"fb,foo bar"
]
}
}
}
}
}
}
结果
token 格式:"token,start_offset-end_offset,type / position / positionLength"
| Query | Search Result | index analysys | Search analysys |
|---|---|---|---|
| fb | fb | fb,0-2,word,0,1 | foo,0-2,SYNONYM / 0 / 1 foo,0-2,SYNONYM / 0 / 3 fb,0-2,word / 0 / 4 bar,0-2,SYNONYM / 2 / 2 bar,0-2,SYNONYM / 3 / 1 |
| foo of bar | fb | foo,0-3,word,0,1 bar,7-10,word,2,1 |
fb,0-10,SYNONYM / 0 / 3 foo,0-3,word / 0 / 1 bar,7-10,word / 2 / 1 |
| foo bar | fb,foo bar | foo,0-3,word,0,1 bar,4-7,word,1,1 |
fb,0-7,SYNONYM / 0 / 2 foo,0-3,word / 0 / 1 bar,4-7,word / 1 / 1 |
所有搜索都期望返回 3 行:
- 脸书
- 富吧
- 酒吧的富人
注意:bar 的 foo 永远不会返回
我的猜测是 foo of bar 被停止过滤器索引到位置 [foo, ,bar] 并且同义词正在寻找 [foo, bar]。
您对实现我的目标有什么建议吗?
【问题讨论】:
-
我找到了一个提出解决方案的页面:sease.io/2018/07/combining-synonyms-and-stopwords.html 但它需要在 Java 中部署自定义过滤器,目前可能难以修补到生产环境。
标签: elasticsearch filter stop-words synonym