ElasticSearch的Java Api基本操作入门指南

ElasticSearch的Java Api基本操作入门指南

ElasticSearch是一个基于Lucene构建的开源,分布式,RESTful搜索引擎框架。分布式是其最大的特点。安装比较简单,如果单机运行的话直接下载解压,在命令行下运行bin/elasticsearch就行了(win的dos和linux的窗口都行)。它能够通过http、restful、thrift等方式访问。不过今天我们看的是使用最普通的java api来测试其功能。

 

java api
 
如果你使用java,Elasticsearch提供两种内置的客户端。
Node client
Transport client
 
两种clients都和集群通过 9300 端口通信,使用本地es传输协议,
注意:集群内部节点通过 9300 端口通信来组成集群。
 
java客户端的版本必须和服务器节点的版本一致。

 

1.获取client实例,连接本地9300端口

1 this.client = new TransportClient()
2    
3 .addTransportAddress(new InetSocketTransportAddress(
4 "localhost"9300));

2.生成一个索引。这里用Map来保存json数据,然后插入到index为“twitter”的索引里面,其document为“tweet”,id为“1”。当然,生成json数据的方法很多,朋友们可以查看相关api。

1 public void generateIndex() {
2   Map<String, Object> json = new HashMap<String, Object>();
3   json.put("user""kimchy");
4   json.put("postDate"new Date());
5   json.put("message""trying out Elastic Search");
6  
7   IndexResponse response = this.client
8     .prepareIndex("twitter""tweet""1").setSource(json)
9     .execute().actionGet();
10  }

3.查询某个索引 ,这个一看就明白。

1 public void getIndex() {
2   GetResponse response = client.prepareGet("twitter""tweet""1")
3     .execute().actionGet();
4   Map<String, Object> rpMap = response.getSource();
5   if (rpMap == null) {
6    System.out.println("empty");
7    return;
8   }
9   Iterator<Entry<String, Object>> rpItor = rpMap.entrySet().iterator();
10   while (rpItor.hasNext()) {
11    Entry<String, Object> rpEnt = rpItor.next();
12    System.out.println(rpEnt.getKey() + " : " + rpEnt.getValue());
13   }
14  }

4. 搜索,创建一个termQuery查询,该查询要求全部匹配才会出结果,如果只要包含关键字里面一部分,可以创建fieldQuery。

1 public void searchIndex() {
2  
3   QueryBuilder qb = QueryBuilders.termQuery("user""kimchy");
4   SearchResponse scrollResp = client.prepareSearch("twitter")
5           .setSearchType(SearchType.SCAN)
6           .setScroll(new TimeValue(60000))
7           .setQuery(qb)
8           .setSize(100).execute().actionGet(); //100 hits per shard will be returned for each scroll
9   //Scroll until no hits are returned
10   while (true) {
11       scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(newTimeValue(600000)).execute().actionGet();
12       for (SearchHit hit : scrollResp.getHits()) {
13        Iterator<Entry<String, Object>> rpItor = hit.getSource().entrySet().iterator();
14     while (rpItor.hasNext()) {
15      Entry<String, Object> rpEnt = rpItor.next();
16      System.out.println(rpEnt.getKey() + " : " + rpEnt.getValue());
17     }
18       }
19       //Break condition: No hits are returned
20       if (scrollResp.getHits().hits().length == 0) {
21           break;
22       }
23   }
24  }

5.删除,删除的时候要指定Id的,这里指定id为1.

1 public void deleteIndex() {
2   DeleteResponse response = client.prepareDelete("twitter""tweet""1")
3           .execute()
4           .actionGet();
5  }

6.操作完毕后别忘记最后一步:关闭client连接。

1 public void closeClient() {
2   client.close();
3  }

总结:本文纯粹科普级,展示了一下ElasticSearch最基本的用法。

 

扫描二维码关注公众号,回复: 361427 查看本文章

from  http://www.dengchuanhua.com/159.html

猜你喜欢

转载自aoyouzi.iteye.com/blog/2116597