Skip to main content

Query Autocomplete

The autocomplete is a type of search that returns a list of results that match the query. These suggestors help you quickly build an autocomplete.

Completion Suggestor

Usage

import Searchkit, {CompletionSuggester} from '@searchkit/sdk';

const request = Searchkit({
host: 'http://localhost:9200',
index: 'test',
suggestions: [
new CompletionSuggester({
index: 'popular-queries',
identifier: 'completion',
field: 'suggestions',
}),
],
});

const results = await request.executeSuggestions('swe');

will give back an example response of the following:

[
{
"identifier": "completion",
"suggestions": ["Sweatpants", "Sweats", "Swimwear"]
}
]

Field Mapping

The field that is used for the suggestions is mapped to the suggestions field in the index. Behind the scenes, its using the completion suggester. Read more information about its capability

{
"mappings": {
"properties": {
"suggestions": {
"type": "completion"
}
}
}
}

Options

OptionDescription
identifierRequired.
indexOptional. Used if the suggestions are contained within a different index. Overrides the main index option.
fieldRequired. String. field that is of type completion. Used for suggesting terms
sizeOptional. Number. number of suggestions to return. Default is 5
skip_duplicatesOptional. Boolean. Whether to skip duplicate suggestions. Default is true

Hits Suggestor

Will return a list of hits that match the query. In this example we are using the Prefix Query which uses a multi-match bool_prefix type. The hits suggestor is used to return a list of hits that match the query.

In this example, title_suggest is a search_as_you_type field. Read more about it.

Usage

import Searchkit, {HitsSuggestor, PrefixQuery} from '@searchkit/sdk';

const request = Searchkit({
host: 'http://localhost:9200',
index: 'test',
suggestions: [
new HitsSuggestor({
index: 'mrp-products',
identifier: 'hits',
hits: {
fields: ['name', 'designerName'],
highlightedFields: ['name', 'designerName'],
},
query: new PrefixQuery({
fields: ['title_suggest'],
}),
}),
],
});

const results = await request.executeSuggestions('shi');

Will get a response like

[
{
"identifier": "hits",
"hits": [
{
"fields": {
"designerName": "GUCCI",
"name": "Slim-Fit Logo-Jacquard Cotton and Silk-Blend Polo Shirt"
},
"highlight": {
"title_suggest._index_prefix": [
"GUCCI Slim-Fit Logo-Jacquard Cotton and Silk-Blend Polo <em>Shirt</em>"
]
},
"id": "10163292707229244"
},
{
"fields": {
"designerName": "FEAR OF GOD",
"name": "Logo-Flocked Cotton-Jersey T-Shirt"
},
"highlight": {
"title_suggest._index_prefix": [
"FEAR OF GOD Logo-Flocked Cotton-Jersey T-<em>Shirt</em>"
]
},
"id": "560971904241887"
}
]
}
]

Options

OptionDescription
identifierRequired.
indexOptional. Used if the suggestions are contained within a different index. Overrides the main index option.
hits.fieldsRequired. Field values to bring back
hits.highlightedFieldsOptional. Fields to highlight
queryOptional. Number. number of suggestions to return. Default is 5
skip_duplicatesOptional. Boolean. Whether to skip duplicate suggestions. Default is true