Query and multiple filters


(Frédéric Houbie) #1

Hello, I'm afraid the question has already been asked before, but I could not find exactly the answer.

I'm indexing documents with this mapping:

  • id
  • title
  • content
  • product
  • category

I would like to do a query with :

  1. filter on
    product in ('prodA','prodB') and category in ('catA', 'catB')
  2. query on
    content : 'interesting text to find'

I've seen that I need a query and a filter, but can't find how to use both in the same search. Could someone clarify me that ?

Thanks

Frédéric


(David Pilato) #2

Use a Bool query. It has a must clause and a filter clause.


(Frédéric Houbie) #3

Hi David,

thanks. I've tried this query

{
"query":{
"bool" : {
"must" : {
"term" : { "content" : "latitude" }
},
"filter": [{
"terms" : { "product" : ["prodA";"prodB" ]},
"terms" : { "categories" : ["guide" ]}
}]
}
}
}

What I expect is to have a query more or less like :

select * from type
where categories in ('guide') and product in ("prodA","prodB") and content
like %latitude%

It seems I'm wrong somewhere as I get results where product are equal to
prodC.

Any advice where I'm wrong ?

Thanks

Frédéric


(David Pilato) #4

Be careful with your analyzers. They will play a big role here.

For example, if you could reproduce with a full SENSE script that would be easier to help you fixing your problem.

For example, prodA might have been indexed as prodb. So the Term query you use won't match.

About the content part, are you looking for full terms or do you also want to search for %atitu%?


(Frédéric Houbie) #5

Hi,
prodA was an example, product names are more different. I'll check for
analyzers. For content, it is more a full text search %latitude%.

What do you mean by SENSE ?

Thanks

Frédéric


(David Pilato) #6

I guessed so. But still you can have issues with analyzers here.

https://www.elastic.co/guide/en/sense/master/index.html

Also look at: https://www.elastic.co/help/


(Frédéric Houbie) #7

David,

thanks,
I've installed Sense, nice apps. I've created a manual mapping with no
analyzers for the fields I want to filter on. Then, I've tries to create a
query. I'm there :
GET _search
{
"query": {
"bool": {
"must": {
"multi_match": {
"query": "decoding aixm metadata",
"fields": [
"title^3",
"content"
]
}
},
"filter": [
{
"terms": {
"categories": [
"Guide",
"API"
]
}
}
]
}
}
}

My question is now, where should I add a second filter on another field ?

Thanks

Frédéric


(David Pilato) #8

filter is an array. Add a new filter in it.


(system) #9