如何在弹性搜索查询中使用价格范围
这是我的查询:
{
"query": {
"nested": {
"path": "prices",
"query": {
"bool": {
"must": {
"range": {
"prices.price": {
"gte": 1000,
"lte": 4000
}
}
}
}
}
}
}
}
这里是索引
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 1,
"hits": [
{
"_index": "courses",
"_type": "doc",
"_id": "5",
"_score": 1,
"_source": {
"grade": {
"pk": 2,
"grade_name": "Grade 2"
},
"prices": [
{
"price_type": {
"pk": 1,
"is_online": true,
"buy_type": "Online"
},
"pk": 5,
"price": 150
}
],
"course_name": "English",
"course_config": {
"pk": 2,
"large_image": "/media/course_image/filer-6_CNewNel.png"
},
"id": 5
}
},
{
"_index": "courses",
"_type": "doc",
"_id": "4",
"_score": 1,
"_source": {
"grade": {
"pk": 1,
"grade_name": "Grade 1"
},
"prices": [
{
"price_type": {
"pk": 1,
"is_online": true,
"buy_type": "Online"
},
"pk": 4,
"price": 250
}
],
"course_name": "Geography",
"course_config": {
"pk": 4,
"large_image": "/media/course_image/filer-5.png"
},
"id": 4
}
},
{
"_index": "courses",
"_type": "doc",
"_id": "2",
"_score": 1,
"_source": {
"grade": {
"pk": 1,
"grade_name": "Grade 1"
},
"prices": [
{
"price_type": {
"pk": 1,
"is_online": true,
"buy_type": "Online"
},
"pk": 3,
"price": 125
}
],
"course_name": "Mathematics",
"course_config": {
"pk": 3,
"large_image": "/media/course_image/filer-3_rEN0JZb.png"
},
"id": 2
}
},
{
"_index": "courses",
"_type": "doc",
"_id": "1",
"_score": 1,
"_source": {
"grade": {
"pk": 1,
"grade_name": "Grade 1"
},
"prices": [
{
"price_type": {
"pk": 1,
"is_online": true,
"buy_type": "Online"
},
"pk": 1,
"price": 100
}
],
"course_name": "English",
"course_config": {
"pk": 2,
"large_image": "/media/course_image/filer-6_CNewNel.png"
},
"id": 1
}
}
]
}
}
不是基于范围来计算结果数据。如果我{'gte':0,'lte':4000},那么它可以正常工作。如果我使用的是{'gte':1000,'lte':4000 },然后它还会返回数据。某些网站要求使用“ to”和“ from”,但是使用相同的结果会得到相同的结果。