Make elasticsearch nested query

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?

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 [7.15] | Elastic]

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?

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"
}
}
}

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