elasticsearch - ElasticSearch 5-迁移 “missing”过滤器

标签 elasticsearch

在ElasticSearch 1和2.x中,我们当前要迁移到5的应用程序中使用了“缺失”过滤器。

“缺少”过滤器用于创建如下选择:
返回缺少价格或价格> 100的所有结果。

在ES5中,缺少的过滤器已不存在,并且文档说可以用“must_not”和“exists”的组合替换它,但是在我的情况下,我正在编写“或”查询,没有“should_not” ”子句。

摘自https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html上的文档:

missing query has been removed because it can be advantageously replaced by an exists query inside a must_not clause as follows:


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);

最佳答案

我猜should查询会做必要的

{
"query": {
  "bool": {
     "should": [
        {
           "bool": {
              "must_not": {
                 "exists": {
                    "field": "price"
                 }
              }
           }
        },
        {
            "range": {
               "price": {
                  "from": 100
               }
            }
        }
     ]
    }
   }
}

关于elasticsearch - ElasticSearch 5-迁移 “missing”过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44235769/

相关文章:

java - ElasticsearchOperations 查询不返回完全匹配

sorting - ElasticSearch的地理距离排序中使用的 `unit`设置是什么?

elasticsearch - 如何使用更新 API 更新 Elastic search 中的子文档?

elasticsearch - 嵌套查询中数组上的聚合

elasticsearch - 使用Java API获取 child 的 parent ID

elasticsearch - 如何知道 elasticsearch 集群上何时发生数据丢失

elasticsearch - 删除包含特定字符串的日志消息

php - elasticsearch地理边界框不返回结果

indexing - 使用 python 请求模块进行 Elasticsearch 批量/批量索引

javascript - Elasticsearch javascript 数组中的搜索字段