Mike Nishizawa
Mike Nishizawa

Reputation: 2042

ElasticSearch fails to return a result on a keyword field where the value contains an apostrophe

The application I am working on allows any character to be used in a tag. Tags are stored in the document as an array. Other characters seem to work fine but an apostrophe does not. In a previous version of ES, we had this field mapped as an analyzed string but the current version does not allow us to use some of the aggregations we were using so I changed it to a keyword and this problem surfaced. When I query for documents with the specified tag, I get no results even though I know my query is correct for the field(the same query returns results for all other tags except tags containing an apostrophe). The setup is as follows:

Field Mapping:

{ 
    "tags" : {
       "type" : "keyword",
       "store" : true
    }
}

Field data:

"tags" : [
    "Mike's Tag"
  ]

Query

{
  "query": {
    "bool": {
      "should": [
        {
          "query_string": {
            "query": "tags:\"Mike's Test\"",
            "default_operator": "AND"
          }
        }
      ]
    }
  }
}

Upvotes: 0

Views: 152

Answers (1)

Nishant
Nishant

Reputation: 7874

If you don't want scoring on tag you can use term query to get the desired result.

Use the query below:

{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "tags": "Mike's Tag"
          }
        }
      ]
    }
  }
}

Update after comments:

In case you want to do it via query_string here is the query:

{
  "query": {
    "query_string": {
      "query": "Mike's Tag",
      "fields": [
        "tags"
      ]
    }
  }
}

NOTE: I would still suggest you to use term or terms query in filter context if you don't want scoring.

Output of above query (using term query):

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0,
    "hits": [
      {
        "_index": "test",
        "_type": "_doc",
        "_id": "1",
        "_score": 0,
        "_source": {
          "tags": [
            "Mike's Tag",
            "Another Tag"
          ]
        }
      },
      {
        "_index": "test",
        "_type": "_doc",
        "_id": "2",
        "_score": 0,
        "_source": {
          "tags": [
            "Mike's Tag",
            "Another one Tag"
          ]
        }
      }
    ]
  }
}

Upvotes: 1

Related Questions