如何解决带有 AND 运算符的 Elasticsearch Multi-Match Query 用于 Hyphenation_decompounder 令牌过滤器生成的令牌
我使用 hyphenation_decompounder 表示德语并遵循文档中提到的示例。到现在为止还挺好。有用!。文本 kaffeetasse
被标记为 kaffee
和 tasse
。
当我使用 multi-match 查询 kaffeetasse 来查找 kaffee 和 tasse 都匹配的文档时,出现了这个问题。似乎多匹配对 hyphenation_decompounder 过滤器生成的标记使用 OR 而不是多匹配查询中的给定运算符(“AND”)。这是我的测试用例
映射
curl -XPUT "http://localhost:9200/testidx" -H 'Content-Type: application/json' -d'{ "settings": { "index": { "analysis": { "analyzer": { "index": { "type" : "custom","tokenizer": "whitespace","filter": [ "lowercase" ] },"search": { "type" : "custom","filter": [ "lowercase","hyph" ] } },"filter": { "hyph": { "type": "hyphenation_decompounder","hyphenation_patterns_path": "analysis/de_DR.xml","word_list": ["kaffee","zucker","tasse"],"only_longest_match": true,"min_subword_size": 4 } } } } },"mappings" : { "properties" : { "title" : { "type" : "text","analyzer": "index","search_analyzer": "search" },"description" : { "type" : "text","search_analyzer": "search" } } } }'
文档 ID=1
curl -XPOST "http://localhost:9200/testidx/_doc/1" -H 'Content-Type: application/json' -d'{ "title" : "Kaffee","description": "Milch Kaffee tasse"}'
文档 ID=2
curl -XPOST "http://localhost:9200/testidx/_doc/2" -H 'Content-Type: application/json' -d'{ "title" : "Kaffee","description": "Latte Kaffee Becher"}'
多匹配查询
curl -XGET "http://localhost:9200/testidx/_search" -H 'Content-Type: application/json' -d'{ "query": { "multi_match": { "query": "kaffeetasse","fields": ["title","description"],"operator": "and","type": "cross_fields","analyzer": "search" } }}'
我的期望是 elasticsearch 应该只返回 id=1 的单个文档,因为它的字段中有 kaffee
AND tasse
但它返回两个文档,因为两者都有 kaffee
OR tasse
文本。
Elasticsearch:7.9.2
de_DR.xml
从文档中提到的 https://sourceforge.net/projects/offo/files/offo-hyphenation/1.2/offo-hyphenation_v1.2.zip/download 下载。
解决方法
Elasticsearch 返回两个文档,因为它将 operator
参数应用于原始查询 kaffeetasse
,而不是应用于由分析器。 kaffee
查询的 documentation 中描述的此类行为:
operator(可选,字符串)布尔逻辑,用于解释tasse
值中的文本。
由于原始查询是一个词,所以 match
参数没有意义。
作为一种解决方法,您可以分两步执行搜索:
-
使用 analyze API 分析您的原始查询字符串:
query
-
使用从
operator
分析器接收到的标记作为curl -XGET "http://localhost:9200/testidx/_analyze" -H 'Content-Type: application/json' -d'{"analyzer": "search","text": "kaffeetasse"}'
查询的单词,search
参数设置为multi_match
,operator
参数设置为 {{ 1}}(防止使用and
分析器再次分析已经分析过的标记):analyzer
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。