Elasticsearch Elasticsearch Exact Match

By Opster Team

Updated: Jun 22, 2023

| 2 min read

Introduction

When querying Elasticsearch, it’s often necessary to perform exact match searches to retrieve specific documents that match a given criterion. In this article, we will discuss various techniques and best practices for exact match searches in Elasticsearch. If you want to learn about Elasticsearch match, multi-match, and match phrase queries, check out this guide. You should also take a look at this guide, which contains a detailed explanation on Elasticsearch match_only_text field type (for storage optimization).

Elasticsearch Exact Match: Techniques and Best Practices

1. Keyword Data Type

For exact match searches, it’s essential to use the keyword data type for the fields you want to query. The keyword data type is not analyzed, which means the field’s value remains unchanged during indexing. This allows for exact match searches without worrying about tokenization or normalization.

Example mapping:

{
"mappings": {
"properties": {
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}

2. Term Query

The term query is the most straightforward way to perform an exact match search. It looks for the exact term in the field without any analysis. Use the term query with the keyword field to retrieve documents that match the given value.

Example query:

{
"query": {
"term": {
"title.keyword": "Elasticsearch Exact Match"
}
}
}

3. Match Query with Operator

The match query is typically used for full-text search, but you can also use it for exact match searches by setting the “operator” parameter to “and”. This ensures that all terms in the query must be present in the field for a document to be considered a match.

Example query:

{
"query": {
"match": {
"title": {
"query": "Elasticsearch Exact Match",
"operator": "and"
}
}
}
}

4. Phrase Query

A phrase query can also be used for exact match searches by searching for the exact sequence of terms in the field. This is particularly useful when you need to match a specific phrase or sequence of words.

Example query:

{
"query": {
"match_phrase": {
"title": "Elasticsearch Exact Match"
}
}
}

5. Filter Context

When performing exact match searches, it’s recommended to use a filter context instead of a query context. This improves performance as filters are cached and do not contribute to the relevance score calculation.

Example query:

{
"query": {
"bool": {
"filter": {
"term": {
"title.keyword": "Elasticsearch Exact Match"
}
}
}
}
}

Conclusion 

In conclusion, there are several techniques for exact match searches in Elasticsearch. By using the keyword data type, term query, match query with AND operator, phrase query, and filter context, you can efficiently perform exact match searches and retrieve the desired documents.

How helpful was this guide?

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?