Hello, I have an ElasticSearch search model that I’m using for a grid view. I’m also using filters. The filters are configured using “andFilterWhere” in the search model. They are working, but only on exact word matches. E.g. searching “chris” will find; chris, chris jones, chris smith, but not christopher. I’ve tried adding a wildcard character, but that is not working.
Is there a way to use existing Yii functionality to search with a wildcard?
Well I came up with a quick and functional (probably dirty) method. This will run a prefix (E.g. mo will match mouse, moose. etc) filter for each attribute that is being searched. It will have better performance than a full wildcard search.
public function search($params)
{
$this->load($params);
/*
* Loop through each attribute and generate the needed arrays for populated fields
*/
$fields = [];
foreach($this->attributes() as $attribute)
{
if(!empty($this->$attribute))
{
$fields[] = ['prefix' => [$attribute => $this->$attribute]];
}
}
if(!empty($fields))
{
$query = Person::find()->query([
'bool' => [
'must' => $fields
//"wildcard" => [
// "nameLast" => '*z*',
//],
]]);
}
else
{
/*
* Query that will not match anything
*/
$query = Person::find();
$query->andFilterWhere(['id' => 0]);
}
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
return $dataProvider;
}