1 | <?php |
||
23 | class FuzzySearch extends AbstractSingleValue |
||
24 | { |
||
25 | /** |
||
26 | * @var array Fuzzy query parameters. |
||
27 | */ |
||
28 | private $parameters = []; |
||
29 | |||
30 | /** |
||
31 | * {@inheritdoc} |
||
32 | */ |
||
33 | public function modifySearch(Search $search, FilterState $state = null, SearchRequest $request = null) |
||
34 | { |
||
35 | if ($state && $state->isActive()) { |
||
36 | if (strpos($this->getField(), ',') !== false) { |
||
37 | $subQuery = new BoolQuery(); |
||
38 | foreach (explode(',', $this->getField()) as $field) { |
||
39 | $subQuery->add(new FuzzyQuery($field, $state->getValue(), $this->getParameters()), 'should'); |
||
40 | } |
||
41 | $search->addQuery($subQuery, 'must'); |
||
42 | } else { |
||
43 | $search->addQuery( |
||
44 | new FuzzyQuery($this->getField(), $state->getValue(), $this->getParameters()), |
||
45 | 'must' |
||
46 | ); |
||
47 | } |
||
48 | } |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @return array |
||
53 | */ |
||
54 | public function getParameters() |
||
58 | |||
59 | /** |
||
60 | * The maximum edit distance. |
||
61 | * |
||
62 | * @param string|int|float $fuzziness |
||
63 | */ |
||
64 | public function setFuzziness($fuzziness) |
||
68 | |||
69 | /** |
||
70 | * The number of initial characters which will not be “fuzzified”. |
||
71 | * |
||
72 | * @param int $prefixLength |
||
73 | */ |
||
74 | public function setPrefixLength($prefixLength) |
||
78 | |||
79 | /** |
||
80 | * The maximum number of terms that the fuzzy query will expand to. |
||
81 | * |
||
82 | * @param int $maxExpansions |
||
83 | */ |
||
84 | public function setMaxExpansions($maxExpansions) |
||
88 | } |
||
89 |