As part of my work, I needed to exclude a combination of attributes from the search results
a combination was needed because I use a combined primary key in my work
and then I ran into a problem that I can’t understand
Below I will show a series of queries to the test index
create table test(a string, b string);
insert into test (a, b) values ('0', '0');
insert into test (a, b) values ('0', '1');
insert into test (a, b) values ('1', '0');
insert into test (a, b) values ('1', '1');
Let’s imagine that I need the search result to not include a=1, b=1
the first attempt that I considered obvious was this
{
"index": "test",
"query": {
"bool": {
"must_not": [
{
"equals": {
"a": "1"
}
},
{
"equals": {
"b": "1"
}
}
]
}
}
}
--------------------------------------------
"hits": [
{
"_id": "7208260601569607687",
"_score": 1,
"_source": {
"a": "0",
"b": "0"
}
}
]
Quite quickly I realized that for some reason - when using must_not, “or” is placed between the conditions and not “and”, which is almost directly stated in the documentation.
then I decided to use a nested condition, since I can’t get by with the option above.
not(a and b)
{
"index": "test",
"query": {
"bool": {
"must_not": [
{
"bool": {
"must": [
{
"equals": {
"a": "1"
}
},
{
"equals": {
"b": "1"
}
}
]
}
}
]
}
}
}
----------------------------------------------------------------------------
"hits": [
{
"_id": "7208260601569607687",
"_score": 1,
"_source": {
"a": "0",
"b": "0"
}
}
]
but for some reason the answer turned out to be as if “must” works like “should”.
on the 3rd attempt I finally managed to get the result I needed, but I completely stopped understanding how “must_not” works.
not(a or b)???
{
"index": "test",
"query": {
"bool": {
"must_not": [
{
"bool": {
"should": [
{
"equals": {
"a": "1"
}
},
{
"equals": {
"b": "1"
}
}
]
}
}
]
}
}
}
-------------------------------------------------------
"hits": [
{
"_id": "7208260601569607687",
"_score": 1,
"_source": {
"a": "0",
"b": "0"
}
},
{
"_id": "7208260601569607688",
"_score": 1,
"_source": {
"a": "0",
"b": "1"
}
},
{
"_id": "7208260601569607686",
"_score": 1,
"_source": {
"a": "1",
"b": "0"
}
}
]
and the actual question
Am I not understanding how it should work or is this a bug?
and if I don’t understand, could you please explain?