Completion suggesters in Elasticsearch
Last Updated :
09 Jul, 2024
Elasticsearch is a scalable search engine that is based on Apache Lucene and provides numerous capabilities related to full-text search, analytics, and others. Of all these features, the completion suggester can be considered one of the most helpful tools built to improve the search functionality through the use of the autocomplete option.
In this article, we will examine the ins and outs of how to use one of the helpful signal features of Elasticsearch called the completion suggester, including its setup, usage, and optimization.
Understanding Completion Suggesters
Completion suggesters are intended to provide fast, effective, and accurate queries to provide the users with relevant autocomplete suggestions. It is especially helpful when a swift outcome is required like in the search bar, auto-complete field, and instant search elements. As opposed to other types of suggesters, the completion suggesters provide faster results, as they use in-memory data structures for data storage.
Syntax
To set up a completion suggester, you need to define a mapping for the field you want to use as a suggestion source. This involves setting the field type to completion.
Index Configuration:
PUT /my_index
{
"mappings": {
"properties": {
"suggest_field": {
"type": "completion"
}
}
}
}
Index ConfigurationIndexing Documents:
POST /my_index/_doc/1
{
"suggest_field": {
"input": ["Elasticsearch", "Elastic", "Search Engine"]
}
}
Examples of Completion Suggesters in Elasticsearch
Example 1: Basic Suggest Query
To use the completion suggester, you need to perform a suggest query. Here’s an example using the previously defined suggest_field:
POST /my_index/_search
{
"suggest": {
"my_suggestion": {
"prefix": "ela",
"completion": {
"field": "suggest_field"
}
}
}
}
Explanation: This query returns suggestions that match the prefix "ela", such as "Elasticsearch" and "Elastic".
Example 2: Contextual Suggestions
Completion suggesters can provide contextual suggestions by using context mappings. Here’s how to define a context mapping and use it in queries:
Context Mapping:
PUT /my_index
{
"mappings": {
"properties": {
"suggest_field": {
"type": "completion",
"contexts": [
{
"name": "location",
"type": "geo"
}
]
}
}
}
}
Indexing with Context:
POST /my_index/_doc/1
{
"suggest_field": {
"input": ["Pizza Place"],
"contexts": {
"location": {
"lat": 40.7128,
"lon": -74.0060
}
}
}
}
Query with Context:
POST /my_index/_search
{
"suggest": {
"my_suggestion": {
"prefix": "piz",
"completion": {
"field": "suggest_field",
"contexts": {
"location": {
"lat": 40.7128,
"lon": -74.0060
}
}
}
}
}
}
Explanation: This query provides suggestions for "Pizza Place" within the specified geographical context (New York City).
Example 3: Query for Autocomplete Suggestions
To use the completion suggester, perform a suggest query. Here’s an example using the previously defined suggest_field:
Basic Suggest Query
POST /my_index/_search
{
"suggest": {
"my_suggestion": {
"prefix": "ela",
"completion": {
"field": "suggest_field"
}
}
}
}
Explanation: This query returns suggestions that match the prefix "ela", such as "Elasticsearch" and "Elastic".
Example Response
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 0,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"suggest": {
"my_suggestion": [
{
"text": "ela",
"offset": 0,
"length": 3,
"options": [
{
"text": "Elasticsearch",
"_index": "my_index",
"_type": "_doc",
"_id": "1",
"_score": 1.0
},
{
"text": "Elastic",
"_index": "my_index",
"_type": "_doc",
"_id": "1",
"_score": 1.0
}
]
}
]
}
}
Key Features
- Real-time Suggestions: Completion suggesters give suggestions to users with an instant search that is going on in the background and this leads to an improved user experience as there is less effort required in the completion of the search.
- Optimized for Speed: They implement in-memory data structures that will require very little time to look up, which is useful when running applications with a lot of traffic.
- Typo Tolerance: It can be programmed to be able to force an error such that users will still be able to get good suggestions even if the input is not very proper.
- Contextual Suggestions: Auto-completion suggesters can recommend more context-sensitive prompts that can be more suited to the context of the search query.
Optimizing Completion Suggesters
To maximize the performance and relevance of your completion suggesters, consider the following best practices:
- Keep Suggestion Data Small: It is recommended to limit the number of input terms to keep the sizes of data structures in memory reasonable.
- Use Edge N-grams for Prefix Matching: It is recommended to perform preprocessing on your suggestion terms using edge n-grams to improve prefix matching.
- Regularly Update Suggestions: It is advisable to make frequent indexing of your data so that the suggestions that are provided to you are always latest.
- Analyze User Behavior: Engage in the use of analytics so as to be in a position to influence the users’ behavior as well as change the logic of the suggestions.
Conclusion
The completion suggester in Elasticsearch is actually a rather strong component that can drastically augment the usability of your application and offer quick queries for the user. Knowing its setup, use and further BT techniques can help to properly utilize this aspect in search applications, making sure that the users are able to easily find what they need.
No matter if you have an e-commerce site, CMS or any application which requires fast and accurate search suggestion, the completion suggester is the wonderful addition to your Elasticsearch toolbox.
Similar Reads
Suggesters in Elasticsearch
Elasticsearch is a powerful, open-source search and analytics engine widely used for full-text search, structured search, and analytics. One of its advanced features is the Suggester, which enhances the search experience by providing real-time, context-aware suggestions to users as they type their q
4 min read
Metric Aggregation in Elasticsearch
Elasticsearch is a powerful tool not just for search but also for performing complex data analytics. Metric aggregations are a crucial aspect of this capability, allowing users to compute metrics like averages, sums, and more on numeric fields within their data. This guide will delve into metric agg
6 min read
API Conventions in Elasticsearch
An API or Application Programming Interface serves as a bridge between different software applications and enables them to communicate effectively. Elasticsearch is a powerful search and analytics engine that provides a robust API that allows users to interact with the Elasticsearch server over HTTP
6 min read
Configuring TLS in Elasticsearch
Transport Layer Security (TLS) is an essential feature for securing communication in Elasticsearch. By encrypting data in transit, TLS helps protect sensitive information from interception and tampering. This article will guide you through configuring TLS in Elasticsearch, complete with examples and
4 min read
Searching Documents in Elasticsearch
Searching documents in Elasticsearch is a foundational skill for anyone working with this powerful search engine. Whether you're building a simple search interface or conducting complex data analysis, understanding how to effectively search and retrieve documents is essential. In this article, we'll
4 min read
Missing Aggregation in Elasticsearch
Elasticsearch is a powerful tool for full-text search and data analytics, and one of its core features is the aggregation framework. Aggregations allow you to summarize and analyze your data flexibly and efficiently. Among the various types of aggregations available, the "missing" aggregation is par
6 min read
Elasticsearch in Java Applications
Elasticsearch is a distributed, free, and public search and analytics engine, that works with all kinds of data, including numerical, textual, geographic, structured, and unstructured. Elasticsearch is lightweight. Elasticsearch has a total dependence size of only about 300 KB. It is just concerned
3 min read
Significant Aggregation in Elasticsearch
Elasticsearch provides a wide range of aggregation capabilities to analyze data in various ways. One powerful aggregation is the Significant Aggregation, which helps identify significant terms or buckets within a dataset. In this guide, we'll delve into the Significant Aggregation in Elasticsearch,
4 min read
Using Query DSL For Complex Search Queries in Elasticsearch
Elasticsearch is a powerful search engine that provides a flexible and powerful query language called Query DSL (Domain Specific Language). Query DSL allows you to write complex search queries to retrieve the most relevant data from your Elasticsearch indices. This article will guide you through the
6 min read
How to Become an Elasticsearch Engineer?
In the world of big data and search technologies, Elasticsearch has emerged as a leading tool for real-time data analysis and search capabilities. As businesses increasingly rely on data-driven decisions, the role of an Elasticsearch Engineer has become crucial. These professionals are responsible f
6 min read