一、动态更新模板
curl -XPUT 'http:
'{
"abctype" : {
"_all": {"enabled": false},
"properties":{
"a" : {
"type" : "long",
"index" : "not_analyzed",
"doc_values" : true
},
"b" : {
"type" : "long",
"index" : "not_analyzed",
"doc_values" : true
},
"c" : {
"type" : "long",
"index" : "not_analyzed",
"doc_values" : true
},
"d" : {
"type" : "long",
"index" : "not_analyzed",
"doc_values" : true
}
}
}
}'
二、提交ES查询
BoolQueryBuilder query = QueryBuilders.boolQuery()
query.must(QueryBuilders.rangeQuery("time").gte(starttime).lt(endtime))
query.must(QueryBuilders.termQuery("topic", topic))
SearchRequestBuilder requestBuilder = client.prepareSearch(indices.split(",", -1))
.setTypes(types)
.setQuery(query)
.addSort("time", SortOrder.ASC)
.setSize(1)
SearchResponse searchReponse = requestBuilder.execute().actionGet()
三、获取ES客户端
Client client = null;
String masterIP = getConfig("ES_NODES");
try
{
Settings settings = ImmutableSettings.settingsBuilder()
.put("client.transport.sniff", true)
.put("cluster.name", getConfig("ES_CLUSTER_NAME"))
.build();
TransportClient transportClient = new TransportClient(settings);
if(masterIP != null)
{
for(String ip : masterIP.split(","))
{
transportClient.addTransportAddress(new InetSocketTransportAddress(ip, 9300));
}
client = transportClient;
}else {
transportClient.close();
}
}
catch(Exception e)
{
logger.error("init es transportclient[" + masterIP + "] error," + e.getMessage());
throw new Exception(e.getMessage());
}
四、获取索引操作
String dayIndex = idxPrefix + dateFormat.format(startCal.getTime()).substring(0, 8)
IndicesExistsRequest indicesExistsRequest = new IndicesExistsRequest(dayIndex)
if(indicesAdminClient.exists(indicesExistsRequest).actionGet().isExists())
{
return dayIndex
}