4

I have data in the following format

{
  "mappings": {
    "blog": {
      "properties": {
        "comments": {
          "type": "nested",
          "properties": {
            "subComments": {
              "type": "nested"
            }
          }
        }
      }
    }
  }
}

And i have multiple documents with data like

{
  "blog_post_id": "blog1",
  "comments": [
    {
      "id": "c1",
      "user_id": "u1",
      "timestamp": 1487781975676,
      "value": "CVLA1",
      "subComments": [
        {
          "value": "sub comment 1"
        },
        {
          "value": "sub comment 2"
        }
      ]
    },
    {
      "id": "c2",
      "user_id": "u1",
      "timestamp": 1487781975686,
      "value": "CVLA2",
      "subComments": [
        {
          "value": "sub comment 3"
        },
        {
          "value": "sub comment 4"
        }
      ]
    }
  ]
}

I'd like match the blog documents which have comment value CVLA1 and a suc comment which has value "sub comment 2".

I wrote a query like

{
  "query": {
    "nested": {
      "path": "comments",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "comments.value": "CVLA1"
              }
            },
            {
              "nested": {
                "path": "comments.subComments",
                "query": {
                  "match": {
                    "commnets.subComments.value": "sub comment 2"
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}

But this one doesn't work as expected. Any help how to query at different levels of a multi level nested document.

1 Answer 1

4

You have a typo in your query around commnets.subComments.value. It should be comments.subComments.value. So the entire query would look like this:

{
  "query": {
    "nested": {
      "path": "comments",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "comments.value": "CVLA1"
              }
            },
            {
              "nested": {
                "path": "comments.subComments",
                "query": {
                  "match": {
                    "comments.subComments.value": "sub comment 2"
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}

I double checked - it works fine for me.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.