# Limiting Query to 1 Item within a source

**URL:** <https://discuss.elastic.co/t/limiting-query-to-1-item-within-a-source/94387>\
**Category:** Elasticsearch\
**Created:** [July 24, 2017, 7:55pm UTC](https://discuss.elastic.co/t/limiting-query-to-1-item-within-a-source/94387 "2017-07-24T19:55:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Velusamy\_Velu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/velusamy_velu/32/72364_2.png) [@Velusamy\_Velu](https://discuss.elastic.co/u/Velusamy_Velu)\
**Post date:** [July 24, 2017, 7:55pm UTC](https://discuss.elastic.co/t/limiting-query-to-1-item-within-a-source/94387/1 "2017-07-24T19:55:58Z")

</div>

Friends:

I have the following document a structural hierarchy, indexed.

```
{  
   "id":"abc",
   "nodeCount":12,
   "edgeCount":11,
   "degreesCountMap":[  
      {  
         "size":2,
         "count":10
      },
      {  
         "size":3,
         "count":2
      }
   ]
}

```

Then I ran the query:

```
{  
   "query":{  
      "bool":{  
         "must":[  
            {  
               "range":{  
                  "degreesCountMap.size":{  
                     "from":3
                  },
                  "degreesCountMap.count":{  
                     "from":10
                  }
               }
            }
         ]
      }
   }
}

```

Expected a no match because I thought ES will try to match `{"size":3,"count":2}` or `{"size":2,"count":10}` however, the document was matched. I assume it matched **size:3** from `{"size":3,"count":2}` and **count:10** from another `{"size":2,"count":10}`. My question: Is there a way to force the match to happen with one `{...}`.

I have also tried the query below and found the same result:

```
{  
   "query":{  
      "bool":{  
         "must":[  
            {  
               "range":{  
                  "degreesCountMap.size":{  
                     "from":3
                  }
               }
            },
            {  
               "range":{  
                  "degreesCountMap.count":{  
                     "from":10
                  }
               }
            }
         ]
      }
   }
}

```

I will appreciate your help.

---

<div class="post-metadata">

**Author:** ![Velusamy\_Velu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/velusamy_velu/32/72364_2.png) [@Velusamy\_Velu](https://discuss.elastic.co/u/Velusamy_Velu)\
**Post date:** [July 25, 2017, 2:53pm UTC](https://discuss.elastic.co/t/limiting-query-to-1-item-within-a-source/94387/2 "2017-07-25T14:53:48Z")

</div>

Found the answer in this post [Managing Relations Inside Elasticsearch](https://www.elastic.co/blog/managing-relations-inside-elasticsearch)

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [July 25, 2017, 7:41pm UTC](https://discuss.elastic.co/t/limiting-query-to-1-item-within-a-source/94387/3 "2017-07-25T19:41:43Z")

</div>

Elasticsearch/Lucene will bunch each inner field into the same bucket  
causing the behavior you noticed. You would need to define each inner  
object as a nested object [1] and use a nested query [2] to limit the query  
on a single nested document.

See  
[https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-objects.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-objects.html)

[1]  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html)  
[2]  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html)

---

<div class="post-metadata">

**Author:** ![Velusamy\_Velu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/velusamy_velu/32/72364_2.png) [@Velusamy\_Velu](https://discuss.elastic.co/u/Velusamy_Velu)\
**Post date:** [July 25, 2017, 8:02pm UTC](https://discuss.elastic.co/t/limiting-query-to-1-item-within-a-source/94387/4 "2017-07-25T20:02:12Z")

</div>

> [@Velusamy\_Velu](#):
>
> Found the answer in this post Managing Relations Inside Elasticsearch

Ivan:

Thanks for your reply. As I mentioned I did find the information I was looking for at [Managing Relations Inside Elasticsearch](https://www.elastic.co/blog/managing-relations-inside-elasticsearch).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 22, 2017, 8:02pm UTC](https://discuss.elastic.co/t/limiting-query-to-1-item-within-a-source/94387/5 "2017-08-22T20:02:13Z")

</div>

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