Elasticsearch return searched word

前端 未结 2 721
清酒与你
清酒与你 2021-01-03 11:09

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 fi

相关标签:
2条回答
  • 2021-01-03 11:22

    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
          }
        }
      }
    }
    
    0 讨论(0)
  • 2021-01-03 11:25

    Named queries is the right choice to understand your query name in results. You could also try suggestion if you want to know the possible corrected terms for your query term.

    {
      "query": {
        "bool": {
        "should": [
            {
              "fuzzy": {
                      "title": "dogo"
                          }
    
              },
            {
              "fuzzy": {
                      "title": "fox"
                          }
              }
            ]
        }
    
      },
      "highlight" : {
          "fields" : {
              "title":{
                  "pre_tags": [
                    "===>"
                  ],
                  "post_tags": [
                    "<==="
                  ],
                  "fragment_size": 200,
                  "number_of_fragments": 100
              }
          }
       }  ,
     "suggest" : {
        "title_suggestion" : {
          "text" : "fox dogo",
          "term" : {
            "field" : "title"
          }
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题