python - Elasticsearch 返回搜索到的词

标签 python elasticsearch

我正在使用 fuzzy并希望 elasticsearch 返回搜索到的单词而不仅仅是命中。 当我搜索 dogo 这个词时我的模糊搜索找到了这个词 dog我想知道它是 dogo谁发现了它。

数据:

{ "index": { "_id":1 }}
{ "title": "The quick brown fox", "price":5 }
{ "index": { "_id":2 }}
{ "title": "The quick blue dog", "price":7 }
{ "index": { "_id":3 }}
{ "title": "The slow brown dog", "price":5 }

查询:

{
  "query": {
    "bool": {
    "should": [
        {
          "fuzzy": {
                  "title": "dogo"
                      }

          },
        {
          "fuzzy": {
                  "title": "fox"
                      }
          }
        ]
    }

  },
  "highlight" : {
      "fields" : {
          "title":{
              "pre_tags": [
                "===>"
              ],
              "post_tags": [
                "<==="
              ],
              "fragment_size": 200,
              "number_of_fragments": 100
          }
      }
   }  
}

此查询将返回 ===>dog<===但不知道是否dogo找到了。

有谁知道怎么做或有什么想法吗? 我希望我的输出类似于 dog : dogo .

最佳答案

你可以使用 named queries为此,通过为每个查询命名。在结果中,每个命中都将包含一个 matched_queries 数组,其中包含匹配的查询的名称(例如下面的 dogofox)。

{
  "query": {
    "bool": {
      "should": [
        {
          "fuzzy": {
            "name": {
              "value": "dogo",
              "_name": "dogo"
            }
          }
        },
        {
          "fuzzy": {
            "name": {
              "value": "fox",
              "_name": "fox"
            }
          }
        }
      ]
    }
  },
  "highlight": {
    "fields": {
      "title": {
        "pre_tags": [
          "===>"
        ],
        "post_tags": [
          "<==="
        ],
        "fragment_size": 200,
        "number_of_fragments": 100
      }
    }
  }
}

关于python - Elasticsearch 返回搜索到的词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59361024/

相关文章:

python - 导入 azure.search.documents 时,Azure 函数未部署(时间触发器)

ruby - 如何使用 ruby​​ 遍历这个 json 文档?

elasticsearch - 如何使用 Liferay API 在 Elastic-search 中构建父子关系搜索?

Python:如何通过切片插入列表?

python - 为什么这个 argparse 代码在 Python 2 和 3 之间表现不同?

.net - 如何使用 NEST 客户端将 Elasticsearch 配置为使用 AutoMap 类型作为动态映射对象的默认模板?

node.js - 如何在nodejs中用mongoosastic写Mapping

elasticsearch - 部分符合 Elasticsearch 查询的要求

Python - 编写打印

python - 将字符串从特定字符编辑为特定字符