Facet filters using nested objects


(Marcin Dojwa) #1

Hi,

I have the following problem, please check out

  1. query_ok.js - executes OK and this is OK
  2. query_failed.js - this returns empty set because there is exists filter
    in facet_filter that checks testobj.idtest which is not in nested object
    (testobj.obj1).

So the question is: how to filter documents on any document fields (here:
testobj.idtest) using facets on field from nested object (here:
testobj.obj1.date)?

Thanks.
Best regards
Marcin.


(Marcin Dojwa) #2

Anyone ? :slight_smile:

2012/5/23 Marcin Dojwa m.dojwa@livechatinc.com

Hi,

I have the following problem, please check out
https://gist.github.com/d3234db747e6e845d1e5

  1. query_ok.js - executes OK and this is OK
  2. query_failed.js - this returns empty set because there is exists filter
    in facet_filter that checks testobj.idtest which is not in nested object
    (testobj.obj1).

So the question is: how to filter documents on any document fields (here:
testobj.idtest) using facets on field from nested object (here:
testobj.obj1.date)?

Thanks.
Best regards
Marcin.


(Clinton Gormley) #3

Hi Marcin

I don't see anything obviously wrong, but it is better to gist actual
curl statements that allow people to copy-and-paste in order to test
locally, otherwise, speaking for myself, I don't have the time :slight_smile:

clint

2012/5/23 Marcin Dojwa m.dojwa@livechatinc.com
Hi,

    I have the following problem, please check
    out https://gist.github.com/d3234db747e6e845d1e5
    1. query_ok.js - executes OK and this is OK
    2. query_failed.js - this returns empty set because there is
    exists filter in facet_filter that checks testobj.idtest which
    is not in nested object (testobj.obj1).
    
    
    So the question is: how to filter documents on any document
    fields (here: testobj.idtest) using facets on field from
    nested object (here: testobj.obj1.date)?
    
    
    Thanks.
    Best regards
    Marcin.

(benjamin leviant) #4

Hello,

why not this :

{
"facets": {
"facet1": {
"date_histogram": {
"field": "date",
"interval": "hour"
},
"facet_filter": {
"and": [
{
"exists": {
"field": "testobj.idtest"
}
},
{
"nested": {
"path": "testobj.obj1",
"query": {
"range": {
"testobj.obj1.date": {
"from": "2012-01-01T00:00:00Z",
"to": "2012-02-01T00:00:00Z"
}
}
}
}
}
]
}
}
}
}

I hope it can help.

Regards

Benjamin

On Fri, May 25, 2012 at 4:31 PM, Clinton Gormley clint@traveljury.comwrote:

Hi Marcin

I don't see anything obviously wrong, but it is better to gist actual
curl statements that allow people to copy-and-paste in order to test
locally, otherwise, speaking for myself, I don't have the time :slight_smile:

clint

2012/5/23 Marcin Dojwa m.dojwa@livechatinc.com
Hi,

    I have the following problem, please check
    out https://gist.github.com/d3234db747e6e845d1e5
    1. query_ok.js - executes OK and this is OK
    2. query_failed.js - this returns empty set because there is
    exists filter in facet_filter that checks testobj.idtest which
    is not in nested object (testobj.obj1).


    So the question is: how to filter documents on any document
    fields (here: testobj.idtest) using facets on field from
    nested object (here: testobj.obj1.date)?


    Thanks.
    Best regards
    Marcin.

(Marcin Dojwa) #5

Hi,

You are right, I added curl_queries.sh in gist

Best regards.

2012/5/25 Clinton Gormley clint@traveljury.com

Hi Marcin

I don't see anything obviously wrong, but it is better to gist actual
curl statements that allow people to copy-and-paste in order to test
locally, otherwise, speaking for myself, I don't have the time :slight_smile:

clint

2012/5/23 Marcin Dojwa m.dojwa@livechatinc.com
Hi,

    I have the following problem, please check
    out https://gist.github.com/d3234db747e6e845d1e5
    1. query_ok.js - executes OK and this is OK
    2. query_failed.js - this returns empty set because there is
    exists filter in facet_filter that checks testobj.idtest which
    is not in nested object (testobj.obj1).


    So the question is: how to filter documents on any document
    fields (here: testobj.idtest) using facets on field from
    nested object (here: testobj.obj1.date)?


    Thanks.
    Best regards
    Marcin.

(Clinton Gormley) #6

You are right, I added curl_queries.sh in gist
https://gist.github.com/d3234db747e6e845d1e5

Got it.

The problem is that your facet is running an a nested object:

     "nested":"testobj.obj1",

which means that it can't see field testobj.idtest

To be clear, a nested object is a SEPARATE doc internally. So either,
you need to use the 'exists' filter as part of the query, or you can
configure your main object to also store a copy of the nested object
data:

{
type: "nested",
include_in_root: true,
properties: {.... }
}

Then, instead of running your facet on the nested object, run it on the
main object (just by leaving out this line: "nested":"testobj.obj1")

Depending on your data, this may or may not suit your purposes

clint


(Marcin Dojwa) #7

Hi Benjamin,

Thank you for your help.
The one you gave does not work because testobj.obj1 is a nested object and
the date_histogram works with testobj.obj1.date field. It would work if the
facet didn't work with field of nested object.
So, any other ideas ? :slight_smile: The question is if this is possible in ES :slight_smile:

Best regards.

2012/5/25 benjamin leviant benjamin.leviant@gmail.com

Hello,

why not this :

{
"facets": {
"facet1": {
"date_histogram": {
"field": "date",
"interval": "hour"
},
"facet_filter": {
"and": [
{
"exists": {
"field": "testobj.idtest"
}
},
{
"nested": {
"path": "testobj.obj1",
"query": {
"range": {
"testobj.obj1.date": {
"from": "2012-01-01T00:00:00Z",
"to": "2012-02-01T00:00:00Z"
}
}
}
}
}
]
}
}
}
}

I hope it can help.

Regards

Benjamin

On Fri, May 25, 2012 at 4:31 PM, Clinton Gormley clint@traveljury.comwrote:

Hi Marcin

I don't see anything obviously wrong, but it is better to gist actual
curl statements that allow people to copy-and-paste in order to test
locally, otherwise, speaking for myself, I don't have the time :slight_smile:

clint

2012/5/23 Marcin Dojwa m.dojwa@livechatinc.com
Hi,

    I have the following problem, please check
    out https://gist.github.com/d3234db747e6e845d1e5
    1. query_ok.js - executes OK and this is OK
    2. query_failed.js - this returns empty set because there is
    exists filter in facet_filter that checks testobj.idtest which
    is not in nested object (testobj.obj1).


    So the question is: how to filter documents on any document
    fields (here: testobj.idtest) using facets on field from
    nested object (here: testobj.obj1.date)?


    Thanks.
    Best regards
    Marcin.

(Marcin Dojwa) #8

Hi Clinton,

Thank you,

I was afraid that these were the only one solutions. I did it the first way

  • I added exists filter to query but I had to separate two different facets
    and create two different queries instead of one query with two facets
    filtered with facet filters :slight_smile: But this works as well.
    In my case the copy of testobj.obj1 object will not work because I need to
    count the number of testobj.obj1 elements not the number of root documents
    (am I right this would count the number of documents not the number of
    testobj.obj1 elements?).
    I just wanted to know if it is possible to access root document's fields
    from the nested query. If not I will leave the solution like I said before.

Thank you all for your help.

2012/5/25 Clinton Gormley clint@traveljury.com

You are right, I added curl_queries.sh in gist
https://gist.github.com/d3234db747e6e845d1e5

Got it.

The problem is that your facet is running an a nested object:

    "nested":"testobj.obj1",

which means that it can't see field testobj.idtest

To be clear, a nested object is a SEPARATE doc internally. So either,
you need to use the 'exists' filter as part of the query, or you can
configure your main object to also store a copy of the nested object
data:

{
type: "nested",
include_in_root: true,
properties: {.... }
}

Then, instead of running your facet on the nested object, run it on the
main object (just by leaving out this line: "nested":"testobj.obj1")

Depending on your data, this may or may not suit your purposes

clint


(Clinton Gormley) #9

Hiya

I was afraid that these were the only one solutions. I did it the
first way - I added exists filter to query but I had to separate two
different facets and create two different queries instead of one query
with two facets filtered with facet filters :slight_smile: But this works as well.
In my case the copy of testobj.obj1 object will not work because I
need to count the number of testobj.obj1 elements not the number of
root documents (am I right this would count the number of documents
not the number of testobj.obj1 elements?).
I just wanted to know if it is possible to access root document's
fields from the nested query. If not I will leave the solution like I
said before.

It's not possible. But what about including a copy of that field in
your nested docs? You would have to do it manually, and it'd take more
space, but you'd achieve what you're after

clint

Thank you all for your help.

2012/5/25 Clinton Gormley clint@traveljury.com

    >
    > You are right, I added curl_queries.sh in gist
    > https://gist.github.com/d3234db747e6e845d1e5
    
    
    Got it.
    
    The problem is that your facet is running an a nested object:
    
            "nested":"testobj.obj1",
    
    which means that it can't see field testobj.idtest
    
    To be clear, a nested object is a SEPARATE doc internally.  So
    either,
    you need to use the 'exists' filter as part of the query, or
    you can
    configure your main object to also store a copy of the nested
    object
    data:
    
     {
       type: "nested",
       include_in_root: true,
       properties: {.... }
     }
    
    Then, instead of running your facet on the nested object, run
    it on the
    main object (just by leaving out this line:
    "nested":"testobj.obj1")
    
    Depending on your data, this may or may not suit your purposes
    
    clint

(Marcin Dojwa) #10

You are right :slight_smile: this solves the problem :slight_smile: Thank you very much.

Best regards.

2012/5/25 Clinton Gormley clint@traveljury.com

Hiya

I was afraid that these were the only one solutions. I did it the
first way - I added exists filter to query but I had to separate two
different facets and create two different queries instead of one query
with two facets filtered with facet filters :slight_smile: But this works as well.
In my case the copy of testobj.obj1 object will not work because I
need to count the number of testobj.obj1 elements not the number of
root documents (am I right this would count the number of documents
not the number of testobj.obj1 elements?).
I just wanted to know if it is possible to access root document's
fields from the nested query. If not I will leave the solution like I
said before.

It's not possible. But what about including a copy of that field in
your nested docs? You would have to do it manually, and it'd take more
space, but you'd achieve what you're after

clint

Thank you all for your help.

2012/5/25 Clinton Gormley clint@traveljury.com

    >
    > You are right, I added curl_queries.sh in gist
    > https://gist.github.com/d3234db747e6e845d1e5


    Got it.

    The problem is that your facet is running an a nested object:

            "nested":"testobj.obj1",

    which means that it can't see field testobj.idtest

    To be clear, a nested object is a SEPARATE doc internally.  So
    either,
    you need to use the 'exists' filter as part of the query, or
    you can
    configure your main object to also store a copy of the nested
    object
    data:

     {
       type: "nested",
       include_in_root: true,
       properties: {.... }
     }

    Then, instead of running your facet on the nested object, run
    it on the
    main object (just by leaving out this line:
    "nested":"testobj.obj1")

    Depending on your data, this may or may not suit your purposes

    clint

(system) #11