- Elasticsearch Guide: other versions:
- Getting Started
- Set up Elasticsearch
- Set up X-Pack
- Breaking changes
- Breaking changes in 5.5
- Breaking changes in 5.4
- Breaking changes in 5.3
- Breaking changes in 5.2
- Breaking changes in 5.1
- Breaking changes in 5.0
- Search and Query DSL changes
- Mapping changes
- Percolator changes
- Suggester changes
- Index APIs changes
- Document API changes
- Settings changes
- Allocation changes
- HTTP changes
- REST API changes
- CAT API changes
- Java API changes
- Packaging
- Plugin changes
- Filesystem related changes
- Path to data on disk
- Aggregation changes
- Script related changes
- API Conventions
- Document APIs
- Search APIs
- Aggregations
- Metrics Aggregations
- Avg Aggregation
- Cardinality Aggregation
- Extended Stats Aggregation
- Geo Bounds Aggregation
- Geo Centroid Aggregation
- Max Aggregation
- Min Aggregation
- Percentiles Aggregation
- Percentile Ranks Aggregation
- Scripted Metric Aggregation
- Stats Aggregation
- Sum Aggregation
- Top hits Aggregation
- Value Count Aggregation
- Bucket Aggregations
- Adjacency Matrix Aggregation
- Children Aggregation
- Date Histogram Aggregation
- Date Range Aggregation
- Diversified Sampler Aggregation
- Filter Aggregation
- Filters Aggregation
- Geo Distance Aggregation
- GeoHash grid Aggregation
- Global Aggregation
- Histogram Aggregation
- IP Range Aggregation
- Missing Aggregation
- Nested Aggregation
- Range Aggregation
- Reverse nested Aggregation
- Sampler Aggregation
- Significant Terms Aggregation
- Terms Aggregation
- Pipeline Aggregations
- Avg Bucket Aggregation
- Derivative Aggregation
- Max Bucket Aggregation
- Min Bucket Aggregation
- Sum Bucket Aggregation
- Stats Bucket Aggregation
- Extended Stats Bucket Aggregation
- Percentiles Bucket Aggregation
- Moving Average Aggregation
- Cumulative Sum Aggregation
- Bucket Script Aggregation
- Bucket Selector Aggregation
- Serial Differencing Aggregation
- Matrix Aggregations
- Caching heavy aggregations
- Returning only aggregation results
- Aggregation Metadata
- Returning the type of the aggregation
- Metrics Aggregations
- Indices APIs
- Create Index
- Delete Index
- Get Index
- Indices Exists
- Open / Close Index API
- Shrink Index
- Rollover Index
- Put Mapping
- Get Mapping
- Get Field Mapping
- Types Exists
- Index Aliases
- Update Indices Settings
- Get Settings
- Analyze
- Index Templates
- Shadow replica indices
- Indices Stats
- Indices Segments
- Indices Recovery
- Indices Shard Stores
- Clear Cache
- Flush
- Refresh
- Force Merge
- cat APIs
- Cluster APIs
- Query DSL
- Mapping
- Analysis
- Anatomy of an analyzer
- Testing analyzers
- Analyzers
- Normalizers
- Tokenizers
- Token Filters
- Standard Token Filter
- ASCII Folding Token Filter
- Flatten Graph Token Filter
- Length Token Filter
- Lowercase Token Filter
- Uppercase Token Filter
- NGram Token Filter
- Edge NGram Token Filter
- Porter Stem Token Filter
- Shingle Token Filter
- Stop Token Filter
- Word Delimiter Token Filter
- Word Delimiter Graph Token Filter
- Stemmer Token Filter
- Stemmer Override Token Filter
- Keyword Marker Token Filter
- Keyword Repeat Token Filter
- KStem Token Filter
- Snowball Token Filter
- Phonetic Token Filter
- Synonym Token Filter
- Synonym Graph Token Filter
- Compound Word Token Filters
- Reverse Token Filter
- Elision Token Filter
- Truncate Token Filter
- Unique Token Filter
- Pattern Capture Token Filter
- Pattern Replace Token Filter
- Trim Token Filter
- Limit Token Count Token Filter
- Hunspell Token Filter
- Common Grams Token Filter
- Normalization Token Filter
- CJK Width Token Filter
- CJK Bigram Token Filter
- Delimited Payload Token Filter
- Keep Words Token Filter
- Keep Types Token Filter
- Classic Token Filter
- Apostrophe Token Filter
- Decimal Digit Token Filter
- Fingerprint Token Filter
- Minhash Token Filter
- Character Filters
- Modules
- Index Modules
- Ingest Node
- Pipeline Definition
- Ingest APIs
- Accessing Data in Pipelines
- Handling Failures in Pipelines
- Processors
- Append Processor
- Convert Processor
- Date Processor
- Date Index Name Processor
- Fail Processor
- Foreach Processor
- Grok Processor
- Gsub Processor
- Join Processor
- JSON Processor
- KV Processor
- Lowercase Processor
- Remove Processor
- Rename Processor
- Script Processor
- Set Processor
- Split Processor
- Sort Processor
- Trim Processor
- Uppercase Processor
- Dot Expander Processor
- X-Pack APIs
- Info API
- Explore API
- Machine Learning APIs
- Close Jobs
- Create Datafeeds
- Create Jobs
- Delete Datafeeds
- Delete Jobs
- Delete Model Snapshots
- Flush Jobs
- Get Buckets
- Get Categories
- Get Datafeeds
- Get Datafeed Statistics
- Get Influencers
- Get Jobs
- Get Job Statistics
- Get Model Snapshots
- Get Records
- Open Jobs
- Post Data to Jobs
- Preview Datafeeds
- Revert Model Snapshots
- Start Datafeeds
- Stop Datafeeds
- Update Datafeeds
- Update Jobs
- Update Model Snapshots
- Security APIs
- Watcher APIs
- Definitions
- How To
- Testing
- Glossary of terms
- Release Notes
- 5.5.3 Release Notes
- 5.5.2 Release Notes
- 5.5.1 Release Notes
- 5.5.0 Release Notes
- 5.4.3 Release Notes
- 5.4.2 Release Notes
- 5.4.1 Release Notes
- 5.4.0 Release Notes
- 5.3.3 Release Notes
- 5.3.2 Release Notes
- 5.3.1 Release Notes
- 5.3.0 Release Notes
- 5.2.2 Release Notes
- 5.2.1 Release Notes
- 5.2.0 Release Notes
- 5.1.2 Release Notes
- 5.1.1 Release Notes
- 5.1.0 Release Notes
- 5.0.2 Release Notes
- 5.0.1 Release Notes
- 5.0.0 Combined Release Notes
- 5.0.0 GA Release Notes
- 5.0.0-rc1 Release Notes
- 5.0.0-beta1 Release Notes
- 5.0.0-alpha5 Release Notes
- 5.0.0-alpha4 Release Notes
- 5.0.0-alpha3 Release Notes
- 5.0.0-alpha2 Release Notes
- 5.0.0-alpha1 Release Notes
- 5.0.0-alpha1 Release Notes (Changes previously released in 2.x)
WARNING: Version 5.5 of Elasticsearch has passed its EOL date.
This documentation is no longer being maintained and may be removed. If you are running this version, we strongly advise you to upgrade. For the latest information, see the current release documentation.
Completion Suggester
editCompletion Suggester
editIn order to understand the format of suggestions, please read the Suggesters page first.
The completion
suggester provides auto-complete/search-as-you-type
functionality. This is a navigational feature to guide users to
relevant results as they are typing, improving search precision.
It is not meant for spell correction or did-you-mean functionality
like the term
or phrase
suggesters.
Ideally, auto-complete functionality should be as fast as a user
types to provide instant feedback relevant to what a user has already
typed in. Hence, completion
suggester is optimized for speed.
The suggester uses data structures that enable fast lookups,
but are costly to build and are stored in-memory.
Mapping
editTo use this feature, specify a special mapping for this field, which indexes the field values for fast completions.
PUT music { "mappings": { "song" : { "properties" : { "suggest" : { "type" : "completion" }, "title" : { "type": "keyword" } } } } }
Mapping supports the following parameters:
-
analyzer
-
The index analyzer to use, defaults to
simple
. In case you are wondering why we did not opt for thestandard
analyzer: We try to have easy to understand behaviour here, and if you index the field contentAt the Drive-in
, you will not get any suggestions fora
, nor ford
(the first non stopword). -
search_analyzer
-
The search analyzer to use, defaults to value of
analyzer
. -
preserve_separators
-
Preserves the separators, defaults to
true
. If disabled, you could find a field starting withFoo Fighters
, if you suggest forfoof
. -
preserve_position_increments
-
Enables position increments, defaults to
true
. If disabled and using stopwords analyzer, you could get a field starting withThe Beatles
, if you suggest forb
. Note: You could also achieve this by indexing two inputs,Beatles
andThe Beatles
, no need to change a simple analyzer, if you are able to enrich your data. -
max_input_length
-
Limits the length of a single input, defaults to
50
UTF-16 code points. This limit is only used at index time to reduce the total number of characters per input string in order to prevent massive inputs from bloating the underlying datastructure. Most usecases won’t be influenced by the default value since prefix completions seldom grow beyond prefixes longer than a handful of characters.
Indexing
editYou index suggestions like any other field. A suggestion is made of an
input
and an optional weight
attribute. An input
is the expected
text to be matched by a suggestion query and the weight
determines how
the suggestions will be scored. Indexing a suggestion is as follows:
PUT music/song/1?refresh { "suggest" : { "input": [ "Nevermind", "Nirvana" ], "weight" : 34 } }
The following parameters are supported:
-
input
- The input to store, this can be an array of strings or just a string. This field is mandatory.
-
weight
- A positive integer or a string containing a positive integer, which defines a weight and allows you to rank your suggestions. This field is optional.
You can index multiple suggestions for a document as follows:
PUT music/song/1?refresh { "suggest" : [ { "input": "Nevermind", "weight" : 10 }, { "input": "Nirvana", "weight" : 3 } ] }
You can use the following shorthand form. Note that you can not specify a weight with suggestion(s) in the shorthand form.
PUT music/song/1?refresh { "suggest" : [ "Nevermind", "Nirvana" ] }
Querying
editSuggesting works as usual, except that you have to specify the suggest
type as completion
. Suggestions are near real-time, which means
new suggestions can be made visible by refresh and
documents once deleted are never shown. This request:
POST music/_search?pretty { "suggest": { "song-suggest" : { "prefix" : "nir", "completion" : { "field" : "suggest" } } } }
returns this response:
{ "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits": ... "took": 2, "timed_out": false, "suggest": { "song-suggest" : [ { "text" : "nir", "offset" : 0, "length" : 3, "options" : [ { "text" : "Nirvana", "_index": "music", "_type": "song", "_id": "1", "_score": 1.0, "_source": { "suggest": ["Nevermind", "Nirvana"] } } ] } ] } }
_source
meta-field must be enabled, which is the default
behavior, to enable returning _source
with suggestions.
The configured weight for a suggestion is returned as _score
. The
text
field uses the input
of your indexed suggestion. Suggestions
return the full document _source
by default. The size of the _source
can impact performance due to disk fetch and network transport overhead.
To save some network overhead, filter out unnecessary fields from the _source
using source filtering to minimize
_source
size. Note that the _suggest endpoint doesn’t support source
filtering but using suggest on the _search
endpoint does:
POST music/_search?size=0 { "_source": "suggest", "suggest": { "song-suggest" : { "prefix" : "nir", "completion" : { "field" : "suggest" } } } }
Which should look like:
{ "took": 6, "timed_out": false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits": { "total" : 0, "max_score" : 0.0, "hits" : [] }, "suggest": { "song-suggest" : [ { "text" : "nir", "offset" : 0, "length" : 3, "options" : [ { "text" : "Nirvana", "_index": "music", "_type": "song", "_id": "1", "_score": 1.0, "_source": { "suggest": ["Nevermind", "Nirvana"] } } ] } ] } }
The basic completion suggester query supports the following parameters:
-
field
- The name of the field on which to run the query (required).
-
size
-
The number of suggestions to return (defaults to
5
).
The completion suggester considers all documents in the index. See Context Suggester for an explanation of how to query a subset of documents instead.
In case of completion queries spanning more than one shard, the suggest is executed in two phases, where the last phase fetches the relevant documents from shards, implying executing completion requests against a single shard is more performant due to the document fetch overhead when the suggest spans multiple shards. To get best performance for completions, it is recommended to index completions into a single shard index. In case of high heap usage due to shard size, it is still recommended to break index into multiple shards instead of optimizing for completion performance.
Fuzzy queries
editThe completion suggester also supports fuzzy queries - this means, you can have a typo in your search and still get results back.
POST music/_search?pretty { "suggest": { "song-suggest" : { "prefix" : "nor", "completion" : { "field" : "suggest", "fuzzy" : { "fuzziness" : 2 } } } } }
Suggestions that share the longest prefix to the query prefix
will
be scored higher.
The fuzzy query can take specific fuzzy parameters. The following parameters are supported:
|
The fuzziness factor, defaults to |
|
if set to |
|
Minimum length of the input before fuzzy
suggestions are returned, defaults |
|
Minimum length of the input, which is not
checked for fuzzy alternatives, defaults to |
|
If |
If you want to stick with the default values, but
still use fuzzy, you can either use fuzzy: {}
or fuzzy: true
.
Regex queries
editThe completion suggester also supports regex queries meaning you can express a prefix as a regular expression
POST music/_search?pretty { "suggest": { "song-suggest" : { "regex" : "n[ever|i]r", "completion" : { "field" : "suggest" } } } }
The regex query can take specific regex parameters. The following parameters are supported:
|
Possible flags are |
|
Regular expressions are dangerous because it’s easy to accidentally
create an innocuous looking one that requires an exponential number of
internal determinized automaton states (and corresponding RAM and CPU)
for Lucene to execute. Lucene prevents these using the
|