我正在使用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_start
,sale_end
]区间内,而sale_price
在价格区间之间。请注意,我已经任意选择了间隔[1,1000],但是您可以自由更改。 now
在sale_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/