Sau
Sau

Reputation: 21

Get terms matching prefix query in ElasticSearch

The result of ElasticSearch prefix query returns only the documents matching the query. Is there a way I can configure the query to also return the "exact term(s)" for each document that resulted in a match?

Upvotes: 2

Views: 617

Answers (2)

mayid
mayid

Reputation: 1775

Well, using Highlighting you get the text with the matched words highlighted. Ie:

GET /_search
{
  "query": {
    "prefix": { "DESCRIPTION": "arthu"}
  },
    "highlight": {
        "fields" : {
            "DESCRIPTION" : {}
        }
    }
}

Retrieves something like:

{
        "_index": "abc",
        "_type": "xyz",
        "_id": "107507",
        "_score": 1,
        "_source": {
          "DESCRIPTION": "Arthur: Attack of the Turbo Tibbles/D.W. Tricks the Tooth Fairy"
        },
        "highlight": {
          "DESCRIPTION": [
            "<em>Arthur</em>: Attack of the Turbo Tibbles/D.W. Tricks the Tooth Fairy"
          ]
        }
      }

You can also customize the wrapping tag (em). But I'm not sure how to extract only the matched words from there.

Take a look at this thread anyway. I don't understand the implementation but it may help: Determining which words were matched in a fuzzy search

Basically he proposed to do this:

GET /common_clarovideo/grupo/_search
{
  "_source": [
    "NOMBRE_INTERNO"
  ],
  "query": {
    "prefix": { "NOMBRE_INTERNO": "arthu"}
  },
    "highlight": {
        "fields" : {
            "NOMBRE_INTERNO" : {"fragment_size" : 5}
        }
    }
}

Meaning "fragment_size" must match the length of your term. Hope this helps.

Upvotes: 2

Lax
Lax

Reputation: 1167

Yes.you need to use term instead match and you will get the exact term. See documentation

Upvotes: 1

Related Questions