2017-09-13 65 views
0

我試圖創建一個時間戳來附加到索引的任何文檔。我知道這可以通過查詢DSL通過下面的一組命令。我無法通過Java API找到如何操作。你有沒有做過這個?如何在ElasticSearch中通過Java API創建和使用管道

謝謝!

DELETE anindex 


PUT _ingest/pipeline/timestamp { 
    "description" : "describe pipeline", 
    "processors" : [{ 
         "set" : { 
           "field": "timestamp", 
           "value": "{{_ingest.timestamp}}" 
           } 
        }] 
       } 


PUT anindex 
{ 
    "mappings": { 
       "jeff": { 
         } 
       } 
} 


PUT anindex/jeff/id10?pipeline=timestamp 
{ 
    "hi": "jeff" 
} 


GET anindex/jeff/id10 
+0

根據我的理解,你已經定義了你的Ingest Pipeline「timestamp」。現在,您只需要通過在每次「時間戳」Pipeline中傳遞索引文檔來使用JAVA API。對? –

+0

謝謝您的回覆!不完全的。我們想將整個代碼翻譯成java。包括管道的定義。 – Tameem

+0

但是,爲什麼你需要每次定義管道定義? 它是一種元數據信息或映射,需要在數據插入或操作之前定義一次。 –

回答

0

據我所知,Elasticsearch沒有定義Ingest Pipeline的JAVA API。 作爲變通,您可以執行以下操作使用HttpURLConnection的定義從JAVA攝取管道:

URL obj = new URL("http://localhost:9200/_ingest/pipeline/timestamp"); 
String json = "{\n" + 
     " \"description\": \"describe pipeline\",\n" + 
     " \"processors\": [\n" + 
     " {\n" + 
     "  \"set\": {\n" + 
     "  \"field\": \"timestamp\",\n" + 
     "  \"value\": \"{{_ingest.timestamp}}\"\n" + 
     "  }\n" + 
     " }\n" + 
     " ]\n" + 
     "}"; 
HttpURLConnection con = (HttpURLConnection) obj.openConnection(); 

con.setRequestMethod("PUT"); 
con.setDoInput(true); 
con.setDoOutput(true); 
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); 

OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream()); 
osw.write(json); 
osw.flush(); 
osw.close(); 

System.out.println(con.getResponseCode() + " : " + con.getResponseMessage()); 
if (con != null) 
    con.disconnect(); 

而現在,下面是用於索引文檔中的Java代碼,通過將它們通過創建「時間戳」攝取管道:

TransportClient client = buildTransPortClient(); 

Map<String, Object> object = new HashMap<String, Object>(); 
object.put("user","kimchy"); 
object.put("postDate",new Date()); 
object.put("message","trying out Elasticsearch"); 

IndexResponse response = client.prepareIndex("test", "test", "100") 
     .setSource(object) 
     .setPipeline("timestamp") 
     .get(); 

System.out.println(response);