如何在ElasticSearch中通过Java API创建和使用管道
问题描述:
我试图创建一个时间戳来附加到索引的任何文档。我知道这可以通过查询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
答
据我所知,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);
根据我的理解,你已经定义了你的Ingest Pipeline“timestamp”。现在,您只需要通过在每次“时间戳”Pipeline中传递索引文档来使用JAVA API。对? –
谢谢您的回复!不完全的。我们想将整个代码翻译成java。包括管道的定义。 – Tameem
但是,为什么你需要每次定义管道定义? 它是一种元数据信息或映射,需要在数据插入或操作之前定义一次。 –