# Make elasticsearch nested query

**URL:** <https://discuss.elastic.co/t/make-elasticsearch-nested-query/127699>\
**Category:** Elasticsearch\
**Created:** [April 11, 2018, 7:53pm UTC](https://discuss.elastic.co/t/make-elasticsearch-nested-query/127699 "2018-04-11T19:53:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lucasluan](https://avatars.discourse-cdn.com/v4/letter/l/858c86/32.png) [@lucasluan](https://discuss.elastic.co/u/lucasluan)\
**Post date:** [April 11, 2018, 7:53pm UTC](https://discuss.elastic.co/t/make-elasticsearch-nested-query/127699/1 "2018-04-11T19:53:52Z")

</div>

I have a index with some nested informations and right now struggle with how make to make a query.

The index below represents a car piece and this piece belongs to 2 cars.

```
  {
    "_index": "items_production_20180411115923024",
    "_type": "item",
    "_id": "1232",
    "_score": 21.715849,
    "_source": {
      "description": "CUBO RODA TRASEIRA VW:GOL, VOYAGE G5/G6 09> C/ABS",
      "observation": null,
      "brand_id": "1 ",
      "product_line_id": "13",
      "line_id": "1",
      "line": "Rolamentos",
      "segment_id": "21",
      "segment": "cubo de roda",
      "application_features": [
        "4 furos"
      ],
      "original_codes": [
        " 5u0 501 611 "
      ],
      "original_brands": [
        "volkswagen"
      ],
      "vehicles": [
        {
          "id": "285",
          "brand": "volkswagen",
          "name": "golf",
          "years": [
            "2009",
            "2010",
            "2011",
            "2012",
            "2013",
            "2014",
            "2015",
            "2016",
            "2017",
            "2018"
          ]
        },
        {
          "id": "345",
          "brand": "volkswagen",
          "name": "jetta",
          "years": [
            "2015",
            "2016",
            "2017",
            "2018"
          ]
        }
      ]
    }
  }

```

I have to make the query match to one single result when search for the model and year of a car. Ie:

```
GET items_production_20180411115923024/_search
   {
     "query":{
    "bool":{
      "must":{
        "multi_match":{
          "query":"golf 2010",
          "type":"cross_fields",
          "operator":"and",
          "fields":[
            "vehicle_names^8",
            "vehicles.years^8"
          ]
        }
      }
    }
  },
  "size":10,"from":0
}

```

I have to return all docs wich are pieces of a golf year 2010. But my response is none?

What I'm doing wrong? How can I make this query?

---

<div class="post-metadata">

**Author:** ![JoshuaIn](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@JoshuaIn](https://discuss.elastic.co/u/JoshuaIn)\
**Post date:** [April 12, 2018, 6:36am UTC](https://discuss.elastic.co/t/make-elasticsearch-nested-query/127699/2 "2018-04-12T06:36:44Z")

</div>

> [@lucasluan](#):
>
> golf

Hi  
Try this query, basically you can use bool to perform the search:

> GET items\_production\_20180411115923024/\_search  
> {  
> "query": {  
> "bool": {  
> "must": [  
> {  
> "term": {  
> "vehicles.name": {  
> "value": "golf"  
> }  
> }  
> },  
> {  
> "terms": {  
> "vehicles.years": [  
> "2010"  
> ]  
> }  
> }  
> ]  
> }  
> }  
> }

Also, you can use query\_string instead of term and terms. With query\_string you can do case insensitive search and can also perform Boolean query within the query\_string query value.

Refer the link here:  
[[Query string query | Elasticsearch Guide [8.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html)]

---

<div class="post-metadata">

**Author:** ![lucasluan](https://avatars.discourse-cdn.com/v4/letter/l/858c86/32.png) [@lucasluan](https://discuss.elastic.co/u/lucasluan)\
**Post date:** [April 12, 2018, 12:36pm UTC](https://discuss.elastic.co/t/make-elasticsearch-nested-query/127699/3 "2018-04-12T12:36:58Z")

</div>

The problem with this approach, is that I need to know the parameters the user will inform and I will not have that.

Because of that I'm using the multi\_match with a query term. I really think that the vehicle name and year have better use as filters but the project need that the user input the query with name and year and return the results.

There is another way to perform that?

---

<div class="post-metadata">

**Author:** ![JoshuaIn](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@JoshuaIn](https://discuss.elastic.co/u/JoshuaIn)\
**Post date:** [April 13, 2018, 9:17am UTC](https://discuss.elastic.co/t/make-elasticsearch-nested-query/127699/4 "2018-04-13T09:17:15Z")

</div>

I understand what you meant thats why I mentioned querystring, it allows exactly what you are looking for, you may require to tweek a bit based on your requirement and you can add as many fields as you want on which you want to perform search:

try this:  
GET items\_production\_20180411115923024/\_search  
{  
"query": {  
"query\_string": {  
"fields": ["vehicles.name", "vehicles.years"],  
"query": "golf OR 2010"  
}  
}  
}

---

<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:** [May 11, 2018, 9:17am UTC](https://discuss.elastic.co/t/make-elasticsearch-nested-query/127699/5 "2018-05-11T09:17:31Z")

</div>

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