1

Consider a document in Elasticsearch like this:

{
  "id": 1,
  "Comment": "Comment text",
  "Reply": [{
    "id": 2,
    "Comment": "Nested comment text",
  }, {
    "id": 3,
    "Comment": "Another nested comment text",
  }]
}

I want to search for id == 2 without knowing whether it is in the firsts level of the document or in the second. Is this possible? Please also keep in mind that the nested level can be anything (unknown at development time).

If this is possible, what's the query to return this document by searching for id == 2 without knowing that there's an id is in the second level of the document?

Mehran
  • 15,593
  • 27
  • 122
  • 221
  • 1
    You know that in ES `nested` has a special meaning. So is this field really a [nested](https://www.elastic.co/guide/en/elasticsearch/reference/2.0/nested.html) field? – Andrei Stefan Aug 16 '16 at 23:59
  • My bad, you are right. I miss-typed the example. I'll update the stem. Thanks. But the question remains the same. – Mehran Aug 17 '16 at 00:21

1 Answers1

2

Try this:

{
  "query": {
    "query_string": {
      "fields": ["*.id","id"],
      "query": "2"
    }
  }
}
Andrei Stefan
  • 51,654
  • 6
  • 98
  • 89
  • Thanks. Is it possible to mix `query_string` with other types of queries like `term` and `must`? – Mehran Aug 17 '16 at 01:12
  • 1
    Yes, use a `bool` and place the `query_string` wherever you see fit: `should`, `must` etc. It depends on what you already have as a query. – Andrei Stefan Aug 17 '16 at 01:15
  • Then if I couldn't figure it out on my own, I'll post another question because right now I'm having difficulties doing that! Thanks again. – Mehran Aug 17 '16 at 01:17
  • Done it! :) Could you also please write the same query using Java API? `QueryBuilders.queryStringQuery(String)` seems different from the syntax that you've used. – Mehran Aug 17 '16 at 01:21