Re: Filter a query without a term


(Shay Banon) #1

Can you post a recreation (http://www.elasticsearch.org/help)? For filter, you should use a term filter for this, not a query wrapping filter.
On Tuesday, March 15, 2011 at 2:07 AM, Rob Faraj wrote:

I'm still relatively new to ElasticSearch and can't seem to figure out
if I'm doing this correctly. I am trying to produce a query that
searches for a given string. In addition to this query I also want to
filter the results off of an integer field in the document. I am using
the request below and its working...

curl -XPOST 'http://localhost:9200/index/type/_search?
pretty=true&fields=_id,item_id' -d '
{
"query" : {
"query_string" : {
"query" : "foobar"
}
},
"filter" : {
"query" : {
"field": {
"status_id" : "1"
}
}
}
}
'

Sometimes there is no term provided, but I would still like to filter
the results. I have substituted the term (foobar) for "*" which I
guess matches everything. Is this a good approach for what I'm trying
to accomplish?

Thanks in advance.


(Clinton Gormley) #2

Hi Rob

https://gist.github.com/e251f1d3f0e17c2fa7ae

I've posted a comment to your gist:

For the first query with a query string, you should use a filtered
query, rather than using the top level filter param.

For the second query, without a query string, you can just wrap your
filter in a constant_score query and use it as shown

clint


(Shay Banon) #3

You are putting the facets within the "query", it should eb on the same level as the "query" (json wise).
On Wednesday, March 16, 2011 at 12:10 AM, Rob Faraj wrote:

Thanks Clint. The constant_score query worked great when there is no
term and I would like to filter by one field. However, I can't seem to
get facets to work with the constant_score query.

I've updated the gist to be a reproduction of this (see the last curl
request). The odd thing is that the response comes back fine, but
there is no facet information in the response even though it is part
of the query object sent.

On Mar 15, 11:11 am, Clinton Gormley clin...@iannounce.co.uk wrote:

Hi Rob

https://gist.github.com/e251f1d3f0e17c2fa7ae

I've posted a comment to your gist:

https://gist.github.com/e251f1d3f0e17c2fa7ae#comments

For the first query with a query string, you should use a filtered
query, rather than using the top level filter param.

For the second query, without a query string, you can just wrap your
filter in a constant_score query and use it as shown

clint


(Clinton Gormley) #4

Hi Rob

Would someone mind taking a glance over the two different JSON docs I
have put together? They work as I have tested them, but I want to make
sure I'm doing things as simplistically efficient as possible.

https://gist.github.com/f8a124e09faf61cff35b

I've added comments showing how to make these queries more efficient. I
left out the facets for simplicity.

clint


(Shay Banon) #5

I approve Clinton enhancements :). One note, you don't need to specify in the fields _id, since you always get it. Just provide an empty array in the fields, and it will not return the _source.
On Wednesday, March 16, 2011 at 9:57 PM, Clinton Gormley wrote:

Hi Rob

Would someone mind taking a glance over the two different JSON docs I
have put together? They work as I have tested them, but I want to make
sure I'm doing things as simplistically efficient as possible.

https://gist.github.com/f8a124e09faf61cff35b

I've added comments showing how to make these queries more efficient. I
left out the facets for simplicity.

clint


(Shay Banon) #6

Those two queries are exactly the same. The field query is just a query_string query with a nicer syntax and the default_field is set to the field provided.
On Wednesday, March 16, 2011 at 10:46 PM, Rob Faraj wrote:

Thanks again. The 'terms' filter is exactly what I was looking for.
The support here is really fast and helpful. Speaks volumes to the
long term forecast of elasticsearch. Kudos to both.

I posted a follow up on gist with a question about the difference
between

"query" : {
"query_string" : {
"query" : "foobar"
}
}

AND

"query" : {
"field" : {
"_all" : "foobar"
}
}

On Mar 16, 3:57 pm, Clinton Gormley clin...@iannounce.co.uk wrote:

Hi Rob

Would someone mind taking a glance over the two different JSON docs I
have put together? They work as I have tested them, but I want to make
sure I'm doing things as simplistically efficient as possible.

https://gist.github.com/f8a124e09faf61cff35b

I've added comments showing how to make these queries more efficient. I
left out the facets for simplicity.

clint


(system) #7