elasticsearch多索引过滤器

sdnqo3pr  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(1)|浏览(384)

我有两个索引,一个存储用户,一个存储文章。
用户文档:

{
  "id" : "152ce52d-e975-4ebd-849a-0a12f535e644",
  "email": "bob@email.com",
  ...
}

索引Map:

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

索引Map:

{
  "mapping": {
    "properties": {
      "articleId": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      },
      "userId": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

我试图创建一个多索引查询,返回所有没有文章的用户。
我不知道如何从第一个索引中排除包含在第二个索引中并且具有相同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"
      }]
wooyq4lh

wooyq4lh1#

正如@leandrojmp所指出的,您正在尝试实现类似于sql中的连接查询的功能,在这里您希望返回一个索引中的所有剩余记录,这些记录与第二个索引中的记录不匹配。请参阅此文档,了解有关在elasticsearch中加入查询的更多信息
我不知道如何从第一个索引中排除包含在第二个索引中并且具有相同id/userid字段的项。
你可以用 _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"
        }
      }
    ]

相关问题