Filtered has_child query?

Is a filtered has_child query possible where the filter is applied to the
child document before the query of the has_child?

e.g. Given the example below...

curl -X PUT "http://localhost:9200/foobar" -d
"{"mappings":{"Foo":{},"Bar":{"_parent":{"type":"Foo"}}}}"

curl -X PUT "http://localhost:9200/foobar/Foo/1"
-d "{"foo":"abc","access":"yes"}"

curl -X PUT "http://localhost:9200/foobar/Bar/2?parent=1"
-d "{"bar":"xyz","access":"no"}"

The following filtered query should not return a result, if the filter was
being applied to the child document first.

{
"query": {
"filtered": {
"query": {
"has_child": {
"type": "Bar",
"query": {
"term": {
"bar": "xyz"
}
}
}
},
"filter": {
"term": {
"access": "yes"
}
}
}
}
}

Please advise.

--
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/575c3aa3-169d-4f00-b353-9e54291da432%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

The query as written will return a result because you are querying for
Parent documents that 'have children' matching your has_child query. You
can tell because the type in the url will be 'Foo'.

Hence, the filter you have specified is not run against the children, but
against the parents. In your example the parent document does indeed have
a access:yes field and does not get filtered out.

It is probably possible to do what you want, but it depends on whether you
are trying to retrieve parent or child documents.

To get parent documents, just add your filter criteria to the has_child
query.

To get child documents, use 'Bar' in the url and take a look into the
has_parent filter/query

On Wed, Mar 11, 2015 at 10:23 AM, asanderson a.steven.anderson@gmail.com
wrote:

Is a filtered has_child query possible where the filter is applied to the
child document before the query of the has_child?

e.g. Given the example below...

curl -X PUT "http://localhost:9200/foobar" -d
"{"mappings":{"Foo":{},"Bar":{"_parent":{"type":"Foo"}}}}"

curl -X PUT "http://localhost:9200/foobar/Foo/1"
-d "{"foo":"abc","access":"yes"}"

curl -X PUT "http://localhost:9200/foobar/Bar/2?parent=1"
-d "{"bar":"xyz","access":"no"}"

The following filtered query should not return a result, if the filter was
being applied to the child document first.

{
"query": {
"filtered": {
"query": {
"has_child": {
"type": "Bar",
"query": {
"term": {
"bar": "xyz"
}
}
}
},
"filter": {
"term": {
"access": "yes"
}
}
}
}
}

Please advise.

--
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/575c3aa3-169d-4f00-b353-9e54291da432%40googlegroups.com
https://groups.google.com/d/msgid/elasticsearch/575c3aa3-169d-4f00-b353-9e54291da432%40googlegroups.com?utm_medium=email&utm_source=footer
.
For more options, visit https://groups.google.com/d/optout.

--
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/CAFps6aCsz%2B%2B%3DduMCZcR--T9s2eczn0Yq1jCftma9ae0KdaR%3Ddw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Actually, I do want only parent documents returned, but I want the filter to be applied to both parent and child documents. Is there a way to specify that the filter is to be applied before the query, so that this would be possible? If not, how would I rewrite the query to do this?

--
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/05a86e8c-9ef2-4028-b937-e6370202e677%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

'apply the filter before the query' doesn't make any sense to me - what
would it filter? I suspect I'm not really understanding you, can you tell
me more? Why do you want to be able to do this? How would it help?

anyway, from what I thing I do understand there are several ways to get the
results you want. Which one you choose probably depends on how you want the
results scored, and possibly performance considerations.

here is one way to try. (If you want to filter both then you need to ...
apply the filter to both :))
something like:

{
"query": {
"filtered": {
"query": {
"has_child": {
"type": "Bar",
"query": {
"filtered": {
"query": {
"term": {
"bar": "xyz"
}
},
"filter": {
"term": {
"access": "yes"
}
}
}
}
}
},
"filter": {
"term": {
"access": "yes"
}
}
}
}
}

On Wed, Mar 11, 2015 at 9:15 PM, asanderson a.steven.anderson@gmail.com
wrote:

Actually, I do want only parent documents returned, but I want the filter
to be applied to both parent and child documents. Is there a way to specify
that the filter is to be applied before the query, so that this would be
possible? If not, how would I rewrite the query to do this?

--
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/05a86e8c-9ef2-4028-b937-e6370202e677%40googlegroups.com
.
For more options, visit https://groups.google.com/d/optout.

--
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/CAFps6aBXHyr67vCxHb6BYV42y6RS9QCzGQK4xKb6Qmh8jph8pA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

On Wednesday, March 11, 2015 at 6:23:40 PM UTC-4, Perryn Fowler wrote:

'apply the filter before the query' doesn't make any sense to me - what
would it filter? I suspect I'm not really understanding you, can you tell
me more? Why do you want to be able to do this? How would it help?

The scenario is actually a very common one. We have a common field to all
document types (regardless of parent or child) whose purpose is to control
access. In this simple example, the access field represents this common
field. Ideally, it would be much simpler to be able to specify a filter
then query
strategy in the filtered query which would apply the term
filter to all documents before the query is executed regardless of whether
or not the query included a has_parent query.

anyway, from what I thing I do understand there are several ways to get
the results you want. Which one you choose probably depends on how you want
the results scored, and possibly performance considerations.

here is one way to try. (If you want to filter both then you need to ...
apply the filter to both :))
something like:

{
"query": {
"filtered": {
"query": {
"has_child": {
"type": "Bar",
"query": {
"filtered": {
"query": {
"term": {
"bar": "xyz"
}
},
"filter": {
"term": {
"access": "yes"
}
}
}
}
}
},
"filter": {
"term": {
"access": "yes"
}
}
}
}
}

Thank you for your work-around solution. Unfortunately, it's not ideal
though, since in practice our filters can be very large, so it really
bloats the request to have to repeat the filter clause.

Thank you for your time though.

--
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/7af03ba4-9654-49f3-98f0-b40ca297a56f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.