SpringBoot集成ES

  1. 版本说明
    https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html
    SpringBoot版本2.7.x支持的最高ES版本为7.17.4
    版本选择:ES、kibana、IK均为7.17.4
    注意:kibana运行需要nodejs支持且存在版本兼容问题-->nodejs版本选择16.14.2
    SpringBoot集成ES_第1张图片
  2. 资源下载
    链接:https://pan.baidu.com/s/13oytXXCRHUeW6WkPtRAFeQ?pwd=jciq
    ES:https://www.elastic.co/cn/downloads/elasticsearch
    kibana:https://www.elastic.co/cn/downloads/kibana
    IK:https://github.com/medcl/elasticsearch-analysis-ik/releases
  3. 资源安装
    ES:https://blog.csdn.net/tongxin_tongmeng/article/details/126752683
    es-head:https://blog.csdn.net/tongxin_tongmeng/article/details/126766931
    kibana::https://blog.csdn.net/tongxin_tongmeng/article/details/126850376
    IK:https://blog.csdn.net/tongxin_tongmeng/article/details/126859129
    注意:ES配置elasticsearch.yml
    cluster.name: my-application
    node.name: node-1
    network.host: 0.0.0.0
    http.port: 9200
    cluster.initial_master_nodes: ["node-1"]
    http.cors.enabled: true
    http.cors.allow-origin: "*"
  4. 操作命令
    1.启动命令
    elasticsearch -d
    nohup kibana >/dev/null 2>&1 &
    nohup npm start >/dev/null 2>&1 &
    
    2.查看命令
    ps -ef | grep -i elasticsearch
    ps -ef | grep -i kibana
    ps -ef | grep -i grunt
    
    3.访问命令
    192.168.1.102:9200
    192.168.1.102:5601
    192.168.1.102:9100
  5. pom.xml
    
    
    	4.0.0
    	
    		org.springframework.boot
    		spring-boot-starter-parent
    		2.7.3
    		 
    	
    	com.es
    	es
    	0.0.1-SNAPSHOT
    	es
    	es
    	
    		1.8
    		7.17.4
    	
    	
    		
    			org.springframework.boot
    			spring-boot-starter-data-elasticsearch
    		
    		
    			org.springframework.boot
    			spring-boot-starter-web
    		
    
    		
    			org.springframework.boot
    			spring-boot-devtools
    			runtime
    			true
    		
    		
    			org.springframework.boot
    			spring-boot-configuration-processor
    			true
    		
    		
    			org.projectlombok
    			lombok
    			true
    		
    		
    			org.springframework.boot
    			spring-boot-starter-test
    			test
    		
    		
    			com.alibaba.fastjson2
    			fastjson2
    			2.0.12
    		
    		
    			
    			org.jsoup
    			jsoup
    			1.15.3
    		
    		
    		
    			org.springframework.boot
    			spring-boot-starter-thymeleaf
    		
    	
    
    	
    		
    			
    				org.springframework.boot
    				spring-boot-maven-plugin
    				
    					
    						
    							org.projectlombok
    							lombok
    						
    					
    				
    			
    		
    	
    
    
  6. ES配置
    package com.es.config;
    
    import org.apache.http.HttpHost;
    import org.elasticsearch.client.RestClient;
    import org.elasticsearch.client.RestHighLevelClient;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class EsConfig {
    	
    	@Bean
    	public RestHighLevelClient restHighLevelClient() {
    		RestHighLevelClient client = new RestHighLevelClient(
    			RestClient.builder(
    				new HttpHost("192.168.1.102", 9200, "http")
    			)
    		);
    		return client;
    	}
    
    }
    
  7. ES测试
    package com.es.entity;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import org.springframework.stereotype.Component;
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Student {
    	
    	private String id;
    	
    	private String name;
    	
    	private int age;
    	
    	private String phone;
    	
    }
    package com.es.entity;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class Good {
    	private String name;
    	private String price;
    	private String img;
    }
    package com.es;
    
    import com.alibaba.fastjson2.JSON;
    import com.es.entity.Good;
    import com.es.entity.Student;
    import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
    import org.elasticsearch.action.bulk.BulkRequest;
    import org.elasticsearch.action.bulk.BulkResponse;
    import org.elasticsearch.action.delete.DeleteRequest;
    import org.elasticsearch.action.delete.DeleteResponse;
    import org.elasticsearch.action.get.GetRequest;
    import org.elasticsearch.action.get.GetResponse;
    import org.elasticsearch.action.index.IndexRequest;
    import org.elasticsearch.action.index.IndexResponse;
    import org.elasticsearch.action.search.SearchRequest;
    import org.elasticsearch.action.search.SearchResponse;
    import org.elasticsearch.action.support.master.AcknowledgedResponse;
    import org.elasticsearch.action.update.UpdateRequest;
    import org.elasticsearch.action.update.UpdateResponse;
    import org.elasticsearch.client.RequestOptions;
    import org.elasticsearch.client.RestHighLevelClient;
    import org.elasticsearch.client.indices.CreateIndexRequest;
    import org.elasticsearch.client.indices.CreateIndexResponse;
    import org.elasticsearch.client.indices.GetIndexRequest;
    import org.elasticsearch.common.text.Text;
    import org.elasticsearch.core.TimeValue;
    import org.elasticsearch.index.query.MatchAllQueryBuilder;
    import org.elasticsearch.index.query.QueryBuilders;
    import org.elasticsearch.index.query.TermQueryBuilder;
    import org.elasticsearch.search.SearchHit;
    import org.elasticsearch.search.builder.SearchSourceBuilder;
    import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
    import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
    import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
    import org.elasticsearch.xcontent.XContentType;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.Map;
    import java.util.concurrent.TimeUnit;
    
    @SpringBootTest
    class EsApplicationTests {
    	
    	@Autowired
    	RestHighLevelClient client;
    	
    	/**
    	 * 创建索引
    	 */
    	@Test
    	void createIndex() throws Exception {
    		CreateIndexRequest request = new CreateIndexRequest("myindex");
    		CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
    		System.out.println(response);
    	}
    	
    	/**
    	 * 判断索引存在
    	 */
    	@Test
    	void existsIndex() throws Exception {
    		GetIndexRequest request = new GetIndexRequest("myindex");
    		boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
    		System.out.println(exists);
    	}
    	
    	/**
    	 * 删除索引
    	 */
    	@Test
    	void deleteIndex() throws Exception {
    		DeleteIndexRequest request = new DeleteIndexRequest("myindex");
    		AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
    		boolean acknowledged = response.isAcknowledged();
    		System.out.println(acknowledged);
    	}
    	
    	/**
    	 * 创建文档
    	 */
    	@Test
    	void createDoc() throws Exception {
    		Student student = new Student("111", "张三", 33, "111222333");	// 文档数据
    		
    		IndexRequest request = new IndexRequest("myindex");
    		request.id(student.getId());
    		request.source(JSON.toJSONString(student), XContentType.JSON);	// 文档数据添加到索引
    		
    		IndexResponse response = client.index(request, RequestOptions.DEFAULT);	// 执行
    		System.out.println(response.toString());
    		System.out.println(response.status());
    	}
    	
    	/**
    	 * 判断文档存在
    	 */
    	@Test
    	void existsDoc() throws Exception {
    		GetRequest request = new GetRequest("myindex", "111");
    		request.fetchSourceContext(new FetchSourceContext(false));
    		boolean exists = client.exists(request, RequestOptions.DEFAULT);
    		System.out.println(exists);
    	}
    	
    	/**
    	 * 获取文档
    	 */
    	@Test
    	void getDoc() throws Exception {
    		GetRequest request = new GetRequest("myindex", "111");
    		request.fetchSourceContext(new FetchSourceContext(false));
    		GetResponse response = client.get(request, RequestOptions.DEFAULT);
    		String source = response.getSourceAsString();
    		System.out.println(source);
    		System.out.println(response);
    	}
    	
    	/**
    	 * 删除文档
    	 */
    	@Test
    	void deleteDoc() throws Exception {
    		DeleteRequest request = new DeleteRequest("myindex", "111");
    		request.timeout("2s");
    		DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
    		System.out.println(response);
    	}
    	
    	/**
    	 * 更新文档
    	 */
    	@Test
    	void updateDoc() throws Exception {
    		UpdateRequest request = new UpdateRequest("myindex", "111");
    		request.timeout("2s");
    		
    		Student student = new Student("111", "张四", 44, "444555666");
    		request.doc(JSON.toJSONString(student), XContentType.JSON);
    		UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
    		System.out.println(response);
    	}
    	
    	/**
    	 * 批量操作文档
    	 */
    	@Test
    	void bulkOperateDoc() throws Exception {
    		ArrayList studentList = new ArrayList<>();
    		studentList.add(new Student("1", "一桶", 11, "aaa"));
    		studentList.add(new Student("2", "二条", 22, "sss"));
    		studentList.add(new Student("3", "三万", 33, "ddd"));
    		studentList.add(new Student("9", "发财", 44, "fff"));
    		studentList.add(new Student("4", "白板", 55, "ggg"));
    		studentList.add(new Student("5", "东风", 66, "hhh"));
    		studentList.add(new Student("6", "南风", 77, "jjj"));
    		studentList.add(new Student("7", "西风", 88, "kkk"));
    		studentList.add(new Student("8", "北风", 99, "lll"));
    		
    		BulkRequest request = new BulkRequest();
    		request.timeout("30s");
    		
    		// 批量创建文档
    		for (Student student : studentList) {
    			request.add(new IndexRequest("myindex").id(student.getId()).source(JSON.toJSONString(student), XContentType.JSON));
    		}
    		
    		// 批量更新文档
    //		for (Student student : studentList) {
    //			student.setAge(student.getAge()+100);	// 年龄+100
    //			request.add(new UpdateRequest("myindex", student.getId()).doc(JSON.toJSONString(student), XContentType.JSON));
    //		}
    		
    		// 批量删除文档
    //		for (Student student : studentList) {
    //			request.add(new DeleteRequest("myindex", student.getId()));
    //		}
    		
    		BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
    		System.out.println(response);
    	}
    	
    	/**
    	 * 文档查询
    	 */
    	@Test
    	void searchDoc() throws Exception {
    		SearchRequest request = new SearchRequest("myindex");
    		
    		SearchSourceBuilder builder = new SearchSourceBuilder();
    		// 超时时间
    		builder.timeout(new TimeValue(60, TimeUnit.SECONDS));
    		// 设置分页
    		builder.from(0);
    		builder.size(5);
    		// 精确匹配
    //		builder.query(QueryBuilders.matchPhraseQuery("name", "东风"));	// 查到
    		builder.query(QueryBuilders.matchPhraseQuery("name", "风"));		// 查到
    //		builder.query(QueryBuilders.matchPhraseQuery("age", "111"));	// 查到
    //		builder.query(QueryBuilders.matchPhraseQuery("age", "1"));		// 查不到
    //		builder.query(QueryBuilders.rangeQuery("age").gt(130).lt(180));	// 查到
    		// 高亮显示
    		HighlightBuilder hBuild = new HighlightBuilder();
    		hBuild.field("name");
    		hBuild.requireFieldMatch(false);	// 单条结果只高亮一个
    		hBuild.preTags("");
    		hBuild.postTags("");
    		builder.highlighter(hBuild);
    		
    		request.source(builder);
    		
    		SearchResponse response = client.search(request, RequestOptions.DEFAULT);
    		// 结果数据
    		for (SearchHit hit : response.getHits()) {
    			Map map = hit.getSourceAsMap();
    			System.out.println(map);
    		}
    		// 高亮数据
    		for (SearchHit hit : response.getHits()) {
    			Map map = hit.getHighlightFields();
    			System.out.println(map);
    		}
    		// 高亮数据解析到结果数据中
    		for (SearchHit hit : response.getHits()) {
    			Map map = hit.getSourceAsMap();	// {phone=hhh, name=东风, id=5, age=166}
    			Map highlightFields = hit.getHighlightFields();	// {name=[name], fragments[[东]]}
    			HighlightField field = highlightFields.get("name");
    			if (field!=null) {
    				Text[] fragments = field.fragments();
    				StringBuilder sb = new StringBuilder();
    				for (Text fragment : fragments) {
    					sb.append(fragment);
    				}
    				map.put("name", sb.toString());	// {phone=hhh, name=东, id=5, age=166}
    			}
    			System.out.println(map);
    		}
    	}
    	
    	/**
    	 * jsoup获取网页数据并存入ES
    	 */
    	@Test
    	void jsoupCreateDoc() throws Exception {
    		ArrayList goodList = new ArrayList<>();
    		// jsoup获取数据并存入集合
    		Document document = Jsoup.connect("https://search.jd.com/Search?keyword=oracle").get();
    		Elements elements = document.getElementById("J_goodsList").getElementsByTag("li");
    		for (Element e : elements) {
    			String name = e.getElementsByClass("p-name").eq(0).text();
    			String price = e.getElementsByClass("p-price").eq(0).text();
    			String img = e.getElementsByTag("img").eq(0).attr("data-lazy-img");
    			goodList.add(new Good(name, price, img));
    		}
    		// 集合数据存入ES的good_index索引下
    		BulkRequest request = new BulkRequest();
    		request.timeout("1m");
    		for (Good good : goodList) {
    			request.add(new IndexRequest("good_index").source(JSON.toJSONString(good), XContentType.JSON));
    		}
    		BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
    		System.out.println(!response.hasFailures());
    	}
    
    }
    
    SpringBoot集成ES_第2张图片
    SpringBoot集成ES_第3张图片
    SpringBoot集成ES_第4张图片
    SpringBoot集成ES_第5张图片
    SpringBoot集成ES_第6张图片

你可能感兴趣的:(springboot,elasticsearch)