簡體   English   中英

使用logstash在彈性搜索中將兩個索引組合成第三個索引

[英]Combine two index into third index in elastic search using logstash

我有兩個索引

  1. employee_data {"code":1, "name":xyz, "city":"Mumbai" }
  2. transaction_data {"code":1, "Month":June", payment:78000 }

我想要這樣的第三個索引 3)join_index

{"code":1, "name":xyz, "city":"Mumbai", "Month":June", payment:78000 }這怎么可能??

我正在嘗試使用 logstash

input {
  elasticsearch {
    hosts => "localost"
    index => "employees_data,transaction_data"
   
     query => '{ "query": { "match": { "code": 1} } }'
    scroll => "5m"
    docinfo => true
  }
}
output {

elasticsearch { 主機 => [“本地主機”]

index => "join1"
   }

}

您可以在employees_data上使用 elasticsearch輸入

在您的過濾器中,對transaction_data使用 elasticsearch過濾器

input {
  elasticsearch {
    hosts => "localost"
    index => "employees_data"
   
     query => '{ "query": { "match_all": { } } }'
     sort => "code:desc"

    scroll => "5m"
    docinfo => true
  }
}
filter {
    elasticsearch {
              hosts => "localhost"
              index => "transaction_data"
              query => "(code:\"%{[code]}\"
              fields => { 
                    "Month" => "Month",
                    "payment" => "payment" 
                   }
        }
}
output {
  elasticsearch { 
    hosts => ["localhost"]
    index => "join1"
   }
}

並使用 elasticsearch output將您的新文檔發送到您的第三個索引

您將擁有 3 個彈性搜索連接,結果可能會有點慢。 但它有效。

您不需要 Logstash 來執行此操作,Elasticsearch 本身通過利用 enrich enrich processor來支持它。

首先,您需要創建一個豐富策略(使用最小的索引,假設它是employees_data ):

PUT /_enrich/policy/employee-policy
{
  "match": {
    "indices": "employees_data",
    "match_field": "code",
    "enrich_fields": ["name", "city"]
  }
}

然后您可以執行該策略以創建濃縮索引

POST /_enrich/policy/employee-policy/_execute

創建並填充豐富索引后,下一步需要您創建使用上述豐富策略/索引的攝取管道:

PUT /_ingest/pipeline/employee_lookup
{
  "description" : "Enriching transactions with employee data",
  "processors" : [
    {
      "enrich" : {
        "policy_name": "employee-policy",
        "field" : "code",
        "target_field": "tmp",
        "max_matches": "1"
      }
    },
    {
      "script": {
        "if": "ctx.tmp != null",
        "source": "ctx.putAll(ctx.tmp); ctx.remove('tmp');"
      }
    }
  ]
}

最后,您現在已准備好使用連接的數據創建目標索引。 只需將_reindex API 與我們剛剛創建的攝取管道結合使用即可:

POST _reindex
{
  "source": {
    "index": "transaction_data"
  },
  "dest": {
    "index": "join1",
    "pipeline": "employee_lookup"
  }
}

運行此命令后, join1索引將包含您所需要的內容,例如:

  {
    "_index" : "join1",
    "_type" : "_doc",
    "_id" : "0uA8dXMBU9tMsBeoajlw",
    "_score" : 1.0,
    "_source" : {
      "code":1, 
      "name": "xyz", 
      "city": "Mumbai", 
      "Month": "June", 
      "payment": 78000 
    }
  }

據我所知,僅使用 elasticsearch API 是不可能發生這種情況的。 要處理此問題,您需要為相關文檔設置唯一 ID。 例如,您在問題中提到的代碼可以作為文檔的良好 ID。 因此,您可以將第一個索引重新索引到第三個索引,並使用 UPDATE API 來更新它們,方法是從第二個索引中讀取文檔並將它們的 ID 更新到第三個索引中。 我希望我能提供幫助。

暫無
暫無

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

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