elasticsearch java 索引操作

1、添加maven依赖 

Xml代码  收藏代码
  1. <dependency>  
  2.     <groupId>org.elasticsearch</groupId>  
  3.     <artifactId>elasticsearch</artifactId>  
  4.     <version>0.90.0</version>  
  5. </dependency>  


建议使用maven管理项目,因为elasticsearch还有很多依赖包,手工维护很麻烦 
2、创建连接elasticsearch服务的client 

Java代码  收藏代码
  1. Settings settings = ImmutableSettings.settingsBuilder().put("client.transport.sniff", true).put("cluster.name", "name of node").build();  
  2. Client client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("ip of server", 9300));  


3、创建索引 
elasticsearch的java客户端,支持多种方式构建索引数据,这里有两种方式的代码示例:使用jsonbuilder构建数据 

Java代码  收藏代码
  1. IndexResponse response = client.prepareIndex("comment_index", "comment_ugc", "comment_123674")  
  2.     .setSource( XContentFactory.jsonBuilder()  
  3.     .startObject()  
  4.       .field("author", "569874")  
  5.       .field("author_name", "riching")  
  6.       .field("mark", 232)  
  7.       .field("body", "北京不错,但是人太多了")  
  8.       .field("createDate", "20130801175520")  
  9.       .field("valid", true)  
  10.     .endObject())  
  11.     .setTTL(8000)  
  12.     .execute().actionGet();  
  13.   
  14. System.out.println(response.getId());  


另外一种,是把数据构造成json串,直接传给client 

Java代码  收藏代码
  1. Student student = new Student(103161066, 20, "riching", "beijing");  
  2. String jsonValue = mapper.writeValueAsString(student);  
  3. response = client.prepareIndex("student_index", "student_info", "stu_103161066").setSource(jsonValue).execute().actionGet();  
  4. System.out.println(response.getId());  


实际应用中应该是下面一种更方便,可以把需要索引的对象直接扔过去了 


4、根据id获取数据 

Java代码  收藏代码
  1. GetResponse responseGet = client.prepareGet("comment_index", "comment_ugc",         "comment_123674").execute().actionGet();  
  2. System.out.println(responseGet.getSourceAsString());  



5、查询索引 

Java代码  收藏代码
  1. SearchRequestBuilder builder = client.prepareSearch("comment_index").setTypes("comment_ugc").setSearchType(SearchType.DEFAULT).setFrom(0).setSize(100);  
  2. BoolQueryBuilder qb = QueryBuilders.boolQuery().must(new   QueryStringQueryBuilder("北京").field("body"))  
  3.     .should(new QueryStringQueryBuilder("太多").field("body"));  
  4. builder.setQuery(qb);  
  5. SearchResponse response = builder.execute().actionGet();  
  6. System.out.println("  " + response);  
  7. System.out.println(response.getHits().getTotalHits());  


执行结果 

Java代码  收藏代码
  1. {  
  2.   "took" : 8,  
  3.   "timed_out" : false,  
  4.   "_shards" : {  
  5.     "total" : 5,  
  6.     "successful" : 5,  
  7.     "failed" : 0  
  8.   },  
  9.   "hits" : {  
  10.     "total" : 1,  
  11.     "max_score" : 0.19178301,  
  12.     "hits" : [ {  
  13.       "_index" : "comment_index",  
  14.       "_type" : "comment_ugc",  
  15.       "_id" : "comment_123674",  
  16.       "_score" : 0.19178301, "_source" : {"author":"569874","author_name":"riching","mark":232,"body":"北京不错,但是人太多了","createDate":"20130801175520","valid":true}  
  17.     } ]  
  18.   }  
  19. }  
  20. 1  



6、删除索引,可以根据索引id删除索引,也可以构造query进行删除,这跟lucene的api是类似的,只不过api不一样而已 

Java代码  收藏代码
  1. DeleteResponse response = client.prepareDelete("comment_index", "comment_ugc", "comment_123674") .setOperationThreaded(false).execute().actionGet();  
  2. System.out.println(response.getId());  


这个删除有个小问题,如果删除完立即进行查询还是可以查到

原文地址:https://www.cnblogs.com/bmaker/p/5463968.html