For a search function in an onlineshop, I'm using ongr's elasticsearchDSL (git / docs).
Basic setup to search product names:
$boolQuery = new BoolQuery();
$boolQuery->addParameter('minimum_should_match', 1);
$wcQuery = new WildcardQuery('name', "*$_term*");
$boolQuery->add($wcQuery, BoolQuery::MUST);
This has an annoying behavior:
The query "notebook" finds all products with that term. But the query "note book" finds nothing at all.
Maybe a wildcard query isn't the best practice anyways.
What I need:
What's the best way to do that?
I've tried MultiMatchQuery
with types best_fields
and phrase_prefix
and SHOULD/MUST
combination, but either the result has too many irrelevant results or none at all.
Thanks for your time.
figured out a solution:
$boolQuery = new BoolQuery();
$boolQuery->addParameter('minimum_should_match', '100%');
// search by manufacurer number
$wcQuery = new QueryStringQuery("$term", [
'fields' => [ 'manufacturerNumber' ]
]);
$boolQuery->add($wcQuery, BoolQuery::SHOULD);
// split query into single terms
$term = explode(" ", trim(preg_replace("@[^a-z0-9äöüß\-]@", " ", strtolower($term))));
// find all terms in any fields
foreach($term as $_term){
$wcQuery = new QueryStringQuery("*$_term*", [
'fields' => [ 'name', 'shortDescription', 'manufacturerName' ]
]);
$boolQuery->add($wcQuery, BoolQuery::SHOULD);
}
Leading wildcard isn't ideal I guess, but it works just fine like this. No performance issues at all.