1

我在弹性搜索数据生产中有 1 个用于存储文档的索引。该索引在每个文档中都有一个名为:document_type的公共字段,用于过滤不同类型的数据。

我在索引中有两种类型的文档:数据生产

一个。document_type = "用户"

湾。document_type = "用户详细信息"

示例数据

  1. 用户
        {
          "user_id" : "123",
          "is_trial_active" : "true",
          "updated_at" : "1577338950969",
          "event_created_at" : "1577338950969",
          "document_type" : "user"
        }
  1. 用户详情
    {         
       "user_id" : "123",
       "name" : "Shivam",
       "gender" : "male",
       "event_created_at" : 1575519449473,
       "phone_number" : "+91-8383838383",
       "document_type" : "user_detail",
       "created_at" : 1576049770184
    }

笔记

  1. user_id 是两个document_type中的公共键
  2. 使用elasticsearch 7.3.1 版本

问题

如何从document_type =“user_details”中获取is_trial_active在document_type =“user”中不正确的用户的详细信息?

4

2 回答 2

1

一个工作示例:

映射

PUT my_index
{
  "mappings": {
    "properties": {
      "document_type": {
        "type": "join",
        "relations": {
          "user": "user_detail"
        }
      }
    }
  }
}

发几个文件

PUT my_index/_doc/1
{
  "user_id": "123",
  "is_trial_active": "false", ---> note i changed this to false for the example
  "updated_at": "1577338950969",
  "event_created_at": "1577338950969",
  "document_type": "user"
}

PUT my_index/_doc/2?routing=1
{
  "user_id": "123",
  "name": "Shivam",
  "gender": "male",
  "event_created_at": 1575519449473,
  "phone_number": "+91-8383838383",
  "created_at": 1576049770184,
  "document_type": {
    "name": "user_detail",
    "parent": "1"  --> you can insert array of parents
  }
}

搜索查询

GET my_index/_search
{
  "query": {
    "has_parent": {
      "parent_type": "user",
      "query": {
        "bool": {
          "must_not": [
            {
              "term": {
                "is_trial_active": {
                  "value": "true"
                }
              }
            }
          ]
        }
      }
    }
  }
}

结果

"hits" : [
  {
    "_index" : "my_index",
    "_type" : "_doc",
    "_id" : "2",
    "_score" : 1.0,
    "_routing" : "1",
    "_source" : {
      "user_id" : "123",
      "name" : "Shivam",
      "gender" : "male",
      "event_created_at" : 1575519449473,
      "phone_number" : "+91-8383838383",
      "created_at" : 1576049770184,
      "document_type" : {
        "name" : "user_detail",
        "parent" : "1"
      }
    }
  }
]

希望这可以帮助

于 2019-12-26T16:12:10.813 回答
0

您可以使用has_parent查询来检索子文档

希望以下查询对您有用

GET data-production/_search
{
  "query": {
    "has_parent": {
      "parent_type": "user",
      "query": {
        "bool": {
          "must_not": [
            {
              "term": {
                "is_trial_active": {
                  "value": "true"
                }
              }
            }
          ]
        }
      }
    }
  }
}
于 2019-12-26T10:28:59.763 回答