簡體   English   中英

彈性搜索/kibana 從同一索引的 2 個文檔中獲取數據?

[英]Elastic search/kibana get data from 2 documents of same Index?

我在用於存儲文檔的彈性搜索數據生產中有 1 個索引。 這個索引在每個文檔中都有一個名為: 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_activedocument_type ="user" 中不正確?

一個工作示例:

映射

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"
      }
    }
  }
]

希望這可以幫助

您可以使用has_parent查詢來檢索子文檔

希望以下查詢對您有用

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM