ES--JAVA例

2816 ワード

データの挿入
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