Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch filter results excluding by id

I need to return results that do not include docs with certain ids. Elasticsearch allows us to specify which ids are allowed but I see no way to disallow certain ids. In my case I want to not return things the user has already seen so the list will be different for each user.

like image 596
pferrel Avatar asked Sep 18 '15 20:09

pferrel


1 Answers

You can achieve this by adding a bool/must_not filter containing an ids filter with an array of ids you don't want to appear, like this:

{
  "query": {
    "bool": {
      "must": [
         ...                    <--- your other filters go here
      ],
      "must_not": [
        {
          "ids": {
            "values": [
              "id1", "id2"      <--- add all the ids you DON'T want in here
            ]
          }
        }
      ]
    }
  }
}
like image 147
Val Avatar answered Nov 15 '22 11:11

Val