i am trying to evolve my mongo search engine into something more advanced. I know why the below code is giving me the results. I struggle with how to expand the capabilities of the search.
I would like that result #5 is on top because it found both search parts in multiple or single field.
My current thought is to do the search query for each search part then merge the results and add an incremental for each occurence and order by that number. I rather not do multiple executions at the same time but atleast you understand where my mind is on trying to solve this.
Simply changing $builder->addOr(...$searchQueries);
to $builder->addAnd(...$searchQueries);
will result zero results because all fields will match.
$search = 'small table';
$fields = ['name', 'small_description', 'long_description'];
foreach ($fields as $field) {
$searchValues = [];
foreach (explode(' ', $search) as $searchPart) {
$searchValues[] = new Regex(preg_quote($searchPart), 'i');
}
$searchQueries[] = $builder->expr()->field($field)->in($searchValues);
}
$builder->addOr(...$searchQueries);
/**
* result 5 products
*
* 1. comfortable ( because it matches table with the regex )
* 2. small ( because one part of the search matches )
* 3. table ( because one part of the search matches )
* 4. tablespoon ( because it matches table with the regex )
* 5. small table ( matches but is below in the order while this should be on top )
*/