查找 ID 不在我的黑名单中的所有 ID

Find all ID where ID are not in my blacklist

经过多次讲座,我不能说这种查询是否可以通过 elasticsearch 实现,我发现 "getting started" 非常出色,但指南的其余部分缺乏示例(从我的角度来看 vue )。

看下面我的结构,我需要检索所有不在我的黑名单中的id。我的黑名单是一些参考编号。对于这个例子,我是 id 1,名字是 "me" 。在结构中我们看到我将 "bob" 列入黑名单,所以 bob id (2) 在我的黑名单数组中,因为我不想在我的搜索结果中找到 bob..:)

是否可以在一次查询中只检索(肯定是动态地)所有不在我的黑名单中的 ID? 如果您来自 SQL,则相同的逻辑可能是:

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

我想避免 2 步查询,如果我的架构不好并且应该重新考虑,请我完全接受建议。

结构如下:

{
    "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 ....],

}

您可以按如下方式使用 terms filter lookup together with a not filter

我用你列出的三个文档建立了索引:

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]
}

然后设置一个查询,过滤掉 "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]
            }
         }
      ]
   }
}

如果您还想过滤掉正在使用其黑名单的用户,您可以使用 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]
            }
         }
      ]
   }
}

这是我使用的代码:

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