Combination of filter and range

hello everyone ,
i am newbie to Elasticsearch ,

i am having some issue with query searching ..
here is my code

GET /new_product/_search
{
  "query": {
    "bool" : {
      
      "filter": [ 
        { "term":  { "category_id": 37 }},
      
        { "range": { "price": { "gte": 2000,"lte":3000 }}}
      ]
}
}
}

but the problem is its showing unwanted result as below

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 18,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "new_product",
        "_type" : "_doc",
        "_id" : "14",
        "_score" : 0.0,
        "_source" : {
          "store_name_arabic" : "مجوهرات أناند",
          "price" : "249.38",
          "made_in" : 1,
          "parent_product_id" : 0,
          "badge_title_en" : null,
          "name_arabic" : "قلادة المرتعشة عيار 21 قيراط",
          "store_owner_id" : 2,
          "serial_number" : "13356652",
          "premium_buy" : "0",
          "length" : 0,
          "style_id" : 0,
          "created_admin" : 1,
          "sku" : "A2002354",
          "factor_liverate" : 18.5,
          "description_ar" : "قلادة المرتعشة عيار 21 قيراط",
          "sub_total" : "279.04",
          "making_cost_type" : "per_gram",
          "category_id" : 37,
          "material_cost" : "0",
          "brand_id" : 0,
          "calculation_factor" : "0.0106195739549839",
          "net_weight" : 13.48,
          "purity_title_ar" : "21k(875)",
          "meta_keywords" : "",
          "metal_color_id" : 3,
          "modified_at" : "2021-01-05T09:29:50.000Z",
          "status" : "active",
          "height" : 0,
          "product_type" : "regular",
          "try_at_home" : null,
          "meta_title" : "",
          "tag_line_1" : "قلادة المرتعشة مع الليرات Almortaash Necklace with coins",
          "last_stock_update" : null,
          "available_for" : "both",
          "making_cost" : "2.2",
          "final_making_cost" : "29.66",
          "final_making_cost_discount_amount" : "0",
          "store_owner_type" : null,
          "gender" : "2",
          "tax_id" : 2,
          "final_tax_amount" : "13.95",
          "metal_type_id" : 1,
          "buy_back" : "yes",
          "jewellery_type_id" : 1,
          "purity_title_en" : "21k(875)",
          "sub_category_id" : 43,
          "product_id" : 14,
          "final_price" : "292.99",
          "product_alias_name" : "",
          "metal_purity_id" : 2,
          "store_address" : "Shop No6",
          "click_count" : 82,
          "no_of_pieces" : 1,
          "@timestamp" : "2021-10-11T13:28:20.269Z",
          "price_per_gram" : "0",
          "approved_note" : null,
          "return" : "yes",
          "decline_note" : null,
          "metal_type_title_en" : "Gold",
          "created_at" : "2021-01-05T09:29:50.000Z",
          "tag_line_2" : "جمال الذهب البحريني الأصيل Bahrini Gold",
          "badge_title_ar" : null,
          "badge_id" : 0,
          "saleable_weight" : 13.48,
          "occasion" : "1,2",
          "return_days" : 30,
          "exchange" : "yes",
          "created_other" : null,
          "design_id" : 0,
          "main_image" : "https://gold-city-mall.s3.me-south-1.amazonaws.com/product/1609858907_2266.jpg",
          "meta_url" : "21k-necklace-21nb",
          "meta_description" : "                                                                            ",
          "displayorder" : 25,
          "store_id" : 2,
          "inventory_stock" : 1,
          "diamond_type" : null,
          "width" : 0,
          "description_en" : "21K NECKLACE (21NB)",
          "addons" : "",
          "average_rating" : "0",
          "name_english" : "21K NECKLACE (21NB)",
          "store_name_english" : "Anand Jewellers",
          "design_group_id" : 0,
          "making_cost_discount" : 0,
          "is_price_fixed" : "no",
          "@version" : "1"
        }
      },

the price must be between 2000 and 3000 but its giving 249.38 which is not valid , can anyone please help me with this issue ?

The price field has been indexed as a string. If you look at the resulting document, the value is surrounded by two quotes. As a result, the range query executes a string range query, rather than a numerical, which is not what you want.

The best solution is to fix your data ingestion process, so that price is indexed as a number, by removing those quotes.

Note that you will have to create a new index (or delete the current one and recreate it), as the price field has now been defined as a text field in that index' mappings (data schema), and there's no way to change an existing mapping.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.