gal007
gal007

Reputation: 7182

Elasticsearch: getting documents with exactly a certain value in a field

I have an index with documents that have some repeated textual content, and I need to retrieve the ones that have exactly the same value, not a similar value. So, for instance, consider each of the following lines as the value of the "text" propery of different documents:

So what I need is to retrieve just the two documents marked with (*). I tried:

GET news/_search
{
  "_source": ["text"],
  "min_score": 1,
  "query": {
    "simple_query_string" : {
        "query": "The car",
        "fields": ["text"],
        "flags": "NONE",
        "minimum_should_match": "100%"
    }
  }
} 

But it always retrieves all the 4 documents. I also tried with match_phrase and I had the same results.

PS: I need to be able to run both queries: one to retrieve the 4 documents, and other one to retrieve just 2.

This is the mapping:

{
  "news" : {
    "aliases" : { },
    "mappings" : {
      "tweet" : {
        "properties" : {
          "text" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          ...
        }
      }
    }
  }
}

Any idea?

Thanks

Upvotes: 1

Views: 29

Answers (1)

Richa
Richa

Reputation: 7649

For exact match you should go for Term Query of elasticsearch on the keyword type of the field. Use this for ex.

{
  "query": {
    "term": {
      "text.keyword": {
        "value": "The car"
      }
    }
  }
}

The above query will return only 2 results. But if you want to fetch all the results go for Match query

{
  "query": {
    "match": {
      "text": "The car"
    }
  }
}

This will return you all the four results.

Hope this helps

Upvotes: 1

Related Questions