0

So I am attempting to use a bool query that checks a user's id against other user's blacklist, as well as removing users that a user has blacklisted in the results.

Each query works fine on its own, but not together. I was under the impression from the elasticsearch documentation that inside a bool query I could chain them with commas.

If someone could tell me where I'm going wrong, it'd be much appreciated.

client.search({
  index: 'firebase',
  type: 'user',
  size: 500,
  body: {
    "query": {
       "filtered" : {
           "query" : {
               "bool" : {
                   "must_not" : {
                     "terms": { "id": user.blacklist},
                     "term": { "blacklist": user.id}
                   }
               }
             },
             "filter": {
                "and": query
             }

       }
 }

}
}).then(function (resp) {
    var hits = resp.hits.hits;
    res.send(hits);

    console.log(hits);
}, function (err) {
    console.log(err);
    res.sendStatus(500);
});

1 Answer 1

2

You're almost there. Your must_not query should be an array of objects, instead of just an object:

"bool" : {
  "must_not" : [
    {"terms": { "id": user.blacklist}},
    {"term": { "blacklist": user.id}}
  ]
}

Don't forget the extra brackets around each of the term(s) searches.

In the ElasticSearch documentation, notice how the should clause in the example query is an array instead of an object since it has more than one sub-clause.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks man...one question though. user.blacklist is an array of id's, shouldn't that be a terms search in order to iterate through it to match against id?
You are right, terms in the way to go if you want to match multiple values.
Oops, you're right. I should have picked up on the fact that blacklist was an array and not a single value. I'll edit my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.