简体   繁体   中英

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

I have 1 index in elastic search data-production for storing documents. This index has a common field in each document named: document_type to filter the different type of data.

I have 2 types of documents in the index: data-production

a. document_type = " user "

b.document_type = " user_detail "

Example Data

  1. Users
        {
          "user_id" : "123",
          "is_trial_active" : "true",
          "updated_at" : "1577338950969",
          "event_created_at" : "1577338950969",
          "document_type" : "user"
        }
  1. Detail of User
    {         
       "user_id" : "123",
       "name" : "Shivam",
       "gender" : "male",
       "event_created_at" : 1575519449473,
       "phone_number" : "+91-8383838383",
       "document_type" : "user_detail",
       "created_at" : 1576049770184
    }

Note

  1. user_id is the common key in both document_type
  2. Using elasticsearch 7.3.1 version

Question

How to fetch detail of users from document_type ="user_details" whose is_trial_active is not true in document_type ="user"?

A working example:

Mappings

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

Post few documents

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

Search Query

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

Results

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

Hope this helps

you can use has_parent query to retrieve child document

Hope the following query work for you

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM