Leon Kennedy
Leon Kennedy

Reputation: 63

Elasticsearch term query with colons

I have a string field "title"(not analyzed) in elasticsearch. A document has title "Garfield 2: A Tail Of Two Kitties (2006)".

When I use the following json to query, no result returns.

{"query":{"term":{"title":"Garfield 2: A Tail Of Two Kitties (2006)"}}}

I tried to escape the colon character and the braces, like:

{"query":{"term":{"title":"Garfield 2\\: A Tail Of Two Kitties \\(2006\\)"}}}

Still not working.

Upvotes: 2

Views: 3054

Answers (1)

Vineeth Mohan
Vineeth Mohan

Reputation: 19263

Term query wont tokenize or apply analyzers to the search text. Instead if looks for the exact match which wont work as the string fields are analyzed/tokenized by default.

To give this a better explanation -

Lets say there is a string value as - "I am in summer:camp" When indexing this its broken into tokens as below -

"I am in summer:camp" => [ I , am , in , summer , camp ]

Hence even if you do a term search for "I am in summer:camp" , it wont still work as the token "I am in summer:camp" is not present in the index. Something like phrase query might work better here. Or you can leave "index" field as "not_analyzed" to make sure that string is not tokenized.

Upvotes: 2

Related Questions