Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elastic search exact match

I'm using elasticsearch and am having a devil of a time getting an exact match to happen. I've tried various combinations of match, query_string, etc, and I either get nothing or bad results. Query looks like this:

{
  "filter": {
    "term": {
      "term": "dog",
      "type": "main"
    }
  },
  "query": {
    "match_phrase": {
      "term": "Dog"
    }
  },
  "sort": [
    "_score"
  ]
}

Sorted results

10.102211 {u'term': u'The Dog', u'type': u'main', u'conceptid': 7730506}
10.102211 {u'term': u'That Dog', u'type': u'main', u'conceptid': 4345664}
10.102211 {u'term': u'Dog', u'type': u'main', u'conceptid': 144}
7.147442 {u'term': u'Dog Eat Dog (song)', u'type': u'main', u'conceptid': u'5288184'}

I see, of course that "The Dog", "That Dog" and "Dog" all have the same score, but I need to figure out how I can boost the exact match "Dog" in score.

I also tried

{
  "sort": [
    "_score"
  ],
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "term": "Dog"
          }
        },
        {
          "match_phrase": {
            "term": {
              "query": "Dog",
              "boost": 5
            }
          }
        }
      ]
    }
  },
  "filter": {
    "term": {
      "term": "dog",
      "type": "main"
    }
  }
}

but that still just gives me

11.887239 {u'term': u'The Dog', u'type': u'main', u'conceptid': 7730506}
11.887239 {u'term': u'That Dog', u'type': u'main', u'conceptid': 4345664}
11.887239 {u'term': u'Dog', u'type': u'main', u'conceptid': 144}
8.410372 {u'term': u'Dog Eat Dog (song)', u'type': u'main', u'conceptid': u'5288184'}
like image 323
Josh Harrison Avatar asked Sep 03 '13 17:09

Josh Harrison


People also ask

How do you use exact match in Elasticsearch?

If you want to be able to easily match only ":feed:" inside the message field you might want to costumize an analyzer which doesn't tokenize ":" so you will be able to query the field with a simple "match" query instead of wild characters.

Which query DSL is used to perform exact text match?

The match query is the standard query for performing a full-text search, including options for fuzzy matching.

What is match phrase in Elasticsearch?

Match phrase queryeditA phrase query matches terms up to a configurable slop (which defaults to 0) in any order. Transposed terms have a slop of 2. The analyzer can be set to control which analyzer will perform the analysis process on the text.

What is Termquery?

The term query finds documents that contain the exact term specified in the inverted index. For instance: POST _search { "query": { "term" : { "user" : "Kimchy" } } } Finds documents which contain the exact term Kimchy in the inverted index of the user field.


1 Answers

Fields are analyzed with the standard analyzer by default. If you would like to check exact match, you could store your field not analyzed also e.g:

"dog":{
            "type":"multi_field",
            "fields":{
                "dog":{
                    "include_in_all":false,
                    "type":"string",
                    "index":"not_analyzed",
                    "store":"no"
                },
                "_tokenized":{
                    "include_in_all":false,
                    "type":"string",
                    "index":"analyzed",
                    "store":"no"
                }
            }
        }

Then you can query the dog-field for exact matches, and dog._tokenized for analyzed queries (like fulltext)

like image 150
runarM Avatar answered Sep 22 '22 12:09

runarM