Names Jewman
Names Jewman

Reputation: 43

Elasticsearch: Querying a nested array

I have seen similar questions posted, but of course, none are exactly what I am trying to do.

When I run the query below I get this error:

"reason": "[nested] failed to find nested object under path [contentGroup]"

I think the problem is contentGroup.name does not exist because contentGroup is an array not an object. It needs to be something like this: contentGroup[0].name and contentGroup[1].name But I can't figure out how to do that.

Another thing that might be wrong is that I have two items nested within each other, I don't know if that is right or not.

Any help would be great!

My mapping:

{
"mappings": {
    "articles": {
        "properties": {
            "contentGroups": {
                "type": "nested",
                "properties": {
                    "contentGroup": {
                        "type": "nested",
                        "properties": {
                            "id": {
                            "type": "string"
                            },
                            "name": {
                                "type": "string"
                            }
                        }
                    }
                }
            }
        }
    }
}

What gets created when I input in an article ( Note the array being created ):

"contentGroups": {
"contentGroup": [
    {
        "name": "Breaking",
        "id": "104"
    },
    {
        "name": "News",
        "id": "22"
    }
]

My query:

{
"query": {
    "bool": {
        "must": [
            { "match": { "headline": "whatever" }}, 
            {
                "nested": {
                    "path": "contentGroup",
                    "query": {
                        "bool": {
                            "must": [
                                { "match": { "contentGroup.name": "Breaking" }}
                            ]
                        }
                    }
                }
            }
        ]
    }
}

Upvotes: 4

Views: 1170

Answers (1)

pkhlop
pkhlop

Reputation: 1844

You should use simpler mapping:

{
  "mappings": {
    "articles": {
      "properties": {
        "contentGroups": {
          "properties": {
            "id": {
              "type": "string"
            },
            "name": {
              "type": "string"
            }
          }
        }
      }
    }
  }
}

Each field in elasticsearch already supports multiple values, no need to specify this.

Upvotes: 3

Related Questions