经过多次演讲后,我无法说出Elasticsearch是否可以进行这种查询,我发现“入门”确实很棒,但是其余指南缺少示例(从我的观点出发)。

请参阅下面的结构,我需要检索不在黑名单中的所有ID。我的黑名单是一些引用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)

我想避免两步查询,如果我的架构不好,应该重新考虑,请向我公开征求意见。

这是结构:
{
    "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 lookupnot 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

10-05 22:15