弹性搜索/基巴纳语从相同索引的2个文档中获取数据?

时间:2019-12-26 08:12:57

标签: elasticsearch amazon-elastic-beanstalk kibana elastic-stack kibana-7

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

索引中有2种文档:数据生成

a。 document_type =“ 用户

b。 document_type =“ user_detail

示例数据

  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. 中的公用密钥
  3. 使用elasticsearch 7.3.1版本

问题

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

2 个答案:

答案 0 :(得分: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"
      }
    }
  }
]

希望这会有所帮助

答案 1 :(得分:0)

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

希望以下查询对您有用

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