Set optional parameters on the existing query.
public setFieldOption ( string $option, mixed $value ) | ||
$option | string | option name |
$value | mixed | Value of the parameter |
/** * @group unit */ public function testToArray() { $fuzzy = new Fuzzy(); $fuzzy->setField('user', 'Nicolas'); $fuzzy->setFieldOption('boost', 1.0); $expectedArray = array('fuzzy' => array('user' => array('value' => 'Nicolas', 'boost' => 1.0))); $this->assertEquals($expectedArray, $fuzzy->toArray(), 'Deprecated method failed'); $fuzzy = new Fuzzy('user', 'Nicolas'); $expectedArray = array('fuzzy' => array('user' => array('value' => 'Nicolas'))); $this->assertEquals($expectedArray, $fuzzy->toArray()); $fuzzy = new Fuzzy(); $fuzzy->setField('user', 'Nicolas')->setFieldOption('boost', 1.0); $expectedArray = array('fuzzy' => array('user' => array('value' => 'Nicolas', 'boost' => 1.0))); $this->assertEquals($expectedArray, $fuzzy->toArray()); }
/** * Perform a basic search (currently fuzzy) * * @param $term * @param null $user * @param null $sortColumn * @param string $sortDirection * @param int $limit * @return array */ public function basicSearch($term, $user = null, $sortColumn = null, $sortDirection = 'desc', $limit = 50) { $query = new Query(); $query->setFrom(0); $query->setSize($limit); $fuzzy = new Fuzzy(); $fuzzy->setField('value', $term); $fuzzy->setFieldOption('fuzziness', 2); if ($user) { $userId = $user->id; $boolQuery = new BoolQuery(); $matchQuery = new Query\Match('user_id', $userId); $boolQuery->addMust($matchQuery); $boolQuery->addMust($fuzzy); $query->setQuery($boolQuery); } if ($sortColumn) { $query->addSort([$sortColumn => ['order' => $sortDirection]]); } $resultSet = $this->getTypedIndex()->search($query); return $this->getModels($resultSet); }
/** * The fuzzy query generates all possible matching terms that are within * the maximum edit distance specified in fuzziness and then checks the * term dictionary to find out which of those generated terms actually * exist in the index. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/1.4/query-dsl-fuzzy-query.html * * @param string $key * @param string $value * @param array $options * @return Fuzzy */ public function fuzzy($key, $value, $options = []) { $query = new Fuzzy($key, $value); foreach ($options as $option => $value) { $query->setFieldOption($option, $value); } $this->query[] = $this->newQuery($query); return $query; }