How to search in array of objects

Current version of ElasticSearch & Kibana 7.6.0

Hello folks! this is my template:

GET _template/my_template
"mappings" : {
      "_meta" : { },
      "_source" : { },
      "properties" : {
        "brands" : {
          "type" : "nested",
          "properties" : {
            "assessment" : {
              "type" : "float"
            },
            "quantity" : {
              "type" : "integer"
            },
            "name" : {
              "type" : "text"
            },
            "id" : {
              "type" : "integer"
            }
          }
        },
        "taxonomies" : {
          "type" : "nested",
          "properties" : {
            "path" : {
              "type" : "text"
            },
            "assessment" : {
              "type" : "float"
            },
            "quantity" : {
              "type" : "integer"
            },
            "name" : {
              "type" : "text"
            },
            "id" : {
              "type" : "integer"
            }
          }
        },
        "categories" : {
          "type" : "nested",
          "properties" : {
            "assessment" : {
              "type" : "float"
            },
            "quantity" : {
              "type" : "integer"
            },
            "name" : {
              "type" : "text"
            },
            "id" : {
              "type" : "integer"
            }
          }
        },
        "warehouse" : {
          "type" : "object",
          "properties" : {
            "name" : {
              "type" : "text"
            },
            "id" : {
              "type" : "integer"
            }
          }
        },
        "assesment" : {
          "type" : "float"
        }
      }
    }

I tried to follow this guide Nested and this one Nested Query. I tried to use the same documents from example but nothing... It return all values. In my documents what I trying to get is all Brands with Id = 1, this is my query:

GET remake/_search
  "_source": ["brands.name","brands.id"], 
  "query": {
    "nested": {
      "path": "brands",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "brands.id": 1
              }
            }
          ]
        }
      }
    }
  }
}

This should return only brands with id = 1, but returns all values. Should I fix the template/mappings? Thanks!

I think you are looking for inner hits. It will return another field named inner hits in your results containing only those brands objects that match you query condition. In the _source, the whole document is returned, that is why it has all the brands.

Hmmmm... _source is like SELECT column1, column2 FROM remake in SQL.
But... Is the template ok?

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