1<?php
2
3namespace Elastica\Suggest\CandidateGenerator;
4
5/**
6 * Class DirectGenerator.
7 *
8 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-phrase.html#_direct_generators
9 */
10class DirectGenerator extends AbstractCandidateGenerator
11{
12    const SUGGEST_MODE_MISSING = 'missing';
13    const SUGGEST_MODE_POPULAR = 'popular';
14    const SUGGEST_MODE_ALWAYS = 'always';
15
16    const DEFAULT_SIZE = 5;
17    const DEFAULT_SUGGEST_MODE = self::SUGGEST_MODE_MISSING;
18    const DEFAULT_MAX_EDITS = 2;
19    const DEFAULT_PREFIX_LENGTH = 1;
20    const DEFAULT_MIN_WORD_LENGTH = 4;
21    const DEFAULT_MAX_INSPECTIONS = 5;
22    const DEFAULT_MIN_DOC_FREQ = 0.0;
23    const DEFAULT_MAX_TERM_FREQ = 0.01;
24
25    /**
26     * @param string $field
27     */
28    public function __construct(string $field)
29    {
30        $this->setField($field);
31    }
32
33    /**
34     * Set the field name from which to fetch candidate suggestions.
35     *
36     * @param string $field
37     *
38     * @return $this
39     */
40    public function setField(string $field)
41    {
42        return $this->setParam('field', $field);
43    }
44
45    /**
46     * Set the maximum corrections to be returned per suggest text token.
47     *
48     * @param int $size
49     *
50     * @return $this
51     */
52    public function setSize(int $size)
53    {
54        return $this->setParam('size', $size);
55    }
56
57    /**
58     * @param string $mode see SUGGEST_MODE_* constants for options
59     *
60     * @return $this
61     */
62    public function setSuggestMode(string $mode)
63    {
64        return $this->setParam('suggest_mode', $mode);
65    }
66
67    /**
68     * @param int $max can only be a value between 1 and 2. Defaults to 2.
69     *
70     * @return $this
71     */
72    public function setMaxEdits(int $max)
73    {
74        return $this->setParam('max_edits', $max);
75    }
76
77    /**
78     * @param int $length defaults to 1
79     *
80     * @return $this
81     */
82    public function setPrefixLength(int $length)
83    {
84        return $this->setParam('prefix_length', $length);
85    }
86
87    /**
88     * @param int $min defaults to 4
89     *
90     * @return $this
91     */
92    public function setMinWordLength(int $min)
93    {
94        return $this->setParam('min_word_length', $min);
95    }
96
97    /**
98     * @param int $max
99     *
100     * @return $this
101     */
102    public function setMaxInspections(int $max)
103    {
104        return $this->setParam('max_inspections', $max);
105    }
106
107    /**
108     * @param float $min
109     *
110     * @return $this
111     */
112    public function setMinDocFrequency(float $min)
113    {
114        return $this->setParam('min_doc_freq', $min);
115    }
116
117    /**
118     * @param float $max
119     *
120     * @return $this
121     */
122    public function setMaxTermFrequency(float $max)
123    {
124        return $this->setParam('max_term_freq', $max);
125    }
126
127    /**
128     * Set an analyzer to be applied to the original token prior to candidate generation.
129     *
130     * @param string $pre an analyzer
131     *
132     * @return $this
133     */
134    public function setPreFilter(string $pre)
135    {
136        return $this->setParam('pre_filter', $pre);
137    }
138
139    /**
140     * Set an analyzer to be applied to generated tokens before they are passed to the phrase scorer.
141     *
142     * @param string $post
143     *
144     * @return $this
145     */
146    public function setPostFilter(string $post)
147    {
148        return $this->setParam('post_filter', $post);
149    }
150}
151