Usage of - operator in elasticsearch

41 views Asked by At

I run this query and it returns 15 products

+(+organizationId:6 +(mainGenre:dance alternateGenre:dance) +(state:pending state:published state:accepted)) +(alias:audioProduct)

So, it means that there are 15 products of organizationId = 6 AND (Main_Genre=Dance or Alternate_Genre=Dance). However, this query seen below doesn't return anything.

+(+organizationId:6 +(-mainGenre:pop -alternateGenre:pop) +(state:pending state:published state:accepted)) +(alias:audioProduct)

Now the query is like "organizationId = 6 AND Main_Genre!=Pop AND Alternate_Genre!=Pop". I think it should return at least 15 products. I mean it should return the products which were found for MainGenre:Dance.

Does anyone have any idea what I am doing wrong here?

2

There are 2 answers

0
Val On BEST ANSWER

In (-mainGenre:pop -alternateGenre:pop), the OR operator is implied, i.e. it's equivalent to

Main_Genre != pop OR Alternate_Genre != pop

while what you're after is

Main_Genre != pop AND Alternate_Genre != pop

You should try this instead (see the && operator):

+(+organizationId:6 +(-mainGenre:pop && -alternateGenre:pop) +(state:pending state:published state:accepted)) +(alias:audioProduct)
0
forzagreen On

The AND relation is a distributive property.
Let * be the AND relation, and + the OR relation, A,B and C three properties.
Then: A*(B+C) = A*B + A*C

Your statement means:
(organizationId = 6 AND Main_Genre!=Dance) OR (organizationId = 6 AND Alternate_Genre!=pop)