一、创建工程,导入坐标
pom.xml坐标
<dependencies> <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> </dependencies>
二、创建索引index
@Test //创建索引 public void test1() throws Exception{ // 创建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(); }
三、创建映射mapping
@Test //创建映射 public void test3() throws Exception{ // 创建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)); // 添加映射 /** * 格式: * "mappings" : { "article" : { "dynamic" : "false", "properties" : { "id" : { "type" : "string" }, "content" : { "type" : "string" }, "author" : { "type" : "string" } } } } */ XContentBuilder builder = XContentFactory.jsonBuilder() .startObject() .startObject("article") .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("article").source(builder); client.admin().indices().putMapping(mapping).get(); //释放资源 client.close(); }
四、建立文档document
1、建立文档(通过XContentBuilder)
@Test //创建文档(通过XContentBuilder) public void test4() throws Exception{ // 创建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", "ElasticSearch是一个基于Lucene的搜索服务器") .field("content", "它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。") .endObject(); // 建立文档对象 /** * 参数一blog1:表示索引对象 * 参数二article:类型 * 参数三1:建立id */ client.prepareIndex("blog2", "article", "1").setSource(builder).get(); //释放资源 client.close(); }
2、建立文档(使用Jackson转换实体)
1)创建Article实体
public class Article { private Integer id; private String title; private String content; getter/setter... }
2)添加jackson坐标
<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>
3)代码实现
@Test //创建文档(通过实体转json) public void test5() throws Exception{ // 创建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)); // 描述json 数据 //{id:xxx, title:xxx, content:xxx} Article article = new Article(); article.setId(2); article.setTitle("搜索工作其实很快乐"); article.setContent("我们希望我们的搜索解决方案要快,我们希望有一个零配置和一个完全免费的搜索模式,我们希望能够简单地使用JSON通过HTTP的索引数据,我们希望我们的搜索服务器始终可用,我们希望能够一台开始并扩展到数百,我们要实时搜索,我们要简单的多租户,我们希望建立一个云的解决方案。Elasticsearch旨在解决所有这些问题和更多的问题。"); ObjectMapper objectMapper = new ObjectMapper(); // 建立文档 client.prepareIndex("blog2", "article", article.getId().toString()) //.setSource(objectMapper.writeValueAsString(article)).get(); .setSource(objectMapper.writeValueAsString(article).getBytes(), XContentType.JSON).get(); //释放资源 client.close(); }
五、查询文档操作
1、关键词查询
@Test public void testTermQuery() throws Exception{ //1、创建es客户端连接对象 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)); //2、设置搜索条件 SearchResponse searchResponse = client.prepareSearch("blog2") .setTypes("article") .setQuery(QueryBuilders.termQuery("content", "搜索")).get(); //3、遍历搜索结果数据 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")); } //4、释放资源 client.close(); }
2、字符串查询
@Test public void testStringQuery() throws Exception{ //1、创建es客户端连接对象 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)); //2、设置搜索条件 SearchResponse searchResponse = client.prepareSearch("blog2") .setTypes("article") .setQuery(QueryBuilders.queryStringQuery("搜索")).get(); //3、遍历搜索结果数据 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")); } //4、释放资源 client.close(); }
3、使用文档ID查询文档
@Test public void testIdQuery() throws Exception { //client对象为TransportClient对象 SearchResponse response = client.prepareSearch("blog1") .setTypes("article") //设置要查询的id .setQuery(QueryBuilders.idsQuery().addIds("test002")) //执行查询 .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()); } }
六、查询文档分页操作
1、批量插入数据
@Test //批量插入100条数据 public void test9() throws Exception{ // 创建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 <= 100; i++) { // 描述json 数据 Article article = new Article(); article.setId(i); article.setTitle(i + "搜索工作其实很快乐"); article.setContent(i + "我们希望我们的搜索解决方案要快,我们希望有一个零配置和一个完全免费的搜索模式,我们希望能够简单地使用JSON通过HTTP的索引数据,我们希望我们的搜索服务器始终可用,我们希望能够一台开始并扩展到数百,我们要实时搜索,我们要简单的多租户,我们希望建立一个云的解决方案。Elasticsearch旨在解决所有这些问题和更多的问题。"); // 建立文档 client.prepareIndex("blog2", "article", article.getId().toString()) //.setSource(objectMapper.writeValueAsString(article)).get(); .setSource(objectMapper.writeValueAsString(article).getBytes(),XContentType.JSON).get(); } //释放资源 client.close(); }
2、分页查询
@Test //分页查询 public void test10() throws Exception{ // 创建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("article") .setQuery(QueryBuilders.matchAllQuery());//默认每页10条记录 // 查询第2页数据,每页20条 //setFrom():从第几条开始检索,默认是0。 //setSize():每页最多显示的记录数。 searchRequestBuilder.setFrom(0).setSize(5); 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(); }
七、查询结果高亮操作
1、什么是高亮显示
在进行关键字搜索时,搜索出的内容中的关键字会显示不同的颜色,称之为高亮
百度搜索关键字"传智播客"
京东商城搜索"笔记本"
2、高亮显示的html分析
通过开发者工具查看高亮数据的html代码实现:
ElasticSearch可以对查询出的内容中关键字部分进行标签和样式的设置,但是你需要告诉ElasticSearch使用什么标签对高亮关键字进行包裹
3、高亮显示代码实现
@Test //高亮查询 public void test11() throws Exception{ // 创建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("article") .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("遍历高亮集合,打印高亮片段:"); Text[] text = hit.getHighlightFields().get("title").getFragments(); for (Text str : text) { System.out.println(str); } } //释放资源 client.close(); }
标签:settings,编程,System,client,ElasticSearch,new,println,ElasticSearch18,out From: https://www.cnblogs.com/ajing2018/p/18215270