K.B. Choi
K.B. Choi

Reputation: 13

Elasticsearch - Bool query failed to parse search

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);
});

Upvotes: 0

Views: 397

Answers (1)

Jeremy
Jeremy

Reputation: 1924

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.

Upvotes: 2

Related Questions