Building a query filtered query


(Mattias Pfeiffer) #1

Hi,

I'm having some trouble generating a query with a filter, and after
hours trying to get it working I'm resorting to the mailing list.

I have a bunch of documents indexed in the form of:

"user": {
"username": "John Doe",
"description": "John is one of our best customers",
"age": 50,
"has_image": true,
"tags":
"primary": ["accountant", "rich", "bald"],
"secondary": ["jetsetter", "friendly"]
}

Now I want to construct a query based on:

Query:

  • "John" found in either "username" or "description"

Filters:

  • "age" should be within 20 or 60
  • "has_image" should be true
  • "tags" should include "jetsetter"
  • "tags"."primary" should have both "accountant" and "bald"

I can't seem to find a way to include all of these, and the error
messages from ES are hard to understand (to say the least).

Could somebody point me in the right direction?

Thanks!


(Shay Banon) #2

Hi,

Here is the suggested query, haven't run it, so ping if its not working:

{
"query" : {
"filtered" : {
"query" : {
"query_string" : {
"query": "username:John description:John"
}
},
"filter" : {
"and" : [
{
"range" : {
"age" : { "from" : 20, "to" : 60 }
},
},
{
"term" : {
"has_image" : true
}
},
{
"or" : [
{
"term" : {
"tags.primary" : "jetsetter"
}
},
{
"term" : {
"tags.secondary" : "jetsetter"
}
}
]
},
{
"term" : {
"tags.primary" : "accountant"
}
},
{
"term" : {
"tags.primary" : "bald"
}
}
]
}
}
}
}

On Mon, Nov 8, 2010 at 9:43 AM, Mattias Pfeiffer <mattias.pfeiffer@gmail.com

wrote:

Hi,

I'm having some trouble generating a query with a filter, and after
hours trying to get it working I'm resorting to the mailing list.

I have a bunch of documents indexed in the form of:

"user": {
"username": "John Doe",
"description": "John is one of our best customers",
"age": 50,
"has_image": true,
"tags":
"primary": ["accountant", "rich", "bald"],
"secondary": ["jetsetter", "friendly"]
}

Now I want to construct a query based on:

Query:

  • "John" found in either "username" or "description"

Filters:

  • "age" should be within 20 or 60
  • "has_image" should be true
  • "tags" should include "jetsetter"
  • "tags"."primary" should have both "accountant" and "bald"

I can't seem to find a way to include all of these, and the error
messages from ES are hard to understand (to say the least).

Could somebody point me in the right direction?

Thanks!


(system) #3