Problem:
Give examples for basic term level queries.
Solution Summary:
The term-level queries operate on the exact terms. The queries that come under term level query group are: term, terms, terms_set, range, exists, prefix, wildcard, regexp, fuzzy, type, ids.
Prerequisites:
Set up accounts index from accounts.json as explained here.
Solution Steps:
Case 1 - Search for account holders with age 25 (term)
GET /accounts/_search
{
"query": {
"term": {
"age": 25
}
}
}
Note: _doc type is optional here.
Alternate (Expanded) Form
GET /accounts/_search
{
"query": {
"term": {
"age": {
"value":25
}
}
}
}
Case 2 - Search for multiple terms (terms)
GET /accounts/_search
{
"query": {
"terms": {
"city":["hamilton", "hatteras"]
}
}
}
Note: Even though the actual document contain city names starting with capital letters, it is stored as small case in the inverted index. Term query unlike full-text query will not do analysis on the query context. Please read more explanation here.
Case 3 - Search By IDs
GET /accounts/_search
{
"query": {
"ids": {
"values": [1,2,3]
}
}
}
Case 4 - Matching documents with non-null values
GET /accounts/_doc/_search
{
"query": {
"exists": {
"field":"lastname"
}
}
}
Case 5 - Search with Range (Simple Range Query)
GET /accounts/_search
{
"query": {
"range" : {
"age" : {
"gte" : 18,
"lte" : 21
}
}
}
}
Note:
-
Return documents with age between 10 and 30.
-
Interestingly, you can omit accounts and just use _search (GET /_search) to search across indexes.
-
Term level queries does not do analysis and as we have placed is inside query context, elastic search will give it a default score. Term level queries makes more sense in a filter context.
TODO
- Give examples for all missing term level query parameters.
- Refer here for list of all term level and full-text query parameters.

Recent comments