Phrase Suggester
The phrase suggester corrects entire phrases based on n-gram matching. It is useful for providing "did you mean" functionality.
Example
import (
es "github.com/elastic/go-elasticsearch/v8"
"github.com/sdqri/effdsl/v2"
mq "github.com/sdqri/effdsl/queries/matchquery"
ps "github.com/sdqri/effdsl/suggesters/phrasesuggester"
)
query, err := effdsl.Define(
effdsl.WithSuggest(
ps.PhraseSuggester(
"simple-phrase",
"noble prize",
"title.trigram",
ps.WithLaplaceSmoothing(0.7),
ps.WithDirectGenerator("title.trigram", ps.WithSuggestMode(ps.Always)),
),
),
)
res, err := es.Search(
es.Search.WithBody(strings.NewReader(query)),
)
Parameters
- SuggestName (string) (Required, positional) Name used to identify the suggestion in the response.
- Text (string) (Required, positional) Text to generate phrase suggestions for.
- Field (string) (Required, positional) Field that contains the n‑grams used for suggestions.
- WithGramSize(uint64) (Optional, Functional option) Maximum size of the n‑grams in the field.
- WithRealWordErrorLikelihood(float64) (Optional, Functional option) Likelihood of a term being misspelled even if it exists in the dictionary.
- WithConfidence(float64) (Optional, Functional option) Threshold factor applied to input phrase scores.
- WithMaxErrors(float64) (Optional, Functional option) Maximum percentage or number of terms considered misspellings.
- WithSeparator(string) (Optional, Functional option) Separator used to join tokens in the bigram field.
- WithSize(uint64) (Optional, Functional option) Number of candidate phrases to return.
- WithAnalyzer(string) (Optional, Functional option) Analyzer used to analyze the suggest text.
- WithShardSize(uint64) (Optional, Functional option) Maximum number of suggestions retrieved from each shard.
- WithHighlight(preTag, postTag string) (Optional, Functional option) Adds highlighting to changed tokens.
- WithCollate(queryResult, opts ...WithCollateOption)
(Optional, Functional option) Checks suggestions against a query.
WithParams
andWithPrune
are available options. - WithStupidBackoffSmoothing(discount float64), WithLaplaceSmoothing(alpha float64), WithLinearInterpolationSmoothing(tri, bi, uni float64) (Optional, Functional option) Smoothing models used to balance frequencies.
- WithDirectGenerator(field string, opts ...WithDirectGeneratorOption)
(Optional, Functional option) Adds a direct generator. Options include
WithSuggestMode
,WithDirectGeneratorSize
,WithMaxEdits
,WithPrefixLength
,WithMinWordLength
,WithMaxInspections
,WithMinDocFreq
,WithMaxTermFreq
,WithPreFilter
andWithPostFilter
.
Additional Information
For more details on the phrase suggester and its parameters, refer to the official Elasticsearch documentation on phrase suggesters.