elasticsearch - Elasticsearch:如何返回包含搜索到的确切单词的文档,而不是返回句子中包含该单词的所有文档?
问题描述
我有名为“描述”的字段(类型文本)
我有 3 个文件。
doc1 描述 = “测试”
doc2 描述 = “测试 dsc”
doc3 description = "2021 测试说明"
案例 1-如果我搜索“测试”,我只想要 doc1
案例 2-如果我搜索“test dsc”,我只想要 doc2
案例 3-如果我搜索“2021 test desc”,我只想要 doc3
但现在只有 CASE 3 有效
例如 CASE1 不工作。如果我尝试这个查询,我有所有 3 个文档
GET /myindex/_search
{
"query": {
"match" : {
"Description" : "test"
}
}
}
谢谢
解决方案
您将在搜索中获取所有三个文档,因为默认情况下,elasticsearch 使用标准分析器,用于text
类型字段。这将标记"2021 test desc"
为
{
"tokens": [
{
"token": "2021",
"start_offset": 0,
"end_offset": 4,
"type": "<NUM>",
"position": 0
},
{
"token": "test",
"start_offset": 5,
"end_offset": 9,
"type": "<ALPHANUM>",
"position": 1
},
{
"token": "desc",
"start_offset": 10,
"end_offset": 14,
"type": "<ALPHANUM>",
"position": 2
}
]
}
因此,它将返回与上述任何标记匹配的所有文档。
如果要搜索需要更新索引映射的确切术语。
您可以通过以多种方式(即使用多个字段)索引同一字段来更新映射。
PUT /_mapping
{
"properties": {
"description": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
}
}
}
然后再次重新索引数据。在此之后,您将能够使用文本类型的“description”字段和关键字类型的“description.raw”进行查询
搜索查询:
{
"query": {
"match": {
"description.raw": "test dsc"
}
}
}
搜索结果:
"hits": [
{
"_index": "67777521",
"_type": "_doc",
"_id": "2",
"_score": 0.9808291,
"_source": {
"description": "test dsc"
}
}
]
推荐阅读
- http - 如何使用 SAP ICF 将数据分块发送到外部服务?
- css - 使用 webpack 和/或汇总将自动缩小的 CSS 文件加载为字符串的最简单/最佳方法是什么?
- node.js - 在 NodeJS 中安装 NPM 包
- html - 当我转换到不同的元素时,导航栏中的元素会出错。我怎样才能防止这种情况发生?
- javascript - Javascript 价值获取
- nginx - 尽管安装了 nginx,但无法连接到我的 kubernetes 集群
- sql - 在 python/sqlite3 中使用 for 循环设置与另一个表相关的外键
- php - 从 php 运行 docker 命令
- terraform - Terraform (v0.13) 如何获取(第三方)提供程序二进制文件的最新版本?
- regex - 使用正则表达式在字符串中添加额外的单引号,以便动态传递