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

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

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

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

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 :

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

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

No problem, good luck to you!

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

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

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