{
"query": {
"term": {
"status": {
"value": "active"
}
}
}
}The term query in Elasticsearch matches documents where the field value exactly matches the given term. It does not analyze or tokenize the field value.
So it matches documents where 'status' is exactly 'active'.
Term queries work best on keyword fields because these fields are not analyzed and store exact values.
Text fields are analyzed and broken into tokens, so term query may not match as expected.
{
"query": {
"term": {
"user": "John Doe"
}
}
}Term query matches exact values. If 'user' is a text field analyzed into tokens like 'John' and 'Doe', the term query looking for 'John Doe' as a single token will not find matches.
Use match query for analyzed text fields.
The term query expects a single value, not an array. Option B uses an array which is invalid syntax for term query.
Options A, B, and D are valid syntaxes.
{
"query": {
"term": {
"category.keyword": "Books"
}
}
}The term query on 'category.keyword' matches exactly 'Books'.
Documents 1 and 4 have 'Books' exactly. Document 2 has 'books' (lowercase), and 3 has 'Books and More' which do not match exactly.