# Difficulty in formulating query for my usecase

**URL:** https://discuss.elastic.co/t/difficulty-in-formulating-query-for-my-usecase/28773
**Category:** Elasticsearch
**Created:** [September 7, 2015, 11:27am UTC](https://discuss.elastic.co/t/difficulty-in-formulating-query-for-my-usecase/28773 "2015-09-07T11:27:48Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![N\_M\_10](https://avatars.discourse-cdn.com/v4/letter/n/f19dbf/32.png) [@N\_M\_10](https://discuss.elastic.co/u/N_M_10)
#### Post date: [September 7, 2015, 11:27am UTC](https://discuss.elastic.co/t/difficulty-in-formulating-query-for-my-usecase/28773/1 "2015-09-07T11:27:48Z")

</div>

Note: We are not using elasticsearch's parent-child concept.

Below is my JSON Doc inside elasticsearch.

```
{
        "_index": "in22",
        "_type": "event",
        "_source": {
            "Title": "Jurassic World",
            "Language": [
                "English"
            ],
            "inner_hits": [
                {
                    "Code": "ET00009709",
                    "IsDefault": "",
                    "Language": "English",
                    "Format": "3D",
                    "Region": "MUMBAI"
                },
                {
                    "Code": "ET00009710",
                    "IsDefault": "Y",
                    "Language": "English",
                    "Format": "2D",
                    "Region": "CHEN"
                },
                {
                    "Code": "ET00009713",
                    "IsDefault": "",
                    "Language": "Hindi",
                    "Format": "2D",
                    "Region": "MUMBAI"
                },
                {
                    "Code": "ET00009714",
                    "IsDefault": "",
                    "Language": "Tamil",
                    "Format": "3D",
                    "Region": "MUMBAI"
                },
                {
                    "Code": "ET00009715",
                    "IsDefault": "",
                    "Language": "Hindi",
                    "Format": "3D",
                    "Region": "MUMBAI"
                },
                {
                    "Code": "ET00009716",
                    "IsDefault": "",
                    "Language": "Bengali",
                    "Format": "2D",
                    "Region": "MUMBAI"
                }
            ]
        }
    }

```

And all I wanted to know is this, can i build a query to output only those block inside inner\_hits which contains region "MUMBAI",  
for example  
inner\_hits should contain this

```
   {
                "Code": "ET00009709",
                "IsDefault": "",
                "Language": "English",
                "Format": "3D",
                "Region": "MUMBAI"
}

```

and should not contain

```
 {
                "Code": "ET00009710",
                "IsDefault": "Y",
                "Language": "English",
                "Format": "2D",
                "Region": "CHEN"
}

```

Is this possible?

---

<div class="post-metadata">

### Author: ![a2tirb](https://avatars.discourse-cdn.com/v4/letter/a/b19c9b/32.png) [@a2tirb](https://discuss.elastic.co/u/a2tirb)
#### Post date: [September 7, 2015, 4:45pm UTC](https://discuss.elastic.co/t/difficulty-in-formulating-query-for-my-usecase/28773/2 "2015-09-07T16:45:48Z")

</div>

If you don't want to use parent/child, you could still use nested documents (see [https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-nested-type.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-nested-type.html)). Below is a small example. Is that what you mean?

```auto
PUT testidx
{
  "mappings": {
    "doc": {
      "properties": {
        "nested_docs": {
          "type": "nested"
        }
      }
    }
  }
}

POST testidx/doc
{
  "Title": "Jurassic World",
  "Language": [
    "English"
  ],
  "nested_docs": [
    {
      "Code": "ET00009709",
      "IsDefault": "",
      "Language": "English",
      "Format": "3D",
      "Region": "MUMBAI"
    },
    {
      "Code": "ET00009710",
      "IsDefault": "Y",
      "Language": "English",
      "Format": "2D",
      "Region": "CHEN"
    }
  ]
}
POST testidx/_search
{
  "fields": [], 
  "query": {
    "nested": {
      "path": "nested_docs",
      "query": {
        "match": {
          "Region": "MUMBAI"
        }
      },
      "inner_hits": {}
    }
  }
}

```

would result in:

```auto
  {
   "took": 6,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1.4054651,
      "hits": [
         {
            "_index": "testidx",
            "_type": "doc",
            "_id": "AU-oqm6l8HuUQps-lae-",
            "_score": 1.4054651,
            "inner_hits": {
               "nested_docs": {
                  "hits": {
                     "total": 1,
                     "max_score": 1.4054651,
                     "hits": [
                        {
                           "_index": "testidx",
                           "_type": "doc",
                           "_id": "AU-oqm6l8HuUQps-lae-",
                           "_nested": {
                              "field": "nested_docs",
                              "offset": 0
                           },
                           "_score": 1.4054651,
                           "_source": {
                              "Code": "ET00009709",
                              "IsDefault": "",
                              "Language": "English",
                              "Format": "3D",
                              "Region": "MUMBAI"
                           }
                        }
                     ]
                  }
               }
            }
         }
      ]
   }
}

```

---

<div class="post-metadata">

### Author: ![N\_M\_10](https://avatars.discourse-cdn.com/v4/letter/n/f19dbf/32.png) [@N\_M\_10](https://discuss.elastic.co/u/N_M_10)
#### Post date: [September 8, 2015, 6:44am UTC](https://discuss.elastic.co/t/difficulty-in-formulating-query-for-my-usecase/28773/3 "2015-09-08T06:44:49Z")

</div>

> [@a2tirb](#):
>
> {  
> "fields": ,  
> "query": {  
> "nested": {  
> "path": "nested\_docs",  
> "query": {  
> "match": {  
> "Region": "MUMBAI"  
> }  
> },  
> "inner\_hits": {}  
> }  
> }  
> }

Ok, First of all Thanks, we are now one step closer to our goal, but the query you mentioned produces only 3 nested doc instead of 5, Let see it, Original JSON DOC in elasticsearch

```
      {
    "_index": "in22",
    "_source": {
        "Language": [
            "English"
        ],
        "Tags": [
            "MT",
            "MUMBAI",
            "CHEN"
        ],
        "_boost": 3,
        "inner_hits": [
            {
                "Code": "ET00009709",
                "IsDefault": "",
                "Language": "English",
                "Format": "3D",
                "Region": "MUMBAI"
            },
            {
                "Code": "ET00009710",
                "IsDefault": "Y",
                "Language": "English",
                "Format": "2D",
                "Region": "CHEN"
            },
            {
                "Code": "ET00009713",
                "IsDefault": "",
                "Language": "Hindi",
                "Format": "2D",
                "Region": "MUMBAI"
            },
            {
                "Code": "ET00009714",
                "IsDefault": "",
                "Language": "Tamil",
                "Format": "3D",
                "Region": "MUMBAI"
            },
            {
                "Code": "ET00009715",
                "IsDefault": "",
                "Language": "Hindi",
                "Format": "3D",
                "Region": "MUMBAI"
            },
            {
                "Code": "ET00009716",
                "IsDefault": "",
                "Language": "Bengali",
                "Format": "2D",
                "Region": "MUMBAI"
            }
        ]
    }
}

```

So as we can see there are 6 sub-doc within inner\_hits and out of that 5 has `Region=MUMBAI` so our query should produce all 5 of them, but with your query result i got is

---

<div class="post-metadata">

### Author: ![N\_M\_10](https://avatars.discourse-cdn.com/v4/letter/n/f19dbf/32.png) [@N\_M\_10](https://discuss.elastic.co/u/N_M_10)
#### Post date: [September 8, 2015, 6:51am UTC](https://discuss.elastic.co/t/difficulty-in-formulating-query-for-my-usecase/28773/4 "2015-09-08T06:51:11Z")

</div>

Pasting in separate post, as body limit of previous post has been exhausted.

```
 {
       ...
       
        "_shards": {
           ...
        },
        "hits": {
            "total": 2,
            "max_score": 28.32935,
            "hits": [
                {
                   ...
                    "fields": {
                        "Title": [
                            "Jurassic World"
                        ],
                        "Status": [
                            "NS"
                        ]
                    },
                    "inner_hits": {
                        "inner_hits": {
                            "hits": {
                                "total": 5,
                                "max_score": 25.036417,
                                "hits": [
                                    {
                                        "_index": "in22",
                                        "_type": "event",
                                        "_id": "ET00009709",
                                        "_nested": {
                                            "field": "inner_hits",
                                            "offset": 5
                                        },
                                        "_score": 25.036417,
                                        "_source": {
                                            "Code": "ET00009716",
                                            "IsDefault": "",
                                            "Language": "Bengali",
                                            "Format": "2D",
                                            "Region": "MUMBAI"
                                        }
                                    },
                                    {
                                        "_index": "in22",
                                        "_type": "event",
                                        "_id": "ET00009709",
                                        "_nested": {
                                            "field": "inner_hits",
                                            "offset": 4
                                        },
                                        "_score": 25.036417,
                                        "_source": {
                                            "Code": "ET00009715",
                                            "IsDefault": "",
                                            "Language": "Hindi",
                                            "Format": "3D",
                                            "Region": "MUMBAI"
                                        }
                                    },
                                    {
                                        "_index": "in22",
                                        "_type": "event",
                                        "_id": "ET00009709",
                                        "_nested": {
                                            "field": "inner_hits",
                                            "offset": 3
                                        },
                                        "_score": 25.036417,
                                        "_source": {
                                            "Code": "ET00009714",
                                            "IsDefault": "",
                                            "Language": "Tamil",
                                            "Format": "3D",
                                            "Region": "MUMBAI"
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            ]
        }
    }

```

So I was wondering how to produces all 5 document which matches `Region=MUMBAI`

---

<div class="post-metadata">

### Author: ![a2tirb](https://avatars.discourse-cdn.com/v4/letter/a/b19c9b/32.png) [@a2tirb](https://discuss.elastic.co/u/a2tirb)
#### Post date: [September 8, 2015, 9:08am UTC](https://discuss.elastic.co/t/difficulty-in-formulating-query-for-my-usecase/28773/5 "2015-09-08T09:08:43Z")

</div>

You have to configure the `size` parameter as described here: [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html#\_options](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html#_options)

---

<div class="post-metadata">

### Author: ![N\_M\_10](https://avatars.discourse-cdn.com/v4/letter/n/f19dbf/32.png) [@N\_M\_10](https://discuss.elastic.co/u/N_M_10)
#### Post date: [September 8, 2015, 9:41am UTC](https://discuss.elastic.co/t/difficulty-in-formulating-query-for-my-usecase/28773/6 "2015-09-08T09:41:30Z")

</div>

Ok, thanks again. But there could be any number of nested doc say 5, 7, n, We want to display n if hits.total is n.  
I Hope you understand my usecase.  
If not please ask.

---

<div class="post-metadata">

### Author: ![a2tirb](https://avatars.discourse-cdn.com/v4/letter/a/b19c9b/32.png) [@a2tirb](https://discuss.elastic.co/u/a2tirb)
#### Post date: [September 8, 2015, 12:46pm UTC](https://discuss.elastic.co/t/difficulty-in-formulating-query-for-my-usecase/28773/7 "2015-09-08T12:46:57Z")

</div>

Hm. I was about to answer: "Well, just set it to INT\_MAX" but when I tried it out that actually results in an oom...I opened an issue here: [https://github.com/elastic/elasticsearch/issues/13394](https://github.com/elastic/elasticsearch/issues/13394) . Once this is fixed you should be able to just get all nested docs by setting size to some ridiculously high number.

---

<div class="post-metadata">

### Author: ![N\_M\_10](https://avatars.discourse-cdn.com/v4/letter/n/f19dbf/32.png) [@N\_M\_10](https://discuss.elastic.co/u/N_M_10)
#### Post date: [September 8, 2015, 1:22pm UTC](https://discuss.elastic.co/t/difficulty-in-formulating-query-for-my-usecase/28773/8 "2015-09-08T13:22:38Z")

</div>

Thanks again. It will be great if you could explain me how this works end-to-end, like how this query work exactly, because i will be plug-in this query with my initial query which looks like this

```
$query = [
        "filtered" => [
            "query" => [
                "bool" => [
                    "should" => [
                        [
                            'query_string' => [
                                'fields' => [
                                    'Title.title^4',
                                    'Title.ngrams_front^2',
                                    'Title.ngrams_back'
                                ],
                                'defaultOperator' => 'or',
                                'analyzer' => 'titles_default_analyzer',
                                'query' => $paramsObj->q
                            ]
                        ],
                        [
                            'fuzzy' => [
                                'Title.title' => [
                                    'value' => $paramsObj->q,
                                    'boost' => 1,
                                    'min_similarity' => 0.5,
                                    'max_expansions' => 20,
                                    'prefix_length' => 0
                                ]
                            ]
                        ]
                    ]   
                ]
            ],

            "filter" => $filters
        ]	
    ];
```

---

<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: [July 5, 2017, 11:51pm UTC](https://discuss.elastic.co/t/difficulty-in-formulating-query-for-my-usecase/28773/9 "2017-07-05T23:51:40Z")

</div>


