Find all IDs where ID is not on my blacklist

After many lectures I can't tell if such a query is possible with elasticsearch, I found the "getting started" really excellent, but the rest of the tutorial has no examples (from my vue point).

See my structure below, I need to get all ids that are not on my blacklist. My blacklist is the link id. For this example, i is id 1 with the first name "i". Here in the structure we can see that I am blacklisted "bob", so bob id (2) is on my blacklist because I don't want to find bob in the search results. :)

Is it only possible to recover (with confidence dynamically) the entire ID that is not on my blacklist in a single request? If you come from SQL, the same logic could be:

SELECT id FROM index WHERE id NOT IN (SELECT * FROM blacklist WHERE id = 1)

      

I would like to avoid asking with two steps, if my schematic is bad and needs to be revised, please, I am completely open to consultation or suggestions.

Here is the structure:

{
    "id: 1,
    "balance": 16623,
    "firstname": "me",
    "blacklist" : [2,1982,939,1982,98716,7611,983838, and thousands others ....],

}
{
    "id: 2,
    "balance": 16623,
    "firstname": "bob,
    "blacklist" : [18,1982,939,1982,98716,7611,983838, and thousands others ....],

}
{
    "id: 3,
    "balance": 16623,
    "firstname": "jhon",
    "blacklist" : [18,1982,939,1982,98716,7611,983838, and thousands others ....],

}

      

+3


source to share


1 answer


You can use search term filters together with not filter as follows.

I have set up an index with the three documents you listed:

DELETE /test_index

PUT /test_index

PUT /test_index/doc/1
{
    "id": 1,
    "balance": 16623,
    "firstname": "me",
    "blacklist" : [2,1982,939,1982,98716,7611,983838]
}
PUT /test_index/doc/2
{
    "id": 2,
    "balance": 16623,
    "firstname": "bob",
    "blacklist" : [18,1982,939,1982,98716,7611,983838]
}
PUT /test_index/doc/3
{
    "id": 3,
    "balance": 16623,
    "firstname": "john",
    "blacklist" : [18,1982,939,1982,98716,7611,983838]
}

      

Then set up a query that filters out documents that are blacklisted for "me"

:

POST /test_index/doc/_search
{
   "filter": {
      "not": {
         "filter": {
            "terms": {
               "id": {
                  "index": "test_index",
                  "type": "doc",
                  "id": "1",
                  "path": "blacklist"
               }
            }
         }
      }
   }
}
...
{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 1,
      "hits": [
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "1",
            "_score": 1,
            "_source": {
               "id": 1,
               "balance": 16623,
               "firstname": "me",
               "blacklist": [2,1982,939,1982,98716,7611,983838]
            }
         },
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "3",
            "_score": 1,
            "_source": {
               "id": 3,
               "balance": 16623,
               "firstname": "john",
               "blacklist": [18,1982,939,1982,98716,7611,983838]
            }
         }
      ]
   }
}

      



If you also want to filter the user whose blacklist is in use, you can set up a slightly more complex filter using or

POST /test_index/doc/_search
{
   "filter": {
      "not": {
         "filter": {
            "or": {
               "filters": [
                  {
                     "terms": {
                        "id": {
                           "index": "test_index",
                           "type": "doc",
                           "id": "1",
                           "path": "blacklist"
                        }
                     }
                  },
                  {
                     "term": {
                        "id": "1"
                     }
                  }
               ]
            }
         }
      }
   }
}
...
{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "3",
            "_score": 1,
            "_source": {
               "id": 3,
               "balance": 16623,
               "firstname": "john",
               "blacklist": [18,1982,939,1982,98716,7611,983838]
            }
         }
      ]
   }
}

      

Here is the code I used:

http://sense.qbox.io/gist/0b6808414f9447d4f7d23eb4c0d3e937ec2ea4e7

0


source







All Articles