弹性搜索中的条件查询 [英] Conditional query in elasticsearch

查看:92
本文介绍了弹性搜索中的条件查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



查询

$ b $我有一个条件查询在mysql中我想在弹性搜索查询中转换: b

我有产品列表页面与价格追踪者搜索。如果用户使用该搜索,我想检查产品是否有销售,则应考虑销售价格其他销售价格。 (支票销售:我正在根据sales_start和sale_end日期之间的当前日期查看销售。)



MySql查询:

  SELECT * 
FROM`product_sku`
WHERE
((sale_start& -05-12 15:23:53'AND sale_end>'Glos12 15:23:53'AND sale_price在600和1800之间)
OR(sale_end<'14,12-12 15: 23:53'和sell_price在600和1800之间)

弹性搜索查询:

  $ 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=> $现在,
],
],
],
[
range=> [
default_product_low_price_with_seller.sale_start=> [
lte=> $现在,
],
],
],

],
],
],
=> [
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=> $现在,
],
],
],
[
range=> [
default_product_low_price_with_seller.sale_start=> [

gte=> $现在,
],
],
],

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


解决方案

尝试此查询:

  {
查询:{
嵌套:{
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
}

},
{
范围:{
default_product_low_price_with_seller.sale_start:{
lte:inserts-05
}
}
},
{
范围: {
default_product_low_price_with_seller.sale_end:{
gte:inserts-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
}
}
}
]
}
}
]
}
}
}
}
}

所以,这两个条件是全局应该列表的分支,在每个分支下有一系列必须语句在一个新的 bool 查询。


I have one conditional query in mysql i want to convert it in elasticsearch query :

Query :

I have product list page with price ranger search. If user use that search I want to check if product has sale then it should consider sale price else selling price. (check sale : i am checking sale by current date between sale_start and sale_end date.)

MySql Query :

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 Query :

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

解决方案

Try this query:

{
  "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"
                      }
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

So, the two conditions are branches of a global should list and under each branch there is a series of must statements under a new bool query.

这篇关于弹性搜索中的条件查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆