Elasticsearch 多索引过滤器
Elasticsearch multi index filter
我有 2 个索引,一个存储 users,一个存储 articles.
用户文档:
{
"id" : "152ce52d-e975-4ebd-849a-0a12f535e644",
"email": "bob@email.com",
...
}
索引映射:
{
"mapping": {
"properties": {
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"email": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
文章文档:
{
"articleId" : "002ce52d-e975-4ebd-849a-0a12f535a536",
"userId": "152ce52d-e975-4ebd-849a-0a12f535e644",
...
}
索引映射:
{
"mapping": {
"properties": {
"articleId": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"userId": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
我正在尝试创建一个多索引查询,returns 所有没有文章的用户。
我不知道如何从第一个索引中排除包含在第二个索引中且具有相同 id/userId 字段的项目。
假设我有以下用户和文章:
用户:
[
{
"id": "user1",
"email": "bob@email.com"
},
{
"id": "user2",
"email": "john@email.com"
},
{
"id": "user3",
"email": "tom@email.com"
},
{
"id": "user4",
"email": "ben@email.com"
}
]
文章:
[
{
"articleId": "id1",
"userId": "user1"
},
{
"articleId": "id1",
"userId": "user2"
}
]
作为查询的结果,我想要所有没有文章的用户:
[{
"id": "user3",
"email": "tom@email.com"
},
{
"id": "user4",
"email": "ben@email.com"
}]
正如@leandrojmp 所指出的,您正在尝试在 SQL 中实现类似 JOIN 查询的操作,您希望在其中 returns 来自一个索引的所有剩余记录,这些记录与来自第二个指标。请参阅此文档,以了解有关 joining queries in elasticsearch
的更多信息
I can't figure out how to exclude from the first index items contained
in the second index and having the same id/userId field.
跨多个索引执行查询时可以使用_index field
。
搜索查询:
{
"query": {
"bool": {
"should": [
{
"bool": {
"must_not": [
{
"match": {
"id": "user1"
}
},
{
"match": {
"id": "user2"
}
}
],
"must": {
"term": {
"_index": "64469393user"
}
}
}
},
{
"bool": {
"must_not": [
{
"match": {
"userId": "user1"
}
},
{
"match": {
"userId": "user2"
}
}
],
"must": {
"term": {
"_index": "64469393article"
}
}
}
}
]
}
}
}
搜索结果:
"hits": [
{
"_index": "64469393user",
"_type": "_doc",
"_id": "3",
"_score": 1.0,
"_source": {
"id": "user3",
"email": "tom@email.com"
}
},
{
"_index": "64469393user",
"_type": "_doc",
"_id": "4",
"_score": 1.0,
"_source": {
"id": "user4",
"email": "ben@email.com"
}
}
]
我有 2 个索引,一个存储 users,一个存储 articles.
用户文档:
{
"id" : "152ce52d-e975-4ebd-849a-0a12f535e644",
"email": "bob@email.com",
...
}
索引映射:
{
"mapping": {
"properties": {
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"email": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
文章文档:
{
"articleId" : "002ce52d-e975-4ebd-849a-0a12f535a536",
"userId": "152ce52d-e975-4ebd-849a-0a12f535e644",
...
}
索引映射:
{
"mapping": {
"properties": {
"articleId": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"userId": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
我正在尝试创建一个多索引查询,returns 所有没有文章的用户。
我不知道如何从第一个索引中排除包含在第二个索引中且具有相同 id/userId 字段的项目。
假设我有以下用户和文章:
用户:
[
{
"id": "user1",
"email": "bob@email.com"
},
{
"id": "user2",
"email": "john@email.com"
},
{
"id": "user3",
"email": "tom@email.com"
},
{
"id": "user4",
"email": "ben@email.com"
}
]
文章:
[
{
"articleId": "id1",
"userId": "user1"
},
{
"articleId": "id1",
"userId": "user2"
}
]
作为查询的结果,我想要所有没有文章的用户:
[{
"id": "user3",
"email": "tom@email.com"
},
{
"id": "user4",
"email": "ben@email.com"
}]
正如@leandrojmp 所指出的,您正在尝试在 SQL 中实现类似 JOIN 查询的操作,您希望在其中 returns 来自一个索引的所有剩余记录,这些记录与来自第二个指标。请参阅此文档,以了解有关 joining queries in elasticsearch
的更多信息I can't figure out how to exclude from the first index items contained in the second index and having the same id/userId field.
跨多个索引执行查询时可以使用_index field
。
搜索查询:
{
"query": {
"bool": {
"should": [
{
"bool": {
"must_not": [
{
"match": {
"id": "user1"
}
},
{
"match": {
"id": "user2"
}
}
],
"must": {
"term": {
"_index": "64469393user"
}
}
}
},
{
"bool": {
"must_not": [
{
"match": {
"userId": "user1"
}
},
{
"match": {
"userId": "user2"
}
}
],
"must": {
"term": {
"_index": "64469393article"
}
}
}
}
]
}
}
}
搜索结果:
"hits": [
{
"_index": "64469393user",
"_type": "_doc",
"_id": "3",
"_score": 1.0,
"_source": {
"id": "user3",
"email": "tom@email.com"
}
},
{
"_index": "64469393user",
"_type": "_doc",
"_id": "4",
"_score": 1.0,
"_source": {
"id": "user4",
"email": "ben@email.com"
}
}
]