php - elasticsearch - all terms in any fields
问题描述
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:
- search multiple fields (name, description, manufacturer)
- no fuzzieness: find all the search terms in any of those fields
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.
推荐阅读
- python - list_filter 以选择作为值
- javascript - 我试图通过改变宽度来隐藏数组中的所有元素。在函数内部使用了 setInterval 但它只对最后一个元素正确运行
- prolog - Prolog:查找并放入列表中的重复项
- c++ - 将彩色文本绘制到 c++ win32
- java - 如何在 JAVA 中从 Azure Functions 中引用文件?
- excel - 将不同的工作表合并为具有相同结构的工作表
- c# - 在“离开”文本框后将十六进制字符串格式化为字节数组
- reactjs - HTML to JSX REACT: addEventListener to onClick
- azure - 授予 StorageV2 目录中的用户访问权限(通用 v2)
- r - 将分离的空间对象拆分为不同的文件