Set fuzziness.
public setFuzziness ( float | string $fuzziness ) | ||
$fuzziness | float | string |
/** * @group functional */ public function testFuzzyWithOptions1() { // Here Elasticsearch will not accept mispells // on the first 6 letters. $multiMatch = new MultiMatch(); $multiMatch->setQuery('Tritsan'); // Misspell on purpose $multiMatch->setFields(array('full_name', 'name')); $multiMatch->setFuzziness(2); $multiMatch->setPrefixLength(6); $resultSet = $this->_getResults($multiMatch); $this->assertEquals(0, $resultSet->count()); }
/** * Find all documents where the value is matched in the fields. The type option * allows you to specify the type of match, can be best_fields, most_fields, * cross_fields, phrase, phrase_prefix. * * best_fields finds documents which match any field, but uses the _score * from the best field. * * most_fields finds documents which match any field and combines the _score * from each field. * * cross_fields treats fields with the same analyzer as though they were * one big field. Looks for each word in any field. * * phrase runs a match_phrase query on each field and combines the _score * from each field. * * phrase_prefix runs a match_phrase_prefix query on each field and combines * the _score from each field. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html * * @param array $fields The fields to search in * @param string $query The string to search for * @param string $type The match type * @param bool $fuzzy Set whether the match should be fuzzy * @param float $tieBreaker Can be between 0.0 and 1.0 * @param string $operator Can be 'and' or 'or' * @return Query */ public function multiMatch(array $fields, $query, $type = 'phrase', $fuzzy = false, $tieBreaker = 0.0, $operator = 'and') { $match = new MultiMatch(); $match->setFields($fields); $match->setQuery($query); $match->setType($type); if ($fuzzy) { $match->setFuzziness('AUTO'); } if ($type == 'best_fields') { $match->setTieBreaker($tieBreaker); } if ($type == 'cross_fields') { $match->setOperator($operator); } $query = $this->newQuery($match); $this->query[] = $query; return $query; }
/** * @param Request $request * @return JsonResponse */ public function autoCompleteAction(Request $request) { $q = filter_var($request->get('q'), FILTER_SANITIZE_STRING); $search = $this->get('fos_elastica.index.search.journal'); $notCollectJournals = []; if ($request->query->has('notCollectJournals')) { $notCollectJournalsParam = $request->query->get('notCollectJournals'); if (!empty($notCollectJournalsParam) && is_array($notCollectJournalsParam)) { $notCollectJournals = $notCollectJournalsParam; } } $searchQuery = new Query('_all'); $boolQuery = new Query\BoolQuery(); $fieldQuery = new Query\MultiMatch(); $fieldQuery->setFields(['title']); $fieldQuery->setQuery(strtoupper($q)); $fieldQuery->setFuzziness(0.7); $boolQuery->addMust($fieldQuery); $searchQuery->setQuery($boolQuery); $searchQuery->setSize(10); $results = $search->search($searchQuery); $data = []; foreach ($results as $result) { if (!in_array($result->getId(), $notCollectJournals)) { $data[] = ['id' => $result->getId(), 'text' => $result->getData()['title']]; } } return new JsonResponse($data); }