springboot+elasticsearch 刺骨的言语ヽ痛彻心扉 2023-06-10 09:27 23阅读 0赞 一、springboot+es整合代码 ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMyNDQ3MzAx_size_16_color_FFFFFF_t_70] pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.citydo</groupId> <artifactId>ccs-service-elastic</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>ccs-service-elastic</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.searchbox</groupId> <artifactId>jest</artifactId> <version>5.3.3</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> 配置`application.properties` spring.elasticsearch.jest.uris=http://118.24.44.169:9200 spring.data.elasticsearch.cluster-name=elasticsearch spring.data.elasticsearch.cluster-nodes=118.24.44.169:9301 启动类CcsServiceElasticApplication.java package com.citydo.ccsserviceelastic; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class CcsServiceElasticApplication { public static void main(String[] args) { SpringApplication.run(CcsServiceElasticApplication.class, args); } } BookRepository.java package com.citydo.ccsserviceelastic.repository; import com.citydo.ccsserviceelastic.bean.Book; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import java.util.List; public interface BookRepository extends ElasticsearchRepository<Book,Integer> { //参照 // https://docs.spring.io/spring-data/elasticsearch/docs/3.0.6.RELEASE/reference/html/ public List<Book> findByBookNameLike(String bookName); } Article.java package com.citydo.ccsserviceelastic.bean; import io.searchbox.annotations.JestId; public class Article { @JestId private Integer id; private String author; private String title; private String content; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } 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; } } Book.java package com.citydo.ccsserviceelastic.bean; import org.springframework.data.elasticsearch.annotations.Document; @Document(indexName = "index_test",type = "book") public class Book { private Integer id; private String bookName; private String author; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } @Override public String toString() { return "Book{" + "id=" + id + ", bookName='" + bookName + '\'' + ", author='" + author + '\'' + '}'; } } CcsServiceElasticApplicationTests.java package com.citydo.ccsserviceelastic; import com.citydo.ccsserviceelastic.bean.Article; import com.citydo.ccsserviceelastic.repository.BookRepository; import io.searchbox.client.JestClient; import io.searchbox.core.Index; import io.searchbox.core.Search; import io.searchbox.core.SearchResult; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.io.IOException; @RunWith(SpringRunner.class) @SpringBootTest public class CcsServiceElasticApplicationTests { @Autowired JestClient jestClient; @Autowired BookRepository bookRepository; @Test public void test02(){ // Book book = new Book(); // book.setId(1); // book.setBookName("西游记"); // book.setAuthor("吴承恩"); // bookRepository.index(book); for (com.citydo.ccsserviceelastic.bean.Book book : bookRepository.findByBookNameLike("游")) { System.out.println(book); } ; } @Test public void contextLoads() { //1、给Es中索引(保存)一个文档; Article article = new Article(); article.setId(1); article.setTitle("好消息"); article.setAuthor("zhangsan"); article.setContent("Hello World"); //构建一个索引功能 Index index = new Index.Builder(article).index("index_test").type("news").build(); try { //执行 jestClient.execute(index); } catch (IOException e) { e.printStackTrace(); } } //测试搜索 @Test public void search(){ //查询表达式 String json ="{\n" + " \"query\" : {\n" + " \"match\" : {\n" + " \"content\" : \"hello\"\n" + " }\n" + " }\n" + "}"; //更多操作:https://github.com/searchbox-io/Jest/tree/master/jest //构建搜索功能 Search search = new Search.Builder(json).addIndex("index_test").addType("news").build(); //执行 try { SearchResult result = jestClient.execute(search); System.out.println(result.getJsonString()); } catch (IOException e) { e.printStackTrace(); } } } [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMyNDQ3MzAx_size_16_color_FFFFFF_t_70]: /images/20230531/201edb4bf9114880bc1fcf0119f63ad1.png
还没有评论,来说两句吧...