Searching for fields in main documents OR nested documents

I am pretty new to ES and have a problem that I could not yet solve using
the docuemntation, so any help is appreciated...

I have these douments in my index:

DocType1
{
"id" : 1,
"name" : "ABC",
"year" : 2014,
"page" : 123,
"linked_docs" : {
{
{
"id" : 2,
"name" : "DEF",
"year" : 2013,
"page" : 111
},
{
"id" : 3,
"name" : "GHI",
"year" : 2012,
"page" : 222
},
}
}
},
{
"id": 12345,
"name" : "XYZ",
"year" : 2014,
"page" : 999,
"linked_docs" : { }
}

DocType2
{
"id" : 98765,
"user" : "User 1",
"type" : "Test",
"linked_docs" : {
{
{
"id" : 1,
"name" : "ABC",
"year" : 2014,
"page" : 123
},
{
"id" : 2,
"name" : "DEF",
"year" : 2013,
"page" : 111
},
}
}
},
{
"id" : 4321,
"user" : "User 2",
"type" : "Test 2",
"linked_docs" : { }
}

Now I want to get all documents matching name = ABC, year = 2014, page =
123 either in the main document >>> OR <<< in the linked_docs.

So with the data aboce the search should deliver the following documents:

  • DocType1, id: 1 (matches in main document)
  • DocType2, id: 98765 (matches in linked docs)

I tried with nested query but I am not getting the result as above.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/7a92ccfd-3df4-47a4-93bf-dd08a7eb8fb9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hi,
This query should works for you:
{
"query": {
"bool": {
"should": [
{
"query_string": {
"query": "ABC 2014 123",
"default_operator": "AND",
"fields": [
"name",
"year",
"page"
]
}
},
{
"nested": {
"path": "linked_docs",
"query": {
"query_string": {
"query": "ABC 2014 123",
"default_operator": "AND",
"fields": [
"linked_docs.name",
"linked_docs.year",
"linked_docs.page"
]
}
}
}
}
]
}
}
}

Cheers, Ramy

Am Montag, 13. Oktober 2014 12:24:42 UTC+2 schrieb Michael Werner:

I am pretty new to ES and have a problem that I could not yet solve using
the docuemntation, so any help is appreciated...

I have these douments in my index:

DocType1
{
"id" : 1,
"name" : "ABC",
"year" : 2014,
"page" : 123,
"linked_docs" : {
{
{
"id" : 2,
"name" : "DEF",
"year" : 2013,
"page" : 111
},
{
"id" : 3,
"name" : "GHI",
"year" : 2012,
"page" : 222
},
}
}
},
{
"id": 12345,
"name" : "XYZ",
"year" : 2014,
"page" : 999,
"linked_docs" : { }
}

DocType2
{
"id" : 98765,
"user" : "User 1",
"type" : "Test",
"linked_docs" : {
{
{
"id" : 1,
"name" : "ABC",
"year" : 2014,
"page" : 123
},
{
"id" : 2,
"name" : "DEF",
"year" : 2013,
"page" : 111
},
}
}
},
{
"id" : 4321,
"user" : "User 2",
"type" : "Test 2",
"linked_docs" : { }
}

Now I want to get all documents matching name = ABC, year = 2014, page =
123 either in the main document >>> OR <<< in the linked_docs.

So with the data aboce the search should deliver the following documents:

  • DocType1, id: 1 (matches in main document)
  • DocType2, id: 98765 (matches in linked docs)

I tried with nested query but I am not getting the result as above.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/a701b8f1-92ab-4186-9c98-d4995e5f653b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

You're great!

I have been trying for several hours... as far as I can tell right now,
your solution is delivering correct results.

Thank you very much!

Am Montag, 13. Oktober 2014 12:24:42 UTC+2 schrieb Michael Werner:

I am pretty new to ES and have a problem that I could not yet solve using
the docuemntation, so any help is appreciated...

I have these douments in my index:

DocType1
{
"id" : 1,
"name" : "ABC",
"year" : 2014,
"page" : 123,
"linked_docs" : {
{
{
"id" : 2,
"name" : "DEF",
"year" : 2013,
"page" : 111
},
{
"id" : 3,
"name" : "GHI",
"year" : 2012,
"page" : 222
},
}
}
},
{
"id": 12345,
"name" : "XYZ",
"year" : 2014,
"page" : 999,
"linked_docs" : { }
}

DocType2
{
"id" : 98765,
"user" : "User 1",
"type" : "Test",
"linked_docs" : {
{
{
"id" : 1,
"name" : "ABC",
"year" : 2014,
"page" : 123
},
{
"id" : 2,
"name" : "DEF",
"year" : 2013,
"page" : 111
},
}
}
},
{
"id" : 4321,
"user" : "User 2",
"type" : "Test 2",
"linked_docs" : { }
}

Now I want to get all documents matching name = ABC, year = 2014, page =
123 either in the main document >>> OR <<< in the linked_docs.

So with the data aboce the search should deliver the following documents:

  • DocType1, id: 1 (matches in main document)
  • DocType2, id: 98765 (matches in linked docs)

I tried with nested query but I am not getting the result as above.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/9c99d4b9-b3f3-4242-85f6-d387a1f2a231%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

You're great!

I have been trying for several hours without success... as far as I can
tell from a few tests so far your solution is delivering the correct
results.

Thank you very much!

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/429a8757-2d70-4653-a4ec-09a873da646f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.