简体   繁体   English

Elasticsearch 返回大于指定范围的结果

[英]Elasticsearch returns results greater than specified in range

I'm use Elasticsearch (Version: 6.8.4) with MongoDB (4.0.3)我将Elasticsearch (版本:6.8.4)与MongoDB (4.0.3)一起使用

I want find all the documents where price between 1 and 800 and sale_date is greater than Date.now(), but I have problem with my query:我想找到价格在 1 到 800 之间且 sale_date 大于 Date.now() 的所有文档,但我的查询有问题:

{
  "query": {
    "bool": {
      "must": [],
      "filter": {
        "term": { 
          "sold_out": false 
        },
        "bool": {
          "should": [
            {
              "range": {
                "sale_date": {
                  "gt": Date.now()
                }
              }
            },
            {
              "range": {
                "price": {
                  "gt": 1,
                  "lte": 800
                }
              }
            }
          ]
        }
      }
    }
  },
  "from": 10,
  "size": 200
}

It's query returns me results with products where some of them have price greater than 800它的查询返回我的结果,其中一些产品的价格大于 800

Price field stored in Elasticsearch as long价格字段存储在Elasticsearch只要

Whant I'm try:我想试试:

  • use from: to: and got the same results使用 from: to: 并得到相同的结果
  • change "should" to "must" in filter and it's returns empty results将过滤器中的“应该”更改为“必须”,它返回空结果
  • remove from query { "range": { "sale_date": { "gt": Date.now(), } } } and it's returns right results!从查询中删除 { "range": { "sale_date": { "gt": Date.now(), } } } 并返回正确的结果!

What I'm doing wrong ?我做错了什么?

First remove the uneccessary nesting ofthe two bool objects, try having all the clauses in the same "bool" field like this.首先删除两个 bool 对象的不必要嵌套,尝试像这样将所有子句放在同一个“bool”字段中。

Remeber that with must a document must have the term you are making a comparison on to be included in the result, with should a match will only improve the score, if the condition is met, so in a certain sense must is like AND and should similar to an OR .记住 with must一个文件必须有你正在做比较的术语才能包含在结果中,如果匹配只会提高分数,如果条件满足,所以在某种意义上must 就像 AND并且应该类似于 OR

In your example changing the filter from should to must made the query return nothing maybe because you don't have any element that has both the date and price you want, or maybe it was the problem was the bool nesting.在您的示例中,将过滤器从 should 更改为 must 使查询不返回任何内容,这可能是因为您没有任何包含您想要的日期和价格的元素,或者可能是布尔嵌套的问题。

Try this:尝试这个:

{
  "query": {
    "bool": {
      "must": [{
        "range": {
          "sale_date": {
            "gt": Date.now()
          }
        }
      },
      {
        "range": {
          "price": {
            "gt": 1,
            "lte": 800
          }
        }
      }],
        "filter": {
        "term": {
          "sold_out": false
        },
      }
    }
  },
  "from": 10,
  "size": 200
}

This will only get the elements with price lte 800 and sale_date gt Date.now().这只会得到价格为 lte 800 和 sale_date gt Date.now() 的元素。

将 Date().now() 更改为“gt”:“now/d”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM