使用索引类似于黑名单,在Elasticsearch上搜索其他索引。

huangapple go评论60阅读模式
英文:

using index like blacklist when searching on other index on Elasticsearch

问题

我正在使用Elasticsearch创建一个用户系统。
我想从名为'Users'的索引中获取所有用户的数据,并排除在名为'BannedUsers'的索引中列出的用户。

在SQL查询中,它看起来像这样:SELECT * FROM [Users] WHERE [name] NOT IN (SELECT [name] FROM [BannedUsers])

在搜索另一个索引时,是否有一种方法可以将特定索引用作黑名单?

我想通过单个QueryDSL请求完成所有操作。

索引映射。

POST Users
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      }
    }
  }
}

POST BannedUsers
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      }
    }
  }
}
英文:

I am creating a user system using Elasticsearch.
I want to fetch data for all users from an index called 'Users' and exclude users whose names are listed in an index called 'BannedUsers'.

In a SQL query, it would look like this: SELECT * FROM [Users] WHERE [name] NOT IN (SELECT [name] FROM [BannedUsers]).

Is there any way to use a specific index as a blacklist when searching against another index?

I'd like to do everything with a single QueryDSL request.

indecs mapping.

POST Users
{
 "mappings": {
    "properties": {
      "name":{
         "type":"text"
    }
  }
}

POST BannedUsers
{
 "mappings": {
    "properties": {
      "name":{
         "type":"text"
    }
  }
}

答案1

得分: 1

你可以使用丰富策略并筛选数据。基本上,Users索引中的每个文档在丰富后都会有一个字段,像这样:is_banned: true,查询将如下所示:

GET users/_search
{
  "query": {
    "term": {
      "is_banned": false
    }
  }
}
英文:

You can use the enrichment policy and filter the data. Basically, each document in Users index will have a field after the enrichment like is_banned: true and the query will look like the following:

GET users/_search
{
  "query": {
    "term": {
      "is_banned": false
    }
  }
}

huangapple
  • 本文由 发表于 2023年7月6日 16:56:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76627120.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定