Elasticsearch Term Level Queries – Term & Terms Query

We had known that Full text queries will analyse query string before executing. In this tutorial, we’re gonna look at term-level queries (Term & Terms Query) that operate on the exact terms which are stored in the inverted index.

These queries are usually used for structured data (numbers, dates, enums…), rather than full text fields.

I. Term Query

term query finds documents that contain the exact term specified in the inverted index.

For example:
This request will find documents which contain the exact term “publisher” in the inverted index of the “title” field.



boost parameter can give term query a higher relevance score than another query.
The default boost value is 1.

For example:


Term Query for text vs keyword

– text: full text (body of an email for example)
– keyword: exact value (for example: email address or a zip code – numbers, dates, and keywords)

text are analyzed: values -> analyzer -> list of terms -> inverted index.
keyword -> inverted index.

term query looks for the exact term in the field’s inverted index without knowing anything about the analyzer.

Look at example that we create an index, specify the field mappings, and index a document:

fulltext field (“type”: “text”) -> analyzer -> inverted index [java, sample, approach]
exactvalue field (“type”: “keyword”) -> inverted index [Java Sample Approach]

Case 1: Matched: exactvalue contains term “Java Sample Approach”.

Case 2: Not Matched: fulltext field only contains the terms [java, sample, approach], not [Java Sample Approach].

Case 3: Matched

Case 4: Matched: fulltext field first analyzes the query string “Java Sample Approach”, then looks for documents containing “java” or “sample” or “approach” or all.

II. Terms Query

Similar to the term query, the terms query can search for multiple terms in a document’s field. Result documents will have fields that match any of the provided terms (not analyzed).

For example:


Look up mechanism

This mechanism helps us fetch term values from a document in an index.

The terms lookup mechanism supports the following options:
– index (defaults to the current index)
– type
– id
– path: thee field specified as path to fetch the actual values for the terms filter.
– routing: a custom routing value to be used when retrieving the external terms doc.

For example, we want to get posts from friends of a Facebook user:


By grokonez | November 2, 2017.

Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *