我在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/