我的ElasticSearch集群的版本是6.2.4,导入elasticsearch相关的maven依赖也是6.2.4,不同版本的api可能会有差异
一:maven依赖
<!--elasticsearch核心依赖--> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>6.2.4</version> </dependency> <!--elasticsearch客户端--> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>6.2.4</version> </dependency> <!--日志--> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-to-slf4j</artifactId> <version>2.9.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.24</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.21</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.12</version> </dependency> <!--单元测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>compile</scope> </dependency> <!--实体转json--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.10.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.10.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.10.2</version> </dependency> <!--实体类简化工具--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.18</version> </dependency>
2.单元测试的共有方法
TransportClient client; @Before //创建连接 public void connection() throws UnknownHostException { //创建客户端连接对象 //参数为config\elasticsearch.yml中对应的值 也就是集群名称 Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build(); client = new PreBuiltTransportClient(settings); client.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.118.3"), 9300)); client.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.118.4"), 9300)); client.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.118.5"), 9300)); } @After //释放资源 public void close() { client.close(); } //遍历结果集 public void iteratorSearchHit(SearchHits searchHits) { System.out.println("符合查询条件有:" + searchHits.getTotalHits() + "条"); //获取迭代器 Iterator<SearchHit> iterator = searchHits.iterator(); System.out.println("开始进行遍历------------------------------"); while (iterator.hasNext()) { //获取每个查询对象 SearchHit searchHit = iterator.next(); //获取字符串类型并进行打印 System.out.println(searchHit.getSourceAsString()); Map<String, Object> sourceAsMap = searchHit.getSourceAsMap(); System.out.println("符合搜索条件的标题是:=======" + sourceAsMap.get("title")); /* for (Map.Entry<String,Object> map:sourceAsMap.entrySet()){ System.out.println("==================================="); System.out.println(map.getKey()); System.out.println(map.getValue()); } */ } }
3.常规操作
@Test //创建索引库 public void createIndex() { //索引名成必须小写 client.admin().indices().prepareCreate("myindex1").get(); } @Test //添加映射 public void mapping() throws IOException, ExecutionException, InterruptedException { XContentBuilder builder = XContentFactory.jsonBuilder().startObject() .startObject("type1") .startObject("properties") .startObject("id") .field("type", "integer") .field("store", "true") .endObject() .startObject("title") .field("type", "text") .field("store", "true") .field("analyzer", "ik_smart") .endObject() .startObject("content") .field("type", "text") .field("store", "true") .field("analyzer", "ik_smart") .endObject() .endObject() .endObject() .endObject(); PutMappingRequest mappingRequest = Requests.putMappingRequest("myindex1") .type("type1") .source(builder); client.admin().indices().putMapping(mappingRequest).get(); } @Test //给myindex1的type1创建文档 public void createDocument() throws IOException { XContentBuilder xContentBuilder = XContentFactory.jsonBuilder(); xContentBuilder.startObject() .field("id", 1) .field("title", "ElasticSearch是一个基于Lucene的搜索服务器") .field("content", "它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用 Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到 实时搜索,稳定,可靠,快速,安装使用方便。") .endObject(); //参数1:索引名 2:类型 3:id client.prepareIndex("myindex1", "type1", "1").setSource(xContentBuilder).get(); } @Test //通过实体转json创建文档 public void jsonDocument() throws JsonProcessingException { Type1 type1 = new Type1(); type1.setId(2); type1.setTitle("通过实体转json创建文档"); type1.setContent("通过创建实体的方式,将实体转json"); ObjectMapper objectMapper = new ObjectMapper(); client.prepareIndex("myindex1", "type1", type1.getId().toString()) .setSource(objectMapper.writeValueAsString(type1).getBytes(), XContentType.JSON) .get(); } @Test //关键词查询 public void termQuery() { //设置搜索条件,目标索引库,目标类型,目标域 SearchResponse searchResponse = client.prepareSearch("myindex1") .setTypes("type1") .setQuery(QueryBuilders.termQuery("content", "实体")).get(); SearchHits hits = searchResponse.getHits(); iteratorSearchHit(hits); } @Test //字符串查询 public void stringQuery() { //设置搜索条件,目标索引库,目标类型 SearchResponse searchResponse = client.prepareSearch("myindex1") .setTypes("type1") .setQuery(QueryBuilders.queryStringQuery("搜索实体")).get(); SearchHits hits = searchResponse.getHits(); iteratorSearchHit(hits); } @Test //根据文档ID进行查询 public void idQuery() { SearchResponse searchResponse = client.prepareSearch("myindex1") .setQuery(QueryBuilders.idsQuery("type1").addIds("2")) .get(); iteratorSearchHit(searchResponse.getHits()); } //分页之前的操作,批量添加文档 @Test public void batchInsert() throws JsonProcessingException { Type1 type1; for (int i = 1; i <= 50; i++) { type1 = new Type1(); type1.setId(i); type1.setTitle(i + "通过实体转json创建文档"); type1.setContent(i + "通过创建实体的方式,将实体转json"); ObjectMapper objectMapper = new ObjectMapper(); client.prepareIndex("myindex1", "type1", type1.getId().toString()) .setSource(objectMapper.writeValueAsString(type1).getBytes(), XContentType.JSON) .get(); } } //搜索文档并分页显示 @Test public void pageDocument() { SearchResponse searchResponse = client.prepareSearch("myindex1") .setTypes("type1") .setQuery(QueryBuilders.matchAllQuery()) //setFrom():从第几条开始检索,默认是0。 // setSize():每页最多显示的记录数默认是十 .setFrom(0) .setSize(5) .get(); iteratorSearchHit(searchResponse.getHits()); } //模拟搜索结果高亮显示 @Test public void highlight() { //设置查询条件 SearchRequestBuilder searchRequestBuilder = client.prepareSearch("myindex1") .setTypes("type1") .setQuery(QueryBuilders.termQuery("title", "实体")); //高亮设置 HighlightBuilder highlightBuilder = new HighlightBuilder(); //开头的标签信息 highlightBuilder.preTags("<font style=‘color:red‘>"); //结尾的标签 highlightBuilder.postTags("</font>"); highlightBuilder.field("title"); searchRequestBuilder.highlighter(highlightBuilder); SearchResponse searchResponse = searchRequestBuilder.get(); Iterator<SearchHit> iterator = searchResponse.getHits().iterator(); while (iterator.hasNext()) { SearchHit hit = iterator.next(); System.out.print("正常String方式打印文档搜索内容===="); System.out.println(hit.getSourceAsString()); Map<String, HighlightField> highlightFields = hit.getHighlightFields(); //遍历集合,输出高亮数据 //方式1 Text[] titles = highlightFields.get("title").getFragments(); for (Text str : titles) { System.out.println(str); } } }