tzortzik
tzortzik

Reputation: 5133

exact matches in elasticsearch (including whitespaces)

I am using ES 6.5 and I have the following documents

{"id": 1, "type": "a"}
{"id": 2, "type": "a b"}

Type mapping looks like this

"type": {
    "type": "text",
    "fields": {
        "keyword": {
            "type": "keyword",
            "ignore_above": 256
        }
    }
}

Desired behavior:

What I have tried:

I probably tried various other solutions suggested on stackoverflow but so far nothing worked as expected. Also please note the version of ES, in newer versions we have other options.

Upvotes: 0

Views: 205

Answers (1)

Bhavya
Bhavya

Reputation: 16172

You need to add .keyword to the type field. This uses the keyword analyzer instead of the standard analyzer (notice the ".keyword" after type field). Try out this below query -

{
  "query":{
    "match":{
      "type.keyword":"a b"
    }
  }
}

Search Result:

"hits": [
      {
        "_index": "65327197",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.6931471,
        "_source": {
          "id": 2,
          "type": "a b"
        }
      }
    ]

Upvotes: 1

Related Questions