Open In App

Completion suggesters in Elasticsearch

Last Updated : 09 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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"
}
}
}
}
1__
Index Configuration

Indexing 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

  1. 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.
  2. 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.
  3. 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.
  4. 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.


Next Article
Article Tags :

Similar Reads