# How to get match results from a single unit from a nested structure, having multiple fields inside?

**URL:** <https://discuss.elastic.co/t/how-to-get-match-results-from-a-single-unit-from-a-nested-structure-having-multiple-fields-inside/92477>\
**Category:** Elasticsearch\
**Created:** [July 10, 2017, 1:33pm UTC](https://discuss.elastic.co/t/how-to-get-match-results-from-a-single-unit-from-a-nested-structure-having-multiple-fields-inside/92477 "2017-07-10T13:33:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![sid\_1199](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@sid\_1199](https://discuss.elastic.co/u/sid_1199)\
**Post date:** [July 10, 2017, 1:33pm UTC](https://discuss.elastic.co/t/how-to-get-match-results-from-a-single-unit-from-a-nested-structure-having-multiple-fields-inside/92477/1 "2017-07-10T13:33:14Z")

</div>

Hello, I have a nested field which can be represented as follows. Documents can have any number of nested fields containing the structure below:

Field:  
{  
-\>course  
-\>duration  
}

Suppose I need to find people who have studied mathematics for a period of 7 years, the ES query would be as:

"query": {  
"bool": {  
"must": [  
{  
"nested": {  
"path": "Field",  
"query": {  
"bool": {  
"must": [  
{"match": {  
"Field.course": "mathematics"  
}  
},  
{"match": {  
"Field.duration": "7"  
}  
}  
]  
}  
}  
}  
}  
]  
}}

Now the issue is, I have data stored in a document as follows:

Field:  
{  
-\>course:Mathematics  
-\>duration: 3  
}  
{  
-\>course:English  
-\>duration: 8  
}

This document is getting returned as a match as both the requirements are fulfilled inside the Field.  
However, the result is incorrect, as the course and duration belong to 2 fields, and are not linked. Is there any way use match queries within specific units inside a nested structure?

The only document that should match is:

Field:  
{  
-\>course:Mathematics  
-\>duration: 7  
}

Thanks for your help

---

<div class="post-metadata">

**Author:** ![Heinmci](https://avatars.discourse-cdn.com/v4/letter/h/6a8cbe/32.png) [@Heinmci](https://discuss.elastic.co/u/Heinmci)\
**Post date:** [July 10, 2017, 2:08pm UTC](https://discuss.elastic.co/t/how-to-get-match-results-from-a-single-unit-from-a-nested-structure-having-multiple-fields-inside/92477/2 "2017-07-10T14:08:31Z")

</div>

Hi,  
I'm not 100% sure I understood your problem (was the duration for the English course meant to be 7 in your example?)  
But I think reading this should help you solve your problem :  
[https://www.elastic.co/guide/en/elasticsearch/guide/master/nested-objects.html](https://www.elastic.co/guide/en/elasticsearch/guide/master/nested-objects.html)

---

<div class="post-metadata">

**Author:** ![sid\_1199](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@sid\_1199](https://discuss.elastic.co/u/sid_1199)\
**Post date:** [July 10, 2017, 2:16pm UTC](https://discuss.elastic.co/t/how-to-get-match-results-from-a-single-unit-from-a-nested-structure-having-multiple-fields-inside/92477/3 "2017-07-10T14:16:09Z")

</div>

The duration of English is 7 years. However, I need docs which have mathematics for 7 years together.

Not mathematics in a separate field, and 7 in a separate field as the above query returns.  
I need the document to be returned if both the field matches are in the same unit, as specified in the last example

---

<div class="post-metadata">

**Author:** ![Heinmci](https://avatars.discourse-cdn.com/v4/letter/h/6a8cbe/32.png) [@Heinmci](https://discuss.elastic.co/u/Heinmci)\
**Post date:** [July 10, 2017, 2:17pm UTC](https://discuss.elastic.co/t/how-to-get-match-results-from-a-single-unit-from-a-nested-structure-having-multiple-fields-inside/92477/4 "2017-07-10T14:17:35Z")

</div>

I asked because in your example you put duration of english as 8, so It confused me!  
Did you read the information provided in the link? Seems like it answers your problem exactly.

According to the documentation (provided you have the correct mapping), your request should look something like :

```auto
GET /my_index/mytype/_search
{
  "query": {
          "nested": {
            "path": "Field", 
            "query": {
              "bool": {
                "must": [ 
                  {
                    "match": {
                      "Field.course": "Mathematics"
                    }
                  },
                  {
                    "match": {
                      "Field.duration": 7
                    }
                  }
                ]
              }
            }
          }
}}

```

---

<div class="post-metadata">

**Author:** ![sid\_1199](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@sid\_1199](https://discuss.elastic.co/u/sid_1199)\
**Post date:** [July 10, 2017, 2:24pm UTC](https://discuss.elastic.co/t/how-to-get-match-results-from-a-single-unit-from-a-nested-structure-having-multiple-fields-inside/92477/5 "2017-07-10T14:24:09Z")

</div>

I'm so sorry I did not see that. I went through the link, and it was really helpful, should hopefully solve my problem.  
Thank you so much

---

<div class="post-metadata">

**Author:** ![Heinmci](https://avatars.discourse-cdn.com/v4/letter/h/6a8cbe/32.png) [@Heinmci](https://discuss.elastic.co/u/Heinmci)\
**Post date:** [July 10, 2017, 2:26pm UTC](https://discuss.elastic.co/t/how-to-get-match-results-from-a-single-unit-from-a-nested-structure-having-multiple-fields-inside/92477/6 "2017-07-10T14:26:09Z")

</div>

No problem, good luck to you!

---

<div class="post-metadata">

**Author:** ![sid\_1199](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@sid\_1199](https://discuss.elastic.co/u/sid_1199)\
**Post date:** [July 10, 2017, 2:45pm UTC](https://discuss.elastic.co/t/how-to-get-match-results-from-a-single-unit-from-a-nested-structure-having-multiple-fields-inside/92477/7 "2017-07-10T14:45:06Z")

</div>

Hey, I ran the above query.  
I'm getting the correct result with a high score, but I'm also getting some incorrect ones with really low scores. Is there any reasons as to why this may be happening?

My field mapping for everything is:  
{"type":"text","index": "not\_analyzed","term\_vector": "yes","similarity":"boolean"}

And my search type is:  
dfs\_query\_then\_fetch

---

<div class="post-metadata">

**Author:** ![Heinmci](https://avatars.discourse-cdn.com/v4/letter/h/6a8cbe/32.png) [@Heinmci](https://discuss.elastic.co/u/Heinmci)\
**Post date:** [July 10, 2017, 2:47pm UTC](https://discuss.elastic.co/t/how-to-get-match-results-from-a-single-unit-from-a-nested-structure-having-multiple-fields-inside/92477/8 "2017-07-10T14:47:09Z")

</div>

I wouldn't know, sorry.  
Hopefully someone with more knowledge can help you out from here!

---

<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 7, 2017, 2:47pm UTC](https://discuss.elastic.co/t/how-to-get-match-results-from-a-single-unit-from-a-nested-structure-having-multiple-fields-inside/92477/9 "2017-08-07T14:47:40Z")

</div>

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