elasticsearch - 模糊多重匹配未按预期工作 Elastic Search
问题描述
我正在尝试在您键入时添加模糊性来编写搜索查询。(弹性搜索 7.12)
{
"query": {
"multi_match": {
"query": "airl recl",
"fields": [
"tags",
"display_text",
"display_subtext"
],
"type" : "most_fields",
"operator": "and",
"fuzziness": "AUTO:4,6",
"prefix_length" :2
}
}
}
我插入了带有“airtel 充值”值的文档。我还对上述给定的 3 个字段以及空间分析器使用 edge n gram(1:50)。
- 如果我使用 airl 搜索 -> 它可以正常工作,使用 airtel 关键字获得结果。
- 如果我使用 recl 搜索 -> 它工作正常,使用充值关键字获得结果。
- 但是当我在查询中使用“airl recl”搜索时,没有得到任何结果。
空间分析仪:
"words_with_spaces_analyzer" : {
"filter" : [
"lowercase",
"asciifolding"
],
"type" : "custom",
"tokenizer" : "words_with_space"
}
},
"tokenizer" : {
"words_with_space" : {
"pattern" : "([a-zA-Z0-9.-]+[\\s]*)",
"type" : "pattern",
"group" : "0"
}
}
},
映射
"display_text": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
},
"analyzer": "edge_nGram_analyzer",
"search_analyzer": "words_with_spaces_analyzer"
}
有人可以帮助我理解为什么上面给定的查询对于多令牌输入会以这种方式表现,而如果单独运行它们,两个令牌都会给出输出?
解决方案
您需要使用空白分析器,作为search_analyzer
. 这会将搜索词"airl recl"
分解为airl
和recl
。然后,将对这些单独的令牌执行搜索。
添加具有索引映射、搜索查询和搜索结果的工作示例
索引映射:
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "my_tokenizer"
}
},
"tokenizer": {
"my_tokenizer": {
"type": "edge_ngram",
"min_gram": 2,
"max_gram": 50,
"token_chars": [
"letter",
"digit"
]
}
}
},
"max_ngram_diff": 50
},
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "my_analyzer",
"search_analyzer": "whitespace"
}
}
}
}
指数数据:
{
"name": "airtel recharge"
}
搜索查询:
{
"query": {
"multi_match": {
"query": "airl recl",
"fields": [
"name"
],
"type": "most_fields",
"operator": "and",
"fuzziness": "AUTO:4,6",
"prefix_length": 2
}
}
}
搜索结果:
"hits": [
{
"_index": "67702617",
"_type": "_doc",
"_id": "1",
"_score": 0.22729424,
"_source": {
"name": "airtel recharge"
}
}
]
推荐阅读
- javascript - 创建和选择
- 动态列出元素
- string - 在 Rust 的命令调用中使用 PathBuf 的最佳方法是什么?
- symfony - 在 Akeneo EventListener 中标准化对象
- c# - NuGet 恢复在 Docker 容器内停止工作
- r - 来自 R 中的 lm 或 glm 函数的自动单面 90% CI
- javascript - 中小企业答案变体不起作用 discord.js
- java - 如何通过 Spring Boot 和 Angular 9 下载 xls 文件
- css - React:如何在 Styled-Components 上为依赖于父级的子元素设置样式
- angular - 在Angular超时之前刷新令牌
- security - Office 365 - 当有新设备登录或多次登录失败时发送警报通知