ES--JAVA示例

插入數(shù)據(jù)

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);
//插入數(shù)據(jù)
IndexResponse response = client.prepareIndex("wyg","employee","5").setSource(map).execute().actionGet();
client.close();

獲取數(shù)據(jù)

GetResponse response = client.prepareGet("wyg", "employee", "5").execute().actionGet();
Map<String,Object> map1 = new HashMap<String,Object>();
map1 = response.getSource();
if(map1==null){
    System.out.println("null");
}else{
    Iterator<Entry<String,Object>> 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") 
//設(shè)置要查詢的索引(index) 
.setTypes("type1", "type2") 
//設(shè)置type, 這個在建立索引的時候同時設(shè)置了, 或者可以使用head工具查看 
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setetQuery(QueryBuilders.termQuery("multi", "test")) 
// Query 查詢之一."multi"是要查詢的field,"test"是要查詢的內(nèi)容
.setFilter(FilterBuilders.rangeFilter("age").from(12).to(18)) 
// Filter 查詢之二, 在這里"age"是要查詢的field, 后面的數(shù)字是查詢的條件范圍 
.setFrom(0).setSize(60).setExplain(true) 
//0-60 表示顯示數(shù)量 
.execute() 
.actionGet();
SearchResponse response = client.prepareSearch("wyg").setTypes("employee").execute().actionGet();

刪除

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

API詳解
java調(diào)用
rabbitmq

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,545評論 19 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,001評論 25 709
  • 原文引自Android框架之Android Validation表單驗證框架 - SegmentFault 簡單介...
    王小冬閱讀 6,536評論 0 4
  • 二零一二年四月十五日下午一點 常務(wù)中學(xué)一間高三教室 剛好是中午吃完飯的時間,班主任要求一點到教室,上自習(xí)或者睡覺。...
    孟梓珉閱讀 294評論 0 0
  • 暮次邊城宿夜涼,樓高猶見笛吹羌。 冰蟾半闕穿云海,欲往乘之歸故鄉(xiāng)。 2017年09月20日
    寺咀山主人閱讀 603評論 1 7

友情鏈接更多精彩內(nèi)容