gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 12:18:23 24 4
gpt4 key购买 nike

经过很多讲座,我不能说这种查询是否可以用 elasticsearch 实现,我发现“入门”非常好,但指南的其余部分缺乏示例(从我的角度来看 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)

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

结构如下:

{
"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连同 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

关于elasticsearch - 查找 ID 不在我的黑名单中的所有 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27878254/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com