弹性搜索多索引过滤器
我有两个索引,一个存储用户,一个存储文章 用户文档:弹性搜索多索引过滤器,
Warning: implode(): Invalid arguments passed in /data/phpspider/zhask/webroot/tpl/detail.html on line 45
,,我有两个索引,一个存储用户,一个存储文章 用户文档: { "id" : "152ce52d-e975-4ebd-849a-0a12f535e644", "email": "bob@email.com", ... } { "articleId" : "002ce52d-e975-4ebd-849a-0a12f535a536", "userId&qu
{
"id" : "152ce52d-e975-4ebd-849a-0a12f535e644",
"email": "bob@email.com",
...
}
{
"articleId" : "002ce52d-e975-4ebd-849a-0a12f535a536",
"userId": "152ce52d-e975-4ebd-849a-0a12f535e644",
...
}
索引映射:
{
"mapping": {
"properties": {
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"email": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
{
"mapping": {
"properties": {
"articleId": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"userId": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
第条文件:
{
"id" : "152ce52d-e975-4ebd-849a-0a12f535e644",
"email": "bob@email.com",
...
}
{
"articleId" : "002ce52d-e975-4ebd-849a-0a12f535a536",
"userId": "152ce52d-e975-4ebd-849a-0a12f535e644",
...
}
索引映射:
{
"mapping": {
"properties": {
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"email": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
{
"mapping": {
"properties": {
"articleId": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"userId": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
我试图创建一个多索引查询,返回所有没有文章的用户
我不知道如何从第二个索引中包含的第一个索引项中排除具有相同id/userId字段的项
假设我有以下用户和文章:
[{
"id": "user3",
"email": "tom@email.com"
},
{
"id": "user4",
"email": "ben@email.com"
}]
用户:
[
{
"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中的联接查询的功能,您希望从一个索引返回与第二个索引中的记录不匹配的所有剩余记录。请参阅本文档,了解更多信息 我不知道如何从包含的第一个索引项中排除 在第二个索引中,并且具有相同的id/userId字段 您可以在跨多个索引执行查询时使用。 搜索查询:
{
"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"
}
}
]
搜索结果:
{
"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"
}
}
]
您能分享一下您的索引映射和示例预期搜索结果吗?@Bhavya用索引映射编辑了描述,谢谢!您要做的是类似于SQL中的联接查询,这在elasticsearch中不受支持。您将需要两个查询,一个用于获取包含文章的用户的id,另一个用于排除用户索引中具有相同id的用户。另一个选择是对数据进行非规范化,只使用一个索引。谢谢@Bhavya。在这种情况下,我需要事先知道ID,以便将它们添加到查询中。如果查询是“获取所有未使用id=id1撰写文章的用户”会是另一种情况吗?@Maucan感谢您接受我的回答,您也可以对我的答案进行投票吗@Maucan可以随意提出一个新问题,您的用例如上面的评论所述