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