【问题标题】:Conditional query in elasticsearchelasticsearch中的条件查询
【发布时间】:2016-05-12 19:53:27
【问题描述】:

我在 mysql 中有一个条件查询,我想在 elasticsearch 查询中转换它:

查询

我有带有价格范围搜索的产品列表页面。如果用户使用该搜索,我想检查产品是否有销售,那么它应该考虑销售价格或者销售价格。 (检查销售:我正在检查销售开始和销售结束日期之间的当前日期。)

MySql 查询:

SELECT *
FROM `product_sku`
WHERE 
((sale_start < '2016-05-12 15:23:53' AND sale_end > '2016-05-12 15:23:53' AND sale_price between 600 AND 1800)
 OR (sale_end < '2016-05-12 15:23:53' AND selling_price between 600 AND 1800) 
)

Elasticsearch 查询:

$params = [
            'index' => 'index',
            'type' => 'product-list',
            'body' => [
                "query" => [
                    "filtered" => [
                        "query" => [
                            "match_all" => [],
                        ],
                        'query' => $query,

                        "filter" => [
                            "nested" => [
                                "path" => "default_product_low_price_with_seller",
                                "filter" => [
                                    "bool" => [
                                        "should" => [
                                            [
                                                "range" => [
                                                    "default_product_low_price_with_seller.sale_price" => [
                                                        "gte" => $_GET['filter']['price']['from'],
                                                        "lte" => $_GET['filter']['price']['to'],
                                                    ],
                                                ],
                                            ],
                                            [
                                                "range" => [
                                                    "default_product_low_price_with_seller.sale_end" => [
                                                        "gte" => $now,
                                                    ],
                                                ],
                                            ],
                                            [
                                                "range" => [
                                                    "default_product_low_price_with_seller.sale_start" => [
                                                        "lte" => $now,
                                                    ],
                                                ],
                                            ],

                                        ],
                                    ],
                                ],
                                "filter" => [
                                    "bool" => [
                                        "should" => [
                                            [
                                                "range" => [
                                                    "default_product_low_price_with_seller.selling_price" => [
                                                        "gte" => $_GET['filter']['price']['from'],
                                                        "lte" => $_GET['filter']['price']['to'],
                                                    ],
                                                ],
                                            ],
                                            [
                                                "range" => [
                                                    "default_product_low_price_with_seller.sale_end" => [
                                                        "lte" => $now,
                                                    ],
                                                ],
                                            ],
                                            [
                                                "range" => [
                                                    "default_product_low_price_with_seller.sale_start" => [

                                                        "gte" => $now,
                                                    ],
                                                ],
                                            ],

                                        ],
                                    ],
                                ],
                            ],
                        ],
                    ],
                ],
                "aggs" => [
                    "brand_name" => ["terms" => ["field" => "brand_name"]],
                    "category_with_in_title" => [
                        "nested" => [
                            "path" => "category_with_in_title.parent_cat",
                        ],
                        "aggs" => [

                            "category_with_in_title.title" => ["terms" => ["field" => "category_with_in_title.parent_cat.title"]],
                        ],
                    ],
                ],
            ],
        ];

【问题讨论】:

  • 你的mysql查询在哪里?
  • 我已经更新了我的问题......

标签: php laravel elasticsearch


【解决方案1】:

试试这个查询:

{
  "query": {
    "nested": {
      "path": "default_product_low_price_with_seller",
      "query": {
        "bool": {
          "should": [
            {
              "bool": {
                "must": [
                  {
                    "range": {
                      "default_product_low_price_with_seller.sale_price": {
                        "gte": 100,
                        "lte": 300
                      }
                    }
                  },
                  {
                    "range": {
                      "default_product_low_price_with_seller.sale_start": {
                        "lte": "2016-05-05"
                      }
                    }
                  },
                  {
                    "range": {
                      "default_product_low_price_with_seller.sale_end": {
                        "gte": "2016-05-05"
                      }
                    }
                  }
                ]
              }
            },
            {
              "bool": {
                "must": [
                  {
                    "range": {
                      "default_product_low_price_with_seller.selling_price": {
                        "gte": 100,
                        "lte": 300
                      }
                    }
                  },
                  {
                    "range": {
                      "default_product_low_price_with_seller.sale_end": {
                        "lte": "2017-05-05"
                      }
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

因此,这两个条件是全局 should 列表的分支,每个分支下都有一系列 must 语句,位于新的 bool 查询下。

【讨论】:

    猜你喜欢
    • 2016-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-09
    相关资源
    最近更新 更多