# Why this range query on nested object returns uncorrect data?

**URL:** <https://discuss.elastic.co/t/why-this-range-query-on-nested-object-returns-uncorrect-data/197015>\
**Category:** Elasticsearch\
**Created:** [August 27, 2019, 11:43pm UTC](https://discuss.elastic.co/t/why-this-range-query-on-nested-object-returns-uncorrect-data/197015 "2019-08-27T23:43:33Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bidzina\_Goginashvili](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bidzina_goginashvili/32/51114_2.png) [@Bidzina\_Goginashvili](https://discuss.elastic.co/u/Bidzina_Goginashvili)\
**Post date:** [August 27, 2019, 11:43pm UTC](https://discuss.elastic.co/t/why-this-range-query-on-nested-object-returns-uncorrect-data/197015/1 "2019-08-27T23:43:33Z")

</div>

GET bliad7/\_search  
{  
"query": {"range": {  
"prices.price": {  
"gte": 500,  
"lte": 600  
}  
}}  
}

mapping

"Price" : {  
"type" : "nested",  
"properties" : {  
"Price" : {  
"type" : "double"  
}  
}  
},

---

<div class="post-metadata">

**Author:** ![Ignacio\_Vera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ignacio_vera/32/36674_2.png) [@Ignacio\_Vera](https://discuss.elastic.co/u/Ignacio_Vera)\
**Post date:** [August 28, 2019, 7:21am UTC](https://discuss.elastic.co/t/why-this-range-query-on-nested-object-returns-uncorrect-data/197015/2 "2019-08-28T07:21:35Z")

</div>

You need to use a nested query when searching on nested fields. For example (note that you are using different case in your provided example for mapping and querying):

mapping:

```auto
PUT example
{
  "settings": {
    "index": {
      "number_of_shards": 1,
      "number_of_replicas": 0
    }
  },
  "mappings": {
    "dynamic": "strict",
    "properties": {
      "Prices": {
        "type": "nested",
        "properties": {
          "Price": {
            "type": "double"
          }
        }
      }
    }
  }
}

```

Query:

```auto
GET example/_search
{
  "query": {
    "nested": {
      "path": "Prices",
      "query": {
        "range": {
          "Prices.Price": {
            "gte": 500,
            "lte": 600
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Bidzina\_Goginashvili](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bidzina_goginashvili/32/51114_2.png) [@Bidzina\_Goginashvili](https://discuss.elastic.co/u/Bidzina_Goginashvili)\
**Post date:** [August 28, 2019, 10:49am UTC](https://discuss.elastic.co/t/why-this-range-query-on-nested-object-returns-uncorrect-data/197015/3 "2019-08-28T10:49:31Z")

</div>

i don't understand what i am mapping in different way than you

---

<div class="post-metadata">

**Author:** ![Ignacio\_Vera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ignacio_vera/32/36674_2.png) [@Ignacio\_Vera](https://discuss.elastic.co/u/Ignacio_Vera)\
**Post date:** [August 28, 2019, 10:55am UTC](https://discuss.elastic.co/t/why-this-range-query-on-nested-object-returns-uncorrect-data/197015/4 "2019-08-28T10:55:49Z")

</div>

What exactly yo don't understand? the important part from your case is the following:

```auto
"properties": {
      "Prices": {
        "type": "nested",
        "properties": {
          "Price": {
            "type": "double"
          }
        }
      }

```

---

<div class="post-metadata">

**Author:** ![Bidzina\_Goginashvili](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bidzina_goginashvili/32/51114_2.png) [@Bidzina\_Goginashvili](https://discuss.elastic.co/u/Bidzina_Goginashvili)\
**Post date:** [August 28, 2019, 11:16am UTC](https://discuss.elastic.co/t/why-this-range-query-on-nested-object-returns-uncorrect-data/197015/5 "2019-08-28T11:16:57Z")

</div>

"Price" : {  
"type" : "nested",  
"properties" : {  
"Price" : {  
"type" : "double"  
}  
}  
},

and i have the same mapping

---

<div class="post-metadata">

**Author:** ![Bidzina\_Goginashvili](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bidzina_goginashvili/32/51114_2.png) [@Bidzina\_Goginashvili](https://discuss.elastic.co/u/Bidzina_Goginashvili)\
**Post date:** [August 28, 2019, 11:17am UTC](https://discuss.elastic.co/t/why-this-range-query-on-nested-object-returns-uncorrect-data/197015/6 "2019-08-28T11:17:23Z")

</div>

too.

---

<div class="post-metadata">

**Author:** ![Bidzina\_Goginashvili](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bidzina_goginashvili/32/51114_2.png) [@Bidzina\_Goginashvili](https://discuss.elastic.co/u/Bidzina_Goginashvili)\
**Post date:** [August 28, 2019, 11:18am UTC](https://discuss.elastic.co/t/why-this-range-query-on-nested-object-returns-uncorrect-data/197015/7 "2019-08-28T11:18:26Z")

</div>

GET bliad7/\_search  
{  
"query": {  
"nested": {  
"path": "Price",  
"query": {  
"range": {  
"Price.Price": {  
"gte": 0,  
"lte": 10  
}  
}  
}  
}  
}  
}  
this query returns nothing

---

<div class="post-metadata">

**Author:** ![Bidzina\_Goginashvili](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bidzina_goginashvili/32/51114_2.png) [@Bidzina\_Goginashvili](https://discuss.elastic.co/u/Bidzina_Goginashvili)\
**Post date:** [August 28, 2019, 11:18am UTC](https://discuss.elastic.co/t/why-this-range-query-on-nested-object-returns-uncorrect-data/197015/8 "2019-08-28T11:18:47Z")

</div>

but there are products which are in this range...

---

<div class="post-metadata">

**Author:** ![Ignacio\_Vera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ignacio_vera/32/36674_2.png) [@Ignacio\_Vera](https://discuss.elastic.co/u/Ignacio_Vera)\
**Post date:** [August 28, 2019, 11:26am UTC](https://discuss.elastic.co/t/why-this-range-query-on-nested-object-returns-uncorrect-data/197015/9 "2019-08-28T11:26:18Z")

</div>

This works for me:

```auto
PUT example
{
  "settings": {
    "index": {
      "number_of_shards": 1,
      "number_of_replicas": 0
    }
  },
  "mappings": {
    "dynamic": "strict",
    "properties": {
      "Price": {
        "type": "nested",
        "properties": {
          "Price": {
            "type": "double"
          }
        }
      }
    }
  }
}

POST example/_doc 
{
  "Price" : [ 
    {"Price" : 10},
    {"Price" : 30}
    ]
}

POST example/_doc 
{
  "Price" : [ 
    {"Price" : 40},
    {"Price" : 50}
    ]
}

GET example/_search
{
  "query": {
    "nested": {
      "path": "Price",
      "query": {
        "range": {
          "Price.Price": {
            "gte": 10,
            "lte": 30
          }
        }
      }
    }
  }
}

```

and I get a hit as expected:

```auto
{
  "took" : 371,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "example",
        "_type" : "_doc",
        "_id" : "JvL212wBubu-7AafcvuH",
        "_score" : 1.0,
        "_source" : {
          "Price" : [
            {
              "Price" : 10
            },
            {
              "Price" : 30
            }
          ]
        }
      }
    ]
  }
}

```

Not sure what is the issue with your environment. Could you try to run this commands in your system and check if it works for you?

---

<div class="post-metadata">

**Author:** ![Bidzina\_Goginashvili](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bidzina_goginashvili/32/51114_2.png) [@Bidzina\_Goginashvili](https://discuss.elastic.co/u/Bidzina_Goginashvili)\
**Post date:** [August 28, 2019, 11:49am UTC](https://discuss.elastic.co/t/why-this-range-query-on-nested-object-returns-uncorrect-data/197015/10 "2019-08-28T11:49:11Z")

</div>

{  
"error": {  
"root\_cause": [  
{  
"type": "query\_shard\_exception",  
"reason": "failed to create query: {\n "nested" : {\n "query" : {\n "range" : {\n "prices.price" : {\n "from" : 500,\n "to" : 600,\n "include\_lower" : true,\n "include\_upper" : true,\n "boost" : 1.0\n }\n }\n },\n "path" : "prices",\n "ignore\_unmapped" : false,\n "score\_mode" : "avg",\n "boost" : 1.0\n }\n}",  
"index\_uuid": "-q0nMbfoRN6Jbhr\_5vNkew",  
"index": "bliad9"  
}  
],  
"type": "search\_phase\_execution\_exception",  
"reason": "all shards failed",  
"phase": "query",  
"grouped": true,  
"failed\_shards": [  
{  
"shard": 0,  
"index": "bliad9",  
"node": "l6WS2f86S-20-\_39aVzTaA",  
"reason": {  
"type": "query\_shard\_exception",  
"reason": "failed to create query: {\n "nested" : {\n "query" : {\n "range" : {\n "prices.price" : {\n "from" : 500,\n "to" : 600,\n "include\_lower" : true,\n "include\_upper" : true,\n "boost" : 1.0\n }\n }\n },\n "path" : "prices",\n "ignore\_unmapped" : false,\n "score\_mode" : "avg",\n "boost" : 1.0\n }\n}",  
"index\_uuid": "-q0nMbfoRN6Jbhr\_5vNkew",  
"index": "bliad9",  
"caused\_by": {  
"type": "illegal\_state\_exception",  
"reason": "[nested] nested object under path [prices] is not of nested type"  
}  
}  
}  
]  
},  
"status": 400  
}

okey this gave me the error but not sure what it is..

---

<div class="post-metadata">

**Author:** ![Ignacio\_Vera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ignacio_vera/32/36674_2.png) [@Ignacio\_Vera](https://discuss.elastic.co/u/Ignacio_Vera)\
**Post date:** [August 28, 2019, 12:11pm UTC](https://discuss.elastic.co/t/why-this-range-query-on-nested-object-returns-uncorrect-data/197015/11 "2019-08-28T12:11:27Z")

</div>

There is something strange I the error. It is not the same query I posted.

---

<div class="post-metadata">

**Author:** ![Bidzina\_Goginashvili](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bidzina_goginashvili/32/51114_2.png) [@Bidzina\_Goginashvili](https://discuss.elastic.co/u/Bidzina_Goginashvili)\
**Post date:** [August 28, 2019, 12:13pm UTC](https://discuss.elastic.co/t/why-this-range-query-on-nested-object-returns-uncorrect-data/197015/12 "2019-08-28T12:13:50Z")

</div>

are you kidding me:smiley: i did what you said and it is not doing and u are saying that it is not the same query and there is no difference

---

<div class="post-metadata">

**Author:** ![Ignacio\_Vera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ignacio_vera/32/36674_2.png) [@Ignacio\_Vera](https://discuss.elastic.co/u/Ignacio_Vera)\
**Post date:** [August 28, 2019, 12:16pm UTC](https://discuss.elastic.co/t/why-this-range-query-on-nested-object-returns-uncorrect-data/197015/13 "2019-08-28T12:16:22Z")

</div>

Not really, I post the statements again I would like you to run, not the name of the attributes (`Price`, singular and the first letter uppercase):

```auto
PUT example
{
  "settings": {
    "index": {
      "number_of_shards": 1,
      "number_of_replicas": 0
    }
  },
  "mappings": {
    "dynamic": "strict",
    "properties": {
      "Price": {
        "type": "nested",
        "properties": {
          "Price": {
            "type": "double"
          }
        }
      }
    }
  }
}

POST example/_doc 
{
  "Price" : [ 
    {"Price" : 10},
    {"Price" : 30}
    ]
}

POST example/_doc 
{
  "Price" : [ 
    {"Price" : 40},
    {"Price" : 50}
    ]
}

GET example/_search
{
  "query": {
    "nested": {
      "path": "Price",
      "query": {
        "range": {
          "Price.Price": {
            "gte": 10,
            "lte": 30
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Bidzina\_Goginashvili](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bidzina_goginashvili/32/51114_2.png) [@Bidzina\_Goginashvili](https://discuss.elastic.co/u/Bidzina_Goginashvili)\
**Post date:** [August 28, 2019, 12:20pm UTC](https://discuss.elastic.co/t/why-this-range-query-on-nested-object-returns-uncorrect-data/197015/14 "2019-08-28T12:20:47Z")

</div>

what i found is really interesting..  
"Price" : {  
"type" : "nested",  
"properties" : {  
"Price" : {  
"type" : "double"  
}  
}  
},

"prices" : {  
"properties" : {  
"price" : {  
"type" : "float"  
}  
}  
},

it did 2 mapping while migration... one for float one for double

---

<div class="post-metadata">

**Author:** ![Ignacio\_Vera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ignacio_vera/32/36674_2.png) [@Ignacio\_Vera](https://discuss.elastic.co/u/Ignacio_Vera)\
**Post date:** [August 28, 2019, 12:27pm UTC](https://discuss.elastic.co/t/why-this-range-query-on-nested-object-returns-uncorrect-data/197015/15 "2019-08-28T12:27:40Z")

</div>

The issue here is that your original mapping is not defined as strict. your original mapping was defined as:

```auto
    "Price": {
        "type": "nested",
        "properties": {
          "Price": {
            "type": "double"
          }
        }
      }

```

But then you try to insert the following data:

```auto
POST example/_doc 
{
  "prices" : [ 
    {"price" : 10},
    {"price" : 30}
    ]
}

```

Elasticsearch did not find any matching definition on your mapping and therefore created a new field in the mapping:

```auto
"prices" : {
  "properties" : { 
     "price" : {
     "type" : "float"
  }
}

```

---

<div class="post-metadata">

**Author:** ![Bidzina\_Goginashvili](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bidzina_goginashvili/32/51114_2.png) [@Bidzina\_Goginashvili](https://discuss.elastic.co/u/Bidzina_Goginashvili)\
**Post date:** [August 28, 2019, 12:36pm UTC](https://discuss.elastic.co/t/why-this-range-query-on-nested-object-returns-uncorrect-data/197015/16 "2019-08-28T12:36:08Z")

</div>

i am going to do migration with dynamic strict... will it work?

---

<div class="post-metadata">

**Author:** ![Ignacio\_Vera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ignacio_vera/32/36674_2.png) [@Ignacio\_Vera](https://discuss.elastic.co/u/Ignacio_Vera)\
**Post date:** [August 28, 2019, 12:38pm UTC](https://discuss.elastic.co/t/why-this-range-query-on-nested-object-returns-uncorrect-data/197015/17 "2019-08-28T12:38:16Z")

</div>

At least it will tell you when the migration is trying to ingest data with an unexpected format. Generally speaking if your application is working with a fixed mapping I would always recommend using strict mapping.

---

<div class="post-metadata">

**Author:** ![Bidzina\_Goginashvili](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bidzina_goginashvili/32/51114_2.png) [@Bidzina\_Goginashvili](https://discuss.elastic.co/u/Bidzina_Goginashvili)\
**Post date:** [August 28, 2019, 12:39pm UTC](https://discuss.elastic.co/t/why-this-range-query-on-nested-object-returns-uncorrect-data/197015/18 "2019-08-28T12:39:12Z")

</div>

okey i will try it.. thank you for help...

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [August 28, 2019, 1:27pm UTC](https://discuss.elastic.co/t/why-this-range-query-on-nested-object-returns-uncorrect-data/197015/19 "2019-08-28T13:27:37Z")

</div>

You may have summarized your mapping to show just the price field, but if you haven't note there's no advantage to using the `nested` type for single-field objects - only disadvantages.

---

<div class="post-metadata">

**Author:** ![Bidzina\_Goginashvili](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bidzina_goginashvili/32/51114_2.png) [@Bidzina\_Goginashvili](https://discuss.elastic.co/u/Bidzina_Goginashvili)\
**Post date:** [August 28, 2019, 8:38pm UTC](https://discuss.elastic.co/t/why-this-range-query-on-nested-object-returns-uncorrect-data/197015/20 "2019-08-28T20:38:02Z")

</div>

I REMOVED NESTED AND I HAVE QUERY LIKE THIS  
{  
"query":  
{  
"range": {  
"prices.price": {  
"gte":500,  
"lte": 1000  
}  
}  
}  
}  
BUT IT GETS WRONG DATA

"prices" : [  
{  
"price" : 13012.0  
},  
{  
"price" : 70867.0  
},  
{  
"price" : 4984.0  
},  
{  
"price" : 44111.0  
},  
{  
"price" : 67594.0  
},  
{  
"price" : 26644.0  
},  
{  
"price" : 42066.0  
},  
{  
"price" : 699.0  
},  
{  
"price" : 72687.0  
}  
],

[Next page](https://discuss.elastic.co/t/why-this-range-query-on-nested-object-returns-uncorrect-data/197015.md?page=2)
