【发布时间】:2017-05-29 14:57:49
【问题描述】:
在 ElasticSearch 1 和 2.x 中,我们正在迁移到 5 的应用中使用了“缺失”过滤器。
“缺失”过滤器用于创建如下选择: 返回 price 缺失或 price > 100 的所有结果。
在 ES5 中,缺少的过滤器不再存在,文档说它可以用“must_not”和“exists”的组合替换,但就我而言,我正在编写一个“or”查询并且有没有“should_not”子句。
摘自https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html 的文档:
缺失的查询已被删除,因为它可以方便地替换为 must_not 子句中的存在查询,如下所示:
GET /_search
{
"query": {
"bool": {
"must_not": {
"exists": {
"field": "user"
}
}
}
}
}
你会如何在 ES5 中迁移它?
谢谢!
更新:这是用于构建查询的 PHP / Elastica 代码。不幸的是,我没有 JSON 格式的查询,因为此时应用程序没有运行。这个想法很简单:我想要所有“价格”缺失或大于 100 的结果。
$and = new \Elastica\Filter\BoolFilter();
$and->addMust(new \Elastica\Filter\Term(array('highlights_ids' => $highlight->getId())));
$or = new \Elastica\Filter\BoolOr();
$or->addFilter(new \Elastica\Filter\Missing('price')); // if price is missing => show always
$or->addFilter(new \Elastica\Filter\Range("price", array('from' => $minPrice)));
$and->addMust($or);
$filter = new \Elastica\Query\BoolQuery(null, $and);
$query = new \Elastica\Query($filter);
$query->setFields(array()); // No fields, we get them from the db
$query->setSize($max);
$result = $this->tripProvider->search($query);
【问题讨论】:
-
您能分享一下您目前正在使用的查询并希望在 5 中迁移。
-
我没有 JSON 格式的查询(应用程序已损坏 atm),但我添加了 PHP 代码。
标签: elasticsearch