6
votes

I am new to elasticsearch and was looking around fuzzy query search.
I have made a new index products with object/record values like this

{
            "_index": "products",
            "_type": "product",
            "_id": "10",
            "_score": 1,
            "_source": {
                "value": [
                    "Ipad",
                    "Apple",
                    "Air",
                    "32 GB"
                ]
            }
        }

Now when i am performing a fuzzy query search in elasticsearch like

{
   query: {
       fuzzy: {
          value: "tpad"
       }
   }
}

It returns me the correct record (the product just made above) which is expected.
And i know that the term tpad matches ipad so record was return.
But technically how would i know that it has matched ipad. Elastic search just returns the full record(or records) like this

{
"took": 4,
"timed_out": false,
"_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
},
"hits": {
    "total": 1,
    "max_score": 0.61489093,
    "hits": [
        {
            "_index": "products",
            "_type": "product",
            "_id": "10",
            "_score": 0.61489093,
            "_source": {
                "value": [
                    "Ipad",
                    "Apple",
                    "Air",
                    "32 GB"
                ]
            }
        }
    ]
}
}

Is there any way in elastic search so that i can know if it has matched tpad against ipad

3

3 Answers

4
votes

if you use highlighting, Elasticsearch will show the terms that matched:

curl -XGET http://localhost:9200/products/product/_search?pretty -d '{
  "query" : {
    "fuzzy" : {
        "value" : "tpad"
      }
  },
  "highlight": {
    "fields" : {
        "value" : {}
    }
  }
}'

Elasticsearch will return matching documents with the fragment highlighted:

{
  "took" : 31,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.13424811,
    "hits" : [ {
      "_index" : "products",
      "_type" : "product",
      "_id" : "10",
      "_score" : 0.13424811,
      "_source":{
 "value" : ["Ipad",
                "Apple",
                "Air",
                "32 GB"
                ]
           },
      "highlight" : {
        "value" : [ "<em>Ipad</em>" ]
      }
    } ]
  }
}
2
votes

if you just want to analyze the result, you could use the Inquisitor plugin.

If you need to do this programmatically, I think the highlighting feature will help you:

Determining which words were matched in a fuzzy search

1
votes

I know the question is older but I just ran into it. The way I do it is by populating the query name field when building the query. This way it will come back inside the "matchedQuery" field in response. Hope this helps :)