Elasticsearch返回搜索词 [英] Elasticsearch return searched word

查看:165
本文介绍了Elasticsearch返回搜索词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 fuzzy ,并希望elasticsearch返回搜索到的单词而不仅仅是点击。
当我搜索单词 dogo 时,模糊搜索找到单词 dog 我想知道就是找到它的 dogo

I am using fuzzy and want elasticsearch to return the searched word not just the hit. When i am searching for the word dogo and my fuzzy search finds the word dog i want to know that it was dogo who found it.

数据:

{ "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 是否找到它。

This query will return ===>dog<=== but don't know if dogo found it.

有人知道如何做到这一点或想法吗?
我希望我的输出像 dog:dogo

Does anyone know how to do this or an idea? I want my output to be something like dog : dogo.

推荐答案

您可以使用为此命名,方法是为每个查询命名。在结果中,每个匹配项都会包含一个 matched_queries 数组,其中包含匹配的查询的名称(例如 dogo fox )。

You could use named queries for this, by giving a name to each of your queries. In the results, each hit will feature a matched_queries array containing the names of the queries that matched (e.g. dogo and fox below).

{
  "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
      }
    }
  }
}

这篇关于Elasticsearch返回搜索词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆