Elasticsearch的Java API使用

来源:互联网 发布:java五子棋人机对战 编辑:程序博客网 时间:2024/05/16 02:36

先将最基本的写出来(增删改查):我这里导入的是elasticsearch2.2.0的jar包

import java.net.InetAddress;import java.util.Date;import java.util.Map;import org.elasticsearch.action.delete.DeleteResponse;import org.elasticsearch.action.get.GetResponse;import org.elasticsearch.action.index.IndexResponse;import org.elasticsearch.action.search.SearchResponse;import org.elasticsearch.action.search.SearchType;import org.elasticsearch.action.update.UpdateRequest;import org.elasticsearch.client.Client;import org.elasticsearch.client.transport.TransportClient;import org.elasticsearch.common.settings.Settings;import org.elasticsearch.common.transport.InetSocketTransportAddress;import org.elasticsearch.common.xcontent.XContentBuilder;import org.elasticsearch.common.xcontent.XContentFactory;import org.elasticsearch.index.query.QueryBuilders;import org.elasticsearch.search.SearchHit;import org.elasticsearch.search.SearchHits;public class test {public static void main(String args[]) throws Exception {//连接es代码Settings settings = Settings.settingsBuilder().put("cluster.name", "my-application").build();Client client = TransportClient.builder().settings(settings).build().addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("h153"), 9300));//这里还必须得用主机名不能用IP//创建索引XContentBuilder builder = XContentFactory.jsonBuilder().startObject().field("user", "kity").field("postDate", new Date()).field("message", "trying out Elasticsearch").endObject();String json = builder.string();IndexResponse response = client.prepareIndex("hui", "emp", "1").setSource(json).execute().actionGet();System.out.println("<---------------创建成功--------------------------->");//查询索引//GetResponse response = client.prepareGet("hui", "emp", "1").get();//Map<String, Object> source= response.getSource();//for(Map.Entry<String, Object> filed : source.entrySet()){//String key = filed.getKey();//System.out.println("key==="+key+"  value==="+filed.getValue().toString());//}//System.out.println("<---------------查询成功--------------------------->");//更新或插入//XContentBuilder builder = XContentFactory.jsonBuilder()//.startObject()//.field("user", "123456")//.field("postDate", new Date())//.field("message", "trying out Elasticsearch!!!")//.endObject();//String json1 = builder.string();//UpdateRequest updateRequest = new UpdateRequest();////updateRequest.index("index1", "index2");//多个Index//updateRequest.index("hui");////updateRequest.type("type1", "type2");//多个type//updateRequest.type("emp");//updateRequest.id("1");//updateRequest.doc(builder);//client.update(updateRequest).get();//GetResponse response = client.prepareGet("hui", "emp", "1").get();//Map<String, Object> source= response.getSource();//for(Map.Entry<String, Object> filed : source.entrySet()){//String key = filed.getKey();//System.out.println("key==="+key+"  value==="+filed.getValue().toString());//}//System.out.println("<---------------更新成功--------------------------->");//搜索//SearchResponse response = client.prepareSearch("hui")//多个index:client.prepareSearch("index1", "index2")//.setTypes("emp")//多个type:setTypes("type1", "type2")//.setSearchType(SearchType.DFS_QUERY_AND_FETCH)//.setQuery(QueryBuilders.termQuery("user", "123456"))//Query////.setPostFilter(QueryBuilders.rangeQuery("message").from(12).to(18))//Filter//.setFrom(0).setSize(60).setExplain(true)//.execute()//.actionGet();//SearchHits hits = response.getHits();//System.out.println("搜索到记录数 "+hits.getHits().length);//SearchHit[] hits1 = hits.getHits();//for(SearchHit hit : hits1){//Map<String, Object> source = hit.getSource();//for(Map.Entry<String, Object> filed : source.entrySet()){//String key = filed.getKey();//System.out.println("key==="+key+"  value==="+filed.getValue().toString());//}//}//System.out.println("<---------------搜索成功--------------------------->");//总数count//Long count = client.prepareCount("hui").execute().get().getCount();//System.out.println("<---------------共有  "+count+" 条索引--------------->");//删除索引//DeleteResponse response = client.prepareDelete("hui", "emp", "1").execute().actionGet();//System.out.println("<---------------删除成功--------------------------->");}}

注意:在创建索引的时候还可以像curl命令那样指定参数



参考:

https://jingyan.baidu.com/article/3052f5a1e8a06397f31f8699.html