1.创建工程,导入依赖
<dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>5.6.8</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>5.6.8</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> </dependency>
2.创建索引index
package com.my.index; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.transport.client.PreBuiltTransportClient; import org.junit.Test; import java.net.InetAddress; import java.net.UnknownHostException; public class IndexTest { /*创建索引*/ @Test public void test01() throws UnknownHostException { //创建client连接对象 Settings settings=Settings.builder().put("cluster.name","my-elasticsearch").build(); TransportClient client=new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9300)); //创建名称为blog2的索引 client.admin().indices().prepareCreate("blog2").get(); //释放资源 client.close(); } }
3.创建映射mapping
/*创建映射*/ @Test public void test02() throws IOException, ExecutionException, InterruptedException { //创建client连接对象 Settings settings=Settings.builder().put("cluster.name","my-elasticsearch").build(); TransportClient client=new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9300)); //添加映射 XContentBuilder builder= XContentFactory.jsonBuilder() .startObject() .startObject("table01") .startObject("properties") .startObject("id") .field("type","integer").field("store","yes") .endObject() .startObject("title") .field("type","string").field("store","yes").field("analyzer","ik_smart") .endObject() .startObject("content") .field("type","string").field("store","yes").field("analyzer","ik_smart") .endObject() .endObject() .endObject() .endObject(); //创建映射 PutMappingRequest mapping= Requests.putMappingRequest("blog2").type("table01").source(builder); client.admin().indices().putMapping(mapping).get(); //释放资源 client.close(); }
4.创建文档document
4.1 通过XContentFactory方式
/*创建文档document*/ @Test public void test03() throws IOException { //创建client连接对象 Settings settings=Settings.builder().put("cluster.name","my-elasticsearch").build(); TransportClient client=new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9300)); //创建文档信息 XContentBuilder builder=XContentFactory.jsonBuilder() .startObject() .field("id",1) .field("title","ES是一个基于Lucene的搜索服务器") .field("content","提供了一个分布式多用户能力的全文引擎") .endObject(); client.prepareIndex("blog2","table01","1").setSource(builder).get(); //释放资源 client.close(); }
4.2 通过Jackson转换实体
4.2.1 创建实体
package com.my.index; public class Table01 { private Integer id; private String title; private String content; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
4.2.2 导入依赖
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.8.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.8.1</version> </dependency>
4.2.3 编写代码
/*创建文档document*/ /*使用Jackson转换实体*/ @Test public void test04() throws UnknownHostException, JsonProcessingException { //创建client连接对象 Settings settings=Settings.builder().put("cluster.name","my-elasticsearch").build(); TransportClient client=new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9300)); //描述数据 Table01 table01=new Table01(); table01.setId(2); table01.setTitle("搜索工作其实很快乐"); table01.setContent("希望我们的搜索解决方案要快"); ObjectMapper objectMapper=new ObjectMapper(); //建立文档 client.prepareIndex("blog2","table01",table01.getId().toString()) .setSource(objectMapper.writeValueAsString(table01).getBytes(), XContentType.JSON).get(); //释放资源 client.close(); }
5.查询文档操作
5.1 关键词查询
/*关键词查询*/ @Test public void testTermQuery() throws UnknownHostException { //创建client连接对象 Settings settings=Settings.builder().put("cluster.name","my-elasticsearch").build(); TransportClient client=new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9300)); //设置搜索条件 SearchResponse searchResponse=client.prepareSearch("blog2") .setTypes("table01") .setQuery(QueryBuilders.termQuery("content","搜索")).get(); //遍历搜索结果数据 SearchHits hits=searchResponse.getHits(); //获取命中次数,查询结果有多少对象 System.out.println("查询结果有:"+hits.getTotalHits()+"条"); Iterator<SearchHit> iterator=hits.iterator(); while (iterator.hasNext()){ SearchHit searchHit=iterator.next(); //每个查询对象 System.out.println(searchHit.getSourceAsString()); //获取字符串格式打印 System.out.println("title:"+searchHit.getSource().get("title")); } //释放资源 client.close(); }
5.2 字符串查询
/*字符串查询*/ @Test public void testStringQuery() throws UnknownHostException { //创建client连接对象 Settings settings=Settings.builder().put("cluster.name","my-elasticsearch").build(); TransportClient client=new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9300)); //设置搜索条件 SearchResponse searchResponse=client.prepareSearch("blog2") .setTypes("table01") .setQuery(QueryBuilders.queryStringQuery("搜索")).get(); //遍历搜索结果数据 SearchHits hits=searchResponse.getHits(); //获取命中次数,查询结果有多少对象 System.out.println("查询结果有:"+hits.getTotalHits()+"条"); Iterator<SearchHit> iterator=hits.iterator(); while (iterator.hasNext()){ SearchHit searchHit=iterator.next(); //每个查询对象 System.out.println(searchHit.getSourceAsString()); //获取字符串格式打印 System.out.println("title:"+searchHit.getSource().get("title")); } //释放资源 client.close(); }
5.3 使用文档ID查询
/*使用文档ID查询*/ @Test public void testIdQuery() throws UnknownHostException { //创建client连接对象 Settings settings=Settings.builder().put("cluster.name","my-elasticsearch").build(); TransportClient client=new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9300)); //设置搜索条件 SearchResponse response=client.prepareSearch("blog2") .setTypes("table01") //设置需要查询的id .setQuery(QueryBuilders.idsQuery().addIds("2")).get(); //取出查询结果 SearchHits searchHits=response.getHits(); //取出查询结果总记录数 System.out.println(searchHits.getTotalHits()); Iterator<SearchHit> hitIterator=searchHits.iterator(); while (hitIterator.hasNext()){ SearchHit searchHit=hitIterator.next(); System.out.println(searchHit.getSourceAsString()); } }
6.查询文档分页操作
6.1 批量插入数据
/*批量插入数据*/ @Test public void test01() throws JsonProcessingException, UnknownHostException { //创建client连接对象 Settings settings=Settings.builder().put("cluster.name","my-elasticsearch").build(); TransportClient client=new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9300)); ObjectMapper objectMapper=new ObjectMapper(); for (int i=1;i<=10;i++){ //描述JSON数据 Table01 table01=new Table01(); table01.setId(i); table01.setTitle(i+"搜索工作其实很快乐"); table01.setContent(i+"我们希望我们的搜索解决方案要快!!!"); //建立文档 client.prepareIndex("blog2","table01",table01.getId().toString()) .setSource(objectMapper.writeValueAsString(table01).getBytes(), XContentType.JSON).get(); } //释放资源 client.close(); }
6.2 分页查询
/*分页查询*/ @Test public void test02() throws UnknownHostException { //创建client连接对象 Settings settings=Settings.builder().put("cluster.name","my-elasticsearch").build(); TransportClient client=new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9300)); //搜索数据 SearchRequestBuilder searchRequestBuilder=client.prepareSearch("blog2") .setTypes("table01").setQuery(QueryBuilders.matchAllQuery()); //默认每页10条数据 //查询第2页数据,每页3条 searchRequestBuilder.setFrom(0).setSize(3); SearchResponse searchResponse=searchRequestBuilder.get(); SearchHits hits=searchResponse.getHits(); //获取命中次数,查询结果有多少对象 System.out.println("查询结果有:"+hits.getTotalHits()+"条"); Iterator<SearchHit> iterator=hits.iterator(); while (iterator.hasNext()){ SearchHit searchHit=iterator.next(); //每个查询对象 System.out.println(searchHit.getSourceAsString()); //获取字符串格式打印 System.out.println("id:"+searchHit.getSource().get("id")); System.out.println("title:"+searchHit.getSource().get("title")); System.out.println("content:"+searchHit.getSource().get("content")); System.out.println("---------------------------"); } //释放资源 client.close(); }
7.查询结果高亮操作
7.1 什么是高亮显示
在进行关键字搜索时,搜索出的内容中的关键字会显示不同的颜色,称之为高亮
7.2 高亮显示的html分析
通过开发者工具查看高亮数据的htmldiamante实现;
ES可以查看出的内容中关键字部分进行标签和样式的设置,但是你需要告诉ES使用什么标签对高亮关键字进行包裹;
7.3 高亮显示代码实现
package com.my.index; import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.text.Text; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.transport.client.PreBuiltTransportClient; import org.junit.Test; import java.net.InetAddress; import java.net.UnknownHostException; public class GaoTest { @Test public void test01() throws UnknownHostException { /*高亮查询*/ //创建client连接对象 Settings settings = Settings.builder().put("cluster.name", "my-elasticsearch").build(); TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300)); //搜索数据 SearchRequestBuilder searchRequestBuilder = client .prepareSearch("blog2").setTypes("table01") .setQuery(QueryBuilders.termQuery("title", "搜索")); //设置高亮数据 HighlightBuilder hiBuilder = new HighlightBuilder(); hiBuilder.preTags("<font style='color:red'>"); hiBuilder.postTags("</font>"); hiBuilder.field("title"); searchRequestBuilder.highlighter(hiBuilder); //获取查询结果数据 SearchResponse searchResponse=searchRequestBuilder.get(); //获取查询结果集 SearchHits searchHits=searchResponse.getHits(); System.out.println("共搜索到:"+searchHits.getTotalHits()+"条结果"); //遍历结果 for (SearchHit hit:searchHits){ System.out.println("String方式打印文档搜索内容"); System.out.println(hit.getSourceAsString()); System.out.println("Map方式打印高亮内容"); System.out.println(hit.getHighlightFields()); System.out.println("遍历高亮集合,打印高亮片段"); org.elasticsearch.common.text.Text[] text = hit.getHighlightFields().get("title").getFragments(); System.out.println("------------------------------------------------------------------"); for (Text str:text){ System.out.println(str); } //关闭资源 client.close(); } } }