ES--JAVA示例

插入数据

Client client = null;
client = TransportClient.builder().build().addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.60.30"), 9300));
String[] in = {"reading","swiming"};        
Map map = new HashMap();
map.put("first_name", "诗诗");
map.put("last_name", "刘");
map.put("age", "22");
map.put("about", "can you guess?");
map.put("interests", in);
//插入数据
IndexResponse response = client.prepareIndex("wyg","employee","5").setSource(map).execute().actionGet();
client.close();

获取数据

GetResponse response = client.prepareGet("wyg", "employee", "5").execute().actionGet();
Map map1 = new HashMap();
map1 = response.getSource();
if(map1==null){
    System.out.println("null");
}else{
    Iterator> iterator = map1.entrySet().iterator();
    while(iterator.hasNext()){
        Entry entry = iterator.next();
        System.out.println(entry.getKey()+":"+entry.getValue());
    }
}
client.close();

查询

QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
QueryBuilder queryBuilder = QueryBuilders.boolQuery().must(new QueryStringQueryBuilder("can you guess?").field("about"));
SearchResponse response = client.prepareSearch("wyg").setTypes("employee").setQuery(queryBuilder).execute().actionGet();
System.out.println("total:"+response.getHits().getTotalHits());
SearchHit[] searchHists = response.getHits().getHits();
if(searchHists.length>0){
    for(SearchHit hit:searchHists){
        System.out.println("about:"+hit.getSource().get("about"));
        System.out.println("first_name:"+hit.getSource().get("first_name"));
    }
}
SearchResponse response = client.prepareSearch("index1", "index2") 
//设置要查询的索引(index) 
.setTypes("type1", "type2") 
//设置type, 这个在建立索引的时候同时设置了, 或者可以使用head工具查看 
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setetQuery(QueryBuilders.termQuery("multi", "test")) 
// Query 查询之一."multi"是要查询的field,"test"是要查询的内容
.setFilter(FilterBuilders.rangeFilter("age").from(12).to(18)) 
// Filter 查询之二, 在这里"age"是要查询的field, 后面的数字是查询的条件范围 
.setFrom(0).setSize(60).setExplain(true) 
//0-60 表示显示数量 
.execute() 
.actionGet();
SearchResponse response = client.prepareSearch("wyg").setTypes("employee").execute().actionGet();

删除

DeleteResponse response=client.prepareDelete("wyg", "employee", "1").execute().actionGet();

API详解
java调用
rabbitmq

你可能感兴趣的:(ES--JAVA示例)