Reputation: 350
I'm trying to search for a document, by field value. The field is of type String that was defined in the index like this:
"field" : {
"type" : "string"
}
There are no other settings to the index but the mappings.
I tried various queries to try to find documents with field="test"
, and every time I get no results or results that contain that substring "test"
. I want the document where "field"
matches "test"
exactly.
I tried match
, match_phrase
, term
and query_string
, none of which seems to work as I want.
Upvotes: 3
Views: 3914
Reputation: 1996
It seems that you are on an Elasticsearch version prior to version 6, which still supported the String
type. More recent version of Elasticsearch replaced the string
field-type with types keyword
and text
to make it easier to support the two use-cases of exact-match
and full-text
search use cases.
You need to change your mapping to support exact-match search as follows:
{
"field": {
"type" "string",
"index": "not_analyzed"
}
}
As you cannot change an existing mapping, you need to do that change on a new index and get everything re-indexed.
If you are on Elasticsearch version 6+ simply use a field of type keyword
for exact-match searches. By default string fields get mapped to a multi-field made of of 2 fields:
text
(for full-text search), referenced by keyword
(for exact match search), referenced by .keywordHereby an interesting blog post about that topic: Strings are dead, long live strings!
Upvotes: 1
Reputation: 7770
You need to use term
query with keyword like this
{
"query" : {
"term" : {
"field.keyword" : "test"
}
}
}
hope this helps.
Upvotes: 3