0

How to use Price Range in elastic search query

Here is my query:

{
  "query": {
    "nested": {
      "path": "prices",
      "query": {
        "bool": {
          "must": {
            "range": {
              "prices.price": {
                "gte": 1000,
                "lte": 4000
              }
            }
          }
        }
      }
    }
  }
}

Here is Indexes

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

The resultant data is not computed based on range.If I {'gte':0, 'lte':4000}, then it works fine.If I am using {'gte':1000, 'lte':4000}, then also it return data.Some website ask to use 'to' and 'from', but get same result on using the same.

Jamin
  • 133
  • 12

0 Answers0