Why is my term query not working?

Here is my settings file -

{
	"settings": {
		"mappings": {
			"default": {
				"properties": {
					"AIRPORT_CODE": {
						"type": "text"
					},
					"PROVINCE_NAME": {
						"type": "text"
					},
					"AIRPORT_NAME": {
						"type": "text"
					},
					"CITY_NAME": {
						"type": "text"
					},
					"TYPE": {
						"type": "keyword"
					},
					"COUNTRY_NAME": {
						"type": "text"
					}
				}
			}
		}
	}
}

Here are my sample documents

{
  "AIRPORT_CODE": "SQA",
  "PROVINCE_NAME": "California",
  "AIRPORT_NAME": "Santa Ynez Airport",
  "CITY_NAME": "SANTA YNEZ",
  "TYPE": "AIRPORT"
}

{
  "PROVINCE_NAME": "SANTIAGO",
  "CITY_NAME": "SANTIAGO",
  "COUNTRY_NAME": "DOMINICAN REPUBLIC",
  "TYPE": "HOTEL"
}

Here is my search -

{
	"size": 4,
	"timeout": "2m",
	"query": {
		"bool": {
			"must": [
				{
					"multi_match": {
						"query": "SQA"
					}
				}
			],
			"filter": [
				{
					"term": {
						"TYPE": "AIRPORT"
					}
				}
			]
		}
	},
	"explain": false
}

The "TYPE" field has been defined as keyword. But the above query doesn't return anything. If change the "TYPE" value from "AIRPORT" to "airport" (lowercase), I get the results back. What am I doing wrong?

Note # My effort is to get this query working so I can take the advantage of filter cache (node query cache).

Thanks in advance!

Check the mappings on the server itself. You probably have keyword analyser with lowercase filter. Case must must when querying a field processed by the keyword analyser. Lots of info on Google for this.

I don't think I do. I am running all the above on a new index, you can do the same. Are you saying for keyword analyzer, it has lowercase tokenizer/filter by default?

I am suggesting that's what you find out by checking on the server. I don't
know off the top of my head.

When do this -
POST _analyze
{
"tokenizer": "keyword",
"text": "AIRPORT"
}

I get back -
{
"tokens": [
{
"token": "AIRPORT",
"start_offset": 0,
"end_offset": 7,
"type": "word",
"position": 0
}
]
}

So by default it doesn't convert lower case. Which is what I have as well.

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