php - Elasticsearch中的条件查询

标签 php laravel elasticsearch

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

查询:

我有带有价格搜寻器搜索的产品列表页面。如果用户使用该搜索,我想检查产品是否有销售,则应考虑销售价格,否则应考虑销售价格。 (检查销售:我正在按sale_start和sale_end之间的当前日期检查销售。)

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"]],
                        ],
                    ],
                ],
            ],
        ];

最佳答案

试试这个查询:

{
  "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语句。

关于php - Elasticsearch中的条件查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37184645/

相关文章:

php - 将字符串转换为 SHA1 和 base64

php - 如何使用 Facebook Graph API 在照片中标记用户?

elasticsearch - 聚合中的弹性同义词用法

elasticsearch - SpringData 和 Elasticsearch - 使用 ignoreFields 时出现 java.lang.StackOverflowError

php - 为什么这不会解码为 html 实体?

php - fatal error : Uncaught PDOException: get wrong values

mysql - 设置laravel Eloquent关系报错

php - Laravel 5 显示 ErrorException file_put_contents 无法打开流 : No such file or directory

php - 拉维尔 "undefined method Illuminate\Database\Query\Builder::attach()"

elasticsearch - Elasticsearch 中的 BM25 相似度调整