Skip to content

Commit

Permalink
Refactor completion suggester to accept regex/prefix and options
Browse files Browse the repository at this point in the history
The completion suggester now supports regex/prefix and their options.

The FuzzyCompletionSuggester is removed as it can be completely
implemented with CompletionSuggester now.
  • Loading branch information
olivere committed Dec 27, 2017
1 parent 7a065d5 commit 36c8ae5
Show file tree
Hide file tree
Showing 8 changed files with 282 additions and 238 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ services:
- docker
before_install:
- sudo sysctl -w vm.max_map_count=262144
- docker run -d --rm -p 9200:9200 -e "http.host=0.0.0.0" -e "transport.host=127.0.0.1" -e "bootstrap.memory_lock=true" -e "ES_JAVA_OPTS=-Xms1g -Xmx1g" docker.elastic.co/elasticsearch/elasticsearch:6.0.0 elasticsearch -Expack.security.enabled=false -Enetwork.host=_local_,_site_ -Enetwork.publish_host=_local_
- docker run -d --rm -p 9200:9200 -e "http.host=0.0.0.0" -e "transport.host=127.0.0.1" -e "bootstrap.memory_lock=true" -e "ES_JAVA_OPTS=-Xms1g -Xmx1g" docker.elastic.co/elasticsearch/elasticsearch:6.1.1 elasticsearch -Expack.security.enabled=false -Enetwork.host=_local_,_site_ -Enetwork.publish_host=_local_
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ Marcy Buccellato [@marcybuccellato](https://github.com/marcybuccellato)
Mark Costello [@mcos](https://github.com/mcos)
Martin Häger [@protomouse](https://github.com/protomouse)
Medhi Bechina [@mdzor](https://github.com/mdzor)
mnpritula [@mnpritula](https://github.com/mnpritula)
mosa [@mosasiru](https://github.com/mosasiru)
naimulhaider [@naimulhaider](https://github.com/naimulhaider)
Naoya Yoshizawa [@azihsoyn](https://github.com/azihsoyn)
Expand Down
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

const (
// Version is the current version of Elastic.
Version = "6.0.0"
Version = "6.1.0"

// DefaultURL is the default endpoint of Elasticsearch on the local machine.
// It is used e.g. when initializing a new Client without a specific URL.
Expand Down
2 changes: 1 addition & 1 deletion run-es.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/sh
VERSION=${VERSION:=6.0.0}
VERSION=${VERSION:=6.1.1}
docker run --rm -p 9200:9200 -e "http.host=0.0.0.0" -e "transport.host=127.0.0.1" -e "bootstrap.memory_lock=true" -e "ES_JAVA_OPTS=-Xms1g -Xmx1g" docker.elastic.co/elasticsearch/elasticsearch:$VERSION elasticsearch -Expack.security.enabled=false -Enetwork.host=_local_,_site_ -Enetwork.publish_host=_local_
226 changes: 220 additions & 6 deletions suggester_completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,30 @@ package elastic
import "errors"

// CompletionSuggester is a fast suggester for e.g. type-ahead completion.
// See https://www.elastic.co/guide/en/elasticsearch/reference/6.0/search-suggesters-completion.html
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-suggesters-completion.html
// for more details.
type CompletionSuggester struct {
Suggester
name string
text string
prefix string
regex string
field string
analyzer string
size *int
shardSize *int
contextQueries []SuggesterContextQuery
payload interface{}

fuzzyOptions *FuzzyCompletionSuggesterOptions
regexOptions *RegexCompletionSuggesterOptions
skipDuplicates *bool
}

// Creates a new completion suggester.
func NewCompletionSuggester(name string) *CompletionSuggester {
return &CompletionSuggester{
name: name,
contextQueries: make([]SuggesterContextQuery, 0),
name: name,
}
}

Expand All @@ -37,6 +43,57 @@ func (q *CompletionSuggester) Text(text string) *CompletionSuggester {
return q
}

func (q *CompletionSuggester) Prefix(prefix string) *CompletionSuggester {
q.prefix = prefix
return q
}

func (q *CompletionSuggester) PrefixWithEditDistance(prefix string, editDistance interface{}) *CompletionSuggester {
q.prefix = prefix
q.fuzzyOptions = NewFuzzyCompletionSuggesterOptions().EditDistance(editDistance)
return q
}

func (q *CompletionSuggester) PrefixWithOptions(prefix string, options *FuzzyCompletionSuggesterOptions) *CompletionSuggester {
q.prefix = prefix
q.fuzzyOptions = options
return q
}

func (q *CompletionSuggester) FuzzyOptions(options *FuzzyCompletionSuggesterOptions) *CompletionSuggester {
q.fuzzyOptions = options
return q
}

func (q *CompletionSuggester) Fuzziness(fuzziness interface{}) *CompletionSuggester {
if q.fuzzyOptions == nil {
q.fuzzyOptions = NewFuzzyCompletionSuggesterOptions()
}
q.fuzzyOptions = q.fuzzyOptions.EditDistance(fuzziness)
return q
}

func (q *CompletionSuggester) Regex(regex string) *CompletionSuggester {
q.regex = regex
return q
}

func (q *CompletionSuggester) RegexWithOptions(regex string, options *RegexCompletionSuggesterOptions) *CompletionSuggester {
q.regex = regex
q.regexOptions = options
return q
}

func (q *CompletionSuggester) RegexOptions(options *RegexCompletionSuggesterOptions) *CompletionSuggester {
q.regexOptions = options
return q
}

func (q *CompletionSuggester) SkipDuplicates(skipDuplicates bool) *CompletionSuggester {
q.skipDuplicates = &skipDuplicates
return q
}

func (q *CompletionSuggester) Field(field string) *CompletionSuggester {
q.field = field
return q
Expand Down Expand Up @@ -72,17 +129,25 @@ func (q *CompletionSuggester) ContextQueries(queries ...SuggesterContextQuery) *
// We got into trouble when using plain maps because the text element
// needs to go before the completion element.
type completionSuggesterRequest struct {
Text string `json:"text"`
Completion interface{} `json:"completion"`
Text string `json:"text,omitempty"`
Prefix string `json:"prefix,omitempty"`
Regex string `json:"regex,omitempty"`
Completion interface{} `json:"completion,omitempty"`
}

// Creates the source for the completion suggester.
// Source creates the JSON data for the completion suggester.
func (q *CompletionSuggester) Source(includeName bool) (interface{}, error) {
cs := &completionSuggesterRequest{}

if q.text != "" {
cs.Text = q.text
}
if q.prefix != "" {
cs.Prefix = q.prefix
}
if q.regex != "" {
cs.Regex = q.regex
}

suggester := make(map[string]interface{})
cs.Completion = suggester
Expand Down Expand Up @@ -126,6 +191,28 @@ func (q *CompletionSuggester) Source(includeName bool) (interface{}, error) {
suggester["contexts"] = ctxq
}

// Fuzzy options
if q.fuzzyOptions != nil {
src, err := q.fuzzyOptions.Source()
if err != nil {
return nil, err
}
suggester["fuzzy"] = src
}

// Regex options
if q.regexOptions != nil {
src, err := q.regexOptions.Source()
if err != nil {
return nil, err
}
suggester["regex"] = src
}

if q.skipDuplicates != nil {
suggester["skip_duplicates"] = *q.skipDuplicates
}

// TODO(oe) Add completion-suggester specific parameters here

if !includeName {
Expand All @@ -136,3 +223,130 @@ func (q *CompletionSuggester) Source(includeName bool) (interface{}, error) {
source[q.name] = cs
return source, nil
}

// -- Fuzzy options --

// FuzzyCompletionSuggesterOptions represents the options for fuzzy completion suggester.
type FuzzyCompletionSuggesterOptions struct {
editDistance interface{}
transpositions *bool
minLength *int
prefixLength *int
unicodeAware *bool
maxDeterminizedStates *int
}

// NewFuzzyCompletionSuggesterOptions initializes a new FuzzyCompletionSuggesterOptions instance.
func NewFuzzyCompletionSuggesterOptions() *FuzzyCompletionSuggesterOptions {
return &FuzzyCompletionSuggesterOptions{}
}

// EditDistance specifies the maximum number of edits, e.g. a number like "1" or "2"
// or a string like "0..2" or ">5". See https://www.elastic.co/guide/en/elasticsearch/reference/5.6/common-options.html#fuzziness
// for details.
func (o *FuzzyCompletionSuggesterOptions) EditDistance(editDistance interface{}) *FuzzyCompletionSuggesterOptions {
o.editDistance = editDistance
return o
}

// Transpositions, if set to true, are counted as one change instead of two (defaults to true).
func (o *FuzzyCompletionSuggesterOptions) Transpositions(transpositions bool) *FuzzyCompletionSuggesterOptions {
o.transpositions = &transpositions
return o
}

// MinLength represents the minimum length of the input before fuzzy suggestions are returned (defaults to 3).
func (o *FuzzyCompletionSuggesterOptions) MinLength(minLength int) *FuzzyCompletionSuggesterOptions {
o.minLength = &minLength
return o
}

// PrefixLength represents the minimum length of the input, which is not checked for
// fuzzy alternatives (defaults to 1).
func (o *FuzzyCompletionSuggesterOptions) PrefixLength(prefixLength int) *FuzzyCompletionSuggesterOptions {
o.prefixLength = &prefixLength
return o
}

// UnicodeAware, if true, all measurements (like fuzzy edit distance, transpositions, and lengths)
// are measured in Unicode code points instead of in bytes. This is slightly slower than
// raw bytes, so it is set to false by default.
func (o *FuzzyCompletionSuggesterOptions) UnicodeAware(unicodeAware bool) *FuzzyCompletionSuggesterOptions {
o.unicodeAware = &unicodeAware
return o
}

// MaxDeterminizedStates is currently undocumented in Elasticsearch. It represents
// the maximum automaton states allowed for fuzzy expansion.
func (o *FuzzyCompletionSuggesterOptions) MaxDeterminizedStates(max int) *FuzzyCompletionSuggesterOptions {
o.maxDeterminizedStates = &max
return o
}

// Source creates the JSON data.
func (o *FuzzyCompletionSuggesterOptions) Source() (interface{}, error) {
out := make(map[string]interface{})

if o.editDistance != nil {
out["fuzziness"] = o.editDistance
}
if o.transpositions != nil {
out["transpositions"] = *o.transpositions
}
if o.minLength != nil {
out["min_length"] = *o.minLength
}
if o.prefixLength != nil {
out["prefix_length"] = *o.prefixLength
}
if o.unicodeAware != nil {
out["unicode_aware"] = *o.unicodeAware
}
if o.maxDeterminizedStates != nil {
out["max_determinized_states"] = *o.maxDeterminizedStates
}

return out, nil
}

// -- Regex options --

// RegexCompletionSuggesterOptions represents the options for regex completion suggester.
type RegexCompletionSuggesterOptions struct {
flags interface{} // string or int
maxDeterminizedStates *int
}

// NewRegexCompletionSuggesterOptions initializes a new RegexCompletionSuggesterOptions instance.
func NewRegexCompletionSuggesterOptions() *RegexCompletionSuggesterOptions {
return &RegexCompletionSuggesterOptions{}
}

// Flags represents internal regex flags. See https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-suggesters-completion.html#regex
// for details.
func (o *RegexCompletionSuggesterOptions) Flags(flags interface{}) *RegexCompletionSuggesterOptions {
o.flags = flags
return o
}

// MaxDeterminizedStates represents the maximum automaton states allowed for regex expansion.
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-suggesters-completion.html#regex
// for details.
func (o *RegexCompletionSuggesterOptions) MaxDeterminizedStates(max int) *RegexCompletionSuggesterOptions {
o.maxDeterminizedStates = &max
return o
}

// Source creates the JSON data.
func (o *RegexCompletionSuggesterOptions) Source() (interface{}, error) {
out := make(map[string]interface{})

if o.flags != nil {
out["flags"] = o.flags
}
if o.maxDeterminizedStates != nil {
out["max_determinized_states"] = *o.maxDeterminizedStates
}

return out, nil
}
Loading

0 comments on commit 36c8ae5

Please sign in to comment.