Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

python - Elasticsearch return searched word

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.

data:

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

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

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

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

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...