Keyword search not behaving as expected

Hello,

I'm having an issue with keyword search not behaving how I would expect from the docs. I can see that the following document exists in my index:

{
      "_index": "my-index",
      "_type": "_doc",
      "_id": "A-A1055-62",
      "_score": 9.326463,
      "_source": {
          "color": "AA105562",
          "description": "red"
      }
  }

But a keyword term query on my-index does not return any results:

{
    "size": 1,
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "color.keyword": {
                            "value": "AA105562",
                            "boost": 1.0
                        }
                    }
                }
            ],
            "adjust_pure_negative": true,
            "boost": 1.0
        }
    }
}

My mapping is set as:

"my-index": {
   "mappings": {
      "properties": {
          "color": {
              "type": "keyword"
          },
        }
    }
}

From the docs, I would have expected that a term query on color.keyword would find the document since the strings are identical. What am I missing?

You do not have a keyword multifield. Does this work?

{
    "size": 1,
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "color": {
                            "value": "AA105562",
                            "boost": 1.0
                        }
                    }
                }
            ],
            "adjust_pure_negative": true,
            "boost": 1.0
        }
    }
}
1 Like

Yes it does - could you explain why that is? Or I'm not quite sure what is meant by a keyword multifield

If you use dynamic mapping the default mapping is to map the field as text (analysed) and create a keyword sub field which is mapped as keyword. Since your field itself is mapped as a keyword you have no subfield defined so need to reference the field itself.

1 Like

That makes sense - thank you so much!!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.