Filters are not considered while calculating facets

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();

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:

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();

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:
Elasticsearch Platform — Find real-time answers at scale | Elastic

Elasticsearch Platform — Find real-time answers at scale | Elastic

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();

Adding a filter on the actual search request will not apply to facets
(intentionally), more info here:
Elasticsearch Platform — Find real-time answers at scale | Elastic.

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:
Elasticsearch Platform — Find real-time answers at scale | Elastic

Elasticsearch Platform — Find real-time answers at scale | Elastic

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();

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:
Elasticsearch Platform — Find real-time answers at scale | Elastic.

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:

Elasticsearch Platform — Find real-time answers at scale | Elastic

Elasticsearch Platform — Find real-time answers at scale | Elastic

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();