【问题标题】:Elasic search: find doc by id and highlight words based on query stringElasic 搜索:通过 id 查找文档并根据查询字符串突出显示单词
【发布时间】:2022-07-28 19:31:05
【问题描述】:

我喜欢在弹性搜索中查找文档,并根据查询字符串突出显示术语。 这可能吗? 我尝试运行查询字符串弹性搜索并根据 ID 过滤结果。但是这些听起来不是很有效,因为弹性首先生成一个巨大的列表,其中包含与查询字符串匹配的所有文档(可能是数百万)一个图片,只有一个基于过滤器的文档。

有没有一种方法或查询结构可以在一个布尔搜索中组合查询字符串和“在 _id 字段中搜索术语”?

类似这样的东西(不起作用):

"query": {
    "bool": {
        "must": {
            "query_string": {
                "query": "red*",
                "fields": [
                    "text",
                    "title"
                ] 
            },
            "term": {
                "_id":"fda72434fa172"
            }
        }
    }
},
"highlight": {
  "fields": {
[...]

【问题讨论】:

    标签: elasticsearch boolean highlight


    【解决方案1】:

    我做了一个小例子,可以作为一个起点。 使用过滤器执行查询并按 id 检索文档。 然后我使用 match 和 highlight 突出显示我想要的术语。

    POST test/_doc/fda72434fa172
    {
      "text": "I like to find an document in elastic search an highlight terms based on an query string. Is this possible?"
    }
    
    GET test/_search
    {
      "query": {
        "bool": {
          "filter": [
            {
              "term": {
                "_id": "fda72434fa172"
              }
            }
          ],
          "must": [
            {
              "match": {
                "text": {
                  "query": "elastic search"
                }
              }
            }
          ]
        }
      },
      "highlight": {
        "fields": {
          "text": {}
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-27
      • 1970-01-01
      • 2017-02-14
      • 2018-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-15
      相关资源
      最近更新 更多