[英]How to write complex ElasticSearch query
I want to return the data from ElasticSearch using the range query.我想使用范围查询从 ElasticSearch 返回数据。 My Condition is something like this.我的情况是这样的。
((Range(Price and Discount) OR Range(Price) AND Filter(Must1) AND Filter(Must2))
The issue I am facing is that some document contains both price and discount but some only contains Price.我面临的问题是有些文件同时包含价格和折扣,但有些只包含价格。 I need a query to get data according to the specified range.我需要一个查询来根据指定范围获取数据。 So, it returns the discount field but not the specified range which I want.因此,它返回折扣字段而不是我想要的指定范围。
right now I am using this query.现在我正在使用这个查询。
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"bool": {
"must": [
{
"range": {
"discount": {
"gte": 10,
"lte": 12
}
}
},
{
"range": {
"price": {
"gte": 10,
"lte": 12
}
}
}
]
}
},
{
"bool": {
"should": [
{
"range": {
"discount": {
"gte": 10,
"lte": 12
}
}
},
{
"range": {
"price": {
"gte": 10,
"lte": 12
}
}
}
]
}
}
]
}
},
{
"terms": {
"Category": [
"123"
]
}
},
{
"nested": {
"path": "the_path",
"query": {
"bool": {
"must": {
"match": {
}
},
"filter": [
]
}
}
}
}
]
}
}
Please help me with this I am stuck with it from past few days.请帮我解决这个问题,过去几天我一直坚持下去。
Based on the condition you have given, following DSL Query will be created根据您提供的条件,将创建以下 DSL 查询
{
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"bool": {
"must": [
{
"range": {
"price": {
"gte": 10,
"lte": 20
}
}
},
{
"range": {
"deiscount": {
"gte": 10,
"lte": 20
}
}
}
]
}
},
{
"range": {
"price": {
"gte": 10,
"lte": 20
}
}
}
]
}
},
{
"bool": {
"filter": {
"term": {
"user.id": "kimchy"
}
}
}
},
{
"bool": {
"filter": {
"term": {
"user.id": "kimchy"
}
}
}
}
]
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.