Filters are not considered while calculating facets


(canavar) #1

Hi,

I am trying to create a date_histogram facet. While calculating the values,
ES considers only the results of the query but not the filters I provided.
Is this the expected behavior. The reason for I am using filters is to be
able to use scripts. Can I use scripts in queries? Below is the Java code.
Thanks for any help.

                .prepareSearch("sessions")

                .setQuery(QueryBuilders.fieldQuery("username", username

))

                .setFilter(FilterBuilders

.andFilter(FilterBuilders.scriptFilter("doc['date'].date.monthOfYear ==
month").addParam("month", month),

FilterBuilders.scriptFilter("doc['date'].date.year == year").addParam("year",
year)))

                .setFacets(jsonBuilder().startObject().field("histo1"

).startObject().field("date_histogram").startObject().field("field", "date")

                                   .field("interval", "day"

).endObject().endObject().endObject()).execute().actionGet();


(egaumer) #2

You need to apply your filters to the query using a filteredQuery.
Something more along the lines of...

    filteredQuery(
        fieldQuery("username", username),        
        andFilter(
            scriptFilter("doc['date'].date.monthOfYear == month")
                .addParam("month", month),
            scriptFilter("doc['date'].date.year == year")
                .addParam("year", year)
        )
    ) 

See:
http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query.html
http://www.elasticsearch.org/guide/reference/java-api/query-dsl.html

On Saturday, March 17, 2012 6:46:26 AM UTC-4, canavar wrote:

Hi,

I am trying to create a date_histogram facet. While calculating the
values, ES considers only the results of the query but not the filters I
provided. Is this the expected behavior. The reason for I am using filters
is to be able to use scripts. Can I use scripts in queries? Below is the
Java code. Thanks for any help.

                .prepareSearch("sessions")

                .setQuery(QueryBuilders.fieldQuery("username", 

username))

                .setFilter(FilterBuilders

.andFilter(FilterBuilders.scriptFilter("doc['date'].date.monthOfYear ==
month").addParam("month", month),

FilterBuilders.scriptFilter("doc['date'].date.year == year").addParam(
"year", year)))

                .setFacets(jsonBuilder().startObject().field("histo1"

).startObject().field("date_histogram").startObject().field("field",
"date")

                                   .field("interval", "day"

).endObject().endObject().endObject()).execute().actionGet();


(canavar) #3

Thanks. I actually tried that but no results returned. I will try it again.
17 Mar 2012 13:27 tarihinde "egaumer" egaumer@gmail.com yazdı:

You need to apply your filters to the query using a filteredQuery.
Something more along the lines of...

    filteredQuery(
        fieldQuery("username", username),
        andFilter(
            scriptFilter("doc['date'].date.monthOfYear == month")
                .addParam("month", month),
            scriptFilter("doc['date'].date.year == year")
                .addParam("year", year)
        )
    )

See:
http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query.html

http://www.elasticsearch.org/guide/reference/java-api/query-dsl.html

On Saturday, March 17, 2012 6:46:26 AM UTC-4, canavar wrote:

Hi,

I am trying to create a date_histogram facet. While calculating the
values, ES considers only the results of the query but not the filters I
provided. Is this the expected behavior. The reason for I am using filters
is to be able to use scripts. Can I use scripts in queries? Below is the
Java code. Thanks for any help.

                .prepareSearch("sessions")

                .setQuery(QueryBuilders.**fieldQuery("username",

username))

                .setFilter(FilterBuilders

                                   .andFilter(FilterBuilders.**

scriptFilter("doc['date'].**date.monthOfYear == month").addParam("month",
month),

FilterBuilders.scriptFilter("**doc['date'].date.year == year").addParam(
"year", year)))

                .setFacets(jsonBuilder().**startObject().field(

"histo1").**startObject().field("date_histogram").startObject().
field("field", "date")

                                   .field("interval", "day"

).endObject().endObject()**.endObject()).execute().**actionGet();


(Shay Banon) #4

Adding a filter on the actual search request will not apply to facets
(intentionally), more info here:
http://www.elasticsearch.org/guide/reference/api/search/filter.html.

On Sat, Mar 17, 2012 at 1:33 PM, canavar fehmican.saglam@gmail.com wrote:

Thanks. I actually tried that but no results returned. I will try it again.
17 Mar 2012 13:27 tarihinde "egaumer" egaumer@gmail.com yazdı:

You need to apply your filters to the query using a filteredQuery.

Something more along the lines of...

    filteredQuery(
        fieldQuery("username", username),
        andFilter(
            scriptFilter("doc['date'].date.monthOfYear == month")
                .addParam("month", month),
            scriptFilter("doc['date'].date.year == year")
                .addParam("year", year)
        )
    )

See:
http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query.html

http://www.elasticsearch.org/guide/reference/java-api/query-dsl.html

On Saturday, March 17, 2012 6:46:26 AM UTC-4, canavar wrote:

Hi,

I am trying to create a date_histogram facet. While calculating the
values, ES considers only the results of the query but not the filters I
provided. Is this the expected behavior. The reason for I am using filters
is to be able to use scripts. Can I use scripts in queries? Below is the
Java code. Thanks for any help.

                .prepareSearch("sessions")

                .setQuery(QueryBuilders.**fieldQuery("username",

username))

                .setFilter(FilterBuilders

                                   .andFilter(FilterBuilders.**

scriptFilter("doc['date'].**date.monthOfYear == month").addParam("month",
month),

FilterBuilders.scriptFilter("**doc['date'].date.year == year").addParam(
"year", year)))

                .setFacets(jsonBuilder().**startObject().field(

"histo1").**startObject().field("date_histogram").startObject().
field("field", "date")

                                   .field("interval", "day"

).endObject().endObject()**.endObject()).execute().**actionGet();


(canavar) #5

Thanks egaumer and Shay. It just worked with a filteredQuery.
20 Mar 2012 11:48 tarihinde "Shay Banon" kimchy@gmail.com yazdı:

Adding a filter on the actual search request will not apply to facets
(intentionally), more info here:
http://www.elasticsearch.org/guide/reference/api/search/filter.html.

On Sat, Mar 17, 2012 at 1:33 PM, canavar fehmican.saglam@gmail.comwrote:

Thanks. I actually tried that but no results returned. I will try it
again.
17 Mar 2012 13:27 tarihinde "egaumer" egaumer@gmail.com yazdı:

You need to apply your filters to the query using a filteredQuery.

Something more along the lines of...

    filteredQuery(
        fieldQuery("username", username),
        andFilter(
            scriptFilter("doc['date'].date.monthOfYear == month")
                .addParam("month", month),
            scriptFilter("doc['date'].date.year == year")
                .addParam("year", year)
        )
    )

See:

http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query.html

http://www.elasticsearch.org/guide/reference/java-api/query-dsl.html

On Saturday, March 17, 2012 6:46:26 AM UTC-4, canavar wrote:

Hi,

I am trying to create a date_histogram facet. While calculating the
values, ES considers only the results of the query but not the filters I
provided. Is this the expected behavior. The reason for I am using filters
is to be able to use scripts. Can I use scripts in queries? Below is the
Java code. Thanks for any help.

                .prepareSearch("sessions")

                .setQuery(QueryBuilders.**fieldQuery("username",

username))

                .setFilter(FilterBuilders

                                   .andFilter(FilterBuilders.**

scriptFilter("doc['date'].**date.monthOfYear == month").addParam(
"month", month),

FilterBuilders.scriptFilter("**doc['date'].date.year == year"
).addParam("year", year)))

                .setFacets(jsonBuilder().**startObject().field(

"histo1").**startObject().field("date_histogram").startObject().
field("field", "date")

                                   .field("interval", "day"

).endObject().endObject()**.endObject()).execute().**actionGet();


(system) #6