1<?php
2
3namespace Elastica\Suggest;
4
5/**
6 * Class Term.
7 *
8 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-term.html
9 */
10class Term extends AbstractSuggest
11{
12    const SORT_SCORE = 'score';
13    const SORT_FREQUENCY = 'frequency';
14
15    const SUGGEST_MODE_MISSING = 'missing';
16    const SUGGEST_MODE_POPULAR = 'popular';
17    const SUGGEST_MODE_ALWAYS = 'always';
18
19    /**
20     * @param string $analyzer
21     *
22     * @return $this
23     */
24    public function setAnalyzer(string $analyzer): Term
25    {
26        return $this->setParam('analyzer', $analyzer);
27    }
28
29    /**
30     * @param string $sort see SORT_* constants for options
31     *
32     * @return $this
33     */
34    public function setSort(string $sort): Term
35    {
36        return $this->setParam('sort', $sort);
37    }
38
39    /**
40     * @param string $mode see SUGGEST_MODE_* constants for options
41     *
42     * @return $this
43     */
44    public function setSuggestMode(string $mode): Term
45    {
46        return $this->setParam('suggest_mode', $mode);
47    }
48
49    /**
50     * If true, suggest terms will be lower cased after text analysis.
51     *
52     * @param bool $lowercase
53     *
54     * @return $this
55     */
56    public function setLowercaseTerms(bool $lowercase = true): Term
57    {
58        return $this->setParam('lowercase_terms', (bool) $lowercase);
59    }
60
61    /**
62     * Set the maximum edit distance candidate suggestions can have in order to be considered as a suggestion.
63     *
64     * @param int $max Either 1 or 2. Any other value will result in an error.
65     *
66     * @return $this
67     */
68    public function setMaxEdits(int $max = 2): Term
69    {
70        return $this->setParam('max_edits', $max);
71    }
72
73    /**
74     * The number of minimum prefix characters that must match in order to be a suggestion candidate.
75     *
76     * @param int $length defaults to 1
77     *
78     * @return $this
79     */
80    public function setPrefixLength(int $length = 1): Term
81    {
82        return $this->setParam('prefix_len', $length);
83    }
84
85    /**
86     * The minimum length a suggest text term must have in order to be included.
87     *
88     * @param int $length defaults to 4
89     *
90     * @return $this
91     */
92    public function setMinWordLength(int $length = 4): Term
93    {
94        return $this->setParam('min_word_length', $length);
95    }
96
97    /**
98     * @param int $max defaults to 5
99     *
100     * @return $this
101     */
102    public function setMaxInspections(int $max = 5): Term
103    {
104        return $this->setParam('max_inspections', $max);
105    }
106
107    /**
108     * Set the minimum number of documents in which a suggestion should appear.
109     *
110     * @param int|float $min Defaults to 0. If the value is greater than 1, it must be a whole number.
111     *
112     * @return $this
113     */
114    public function setMinDocFrequency(float $min = 0): Term
115    {
116        return $this->setParam('min_doc_freq', $min);
117    }
118
119    /**
120     * Set the maximum number of documents in which a suggest text token can exist in order to be included.
121     *
122     * @param float $max
123     *
124     * @return $this
125     */
126    public function setMaxTermFrequency(float $max = 0.01): Term
127    {
128        return $this->setParam('max_term_freq', $max);
129    }
130
131    /**
132     * Which string distance implementation to use for comparing how similar suggested terms are.
133     * Five possible values can be specified:.
134     *
135     * - internal
136     * - damerau_levenshtein
137     * - levenshtein
138     * - jaro_winkler
139     * - ngram
140     *
141     * @param string $distanceAlgorithm
142     *
143     * @return $this
144     */
145    public function setStringDistanceAlgorithm(string $distanceAlgorithm): Term
146    {
147        return $this->setParam('string_distance', $distanceAlgorithm);
148    }
149}
150