elasticsearch - elasticsearch如何查询具有数据范围的两个不同字段

标签 elasticsearch elasticsearch-plugin

我正在使用elasticsearch,我在doc中有以下字段。

sale_start

sale_end

销售价格

selling_price

我想查询的数据应该是这样的:

如果“sale_start”日期和“sale_end”日期在当前日期之间

那么它应该将价格范围条件与sale_price相匹配

如果“sale_start”日期和“sale_end”日期不在当前日期之间

那么它应该将价格范围条件与Selling_price相匹配

我进行了很多搜索,但是找不到写这种条件的方法。我也是Elasticsearch的新手。提前致谢

最佳答案

如果我正确地获得了您的要求,可以这样表示:

  • 外部bool/should包含两种主要情况:
  • 当前日期now在[sale_startsale_end]区间内,而sale_price在价格区间之间。请注意,我已经任意选择了间隔[1,1000],但是您可以自由更改。
  • 当前日期nowsale_start之前或之后,并且sale_end在[1,1000]价格范围内。

  • 查询:
    {
      "query": {
        "bool": {
          "should": [
            {
              "bool": {
                "must": [
                  {
                    "range": {
                      "sale_start": {
                        "lt": "now"
                      }
                    }
                  },
                  {
                    "range": {
                      "sale_end": {
                        "gt": "now"
                      }
                    }
                  },
                  {
                    "range": {
                      "sale_price": {
                        "gt": 1,
                        "lt": 1000
                      }
                    }
                  }
                ]
              }
            },
            {
              "bool": {
                "should": [
                  {
                    "range": {
                      "sale_start": {
                        "gt": "now"
                      }
                    }
                  },
                  {
                    "range": {
                      "sale_end": {
                        "lt": "now"
                      }
                    }
                  }
                ],
                "must": [
                  {
                    "range": {
                      "selling_price": {
                        "gt": 1,
                        "lt": 1000
                      }
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
    

    关于elasticsearch - elasticsearch如何查询具有数据范围的两个不同字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37153103/

    相关文章:

    monitoring - 如何使用nagios监控elasticsearch

    Elasticsearch GROUP BY 中不同的计数

    elasticsearch - Logstash 无法连接安全 (ssl) Elasticsearch 集群

    debugging - 如何可视化Elasticsearch受检查询历史

    elasticsearch - 更新已经使用logstash和filebeat上载的elasticsearch数据

    elasticsearch - 更新elasticsearch模板而不覆盖它

    azure - Elasticsearch 快照到 Azure Blob 存储 - 找不到帐户的 azure 客户端

    elasticsearch - 用于 Elasticsearch 的远程 GUI 客户端

    elasticsearch - 将脚本输出输出到Elasticsearch的最佳方法?

    elasticsearch - ElasticSearch mapper-annotated-text插件:仅搜索带注释的 token