SpringBoot-检索

xiaoxiao2025-07-12  4

SpringBoot整合Elasticsearch

SpringBoot默认支持两种技术来和ES交互;

1、Jest(默认不生效)需要导入jest的工具包(io.searchbox.client.JestClient)

1.1、加入依赖:

<!-- https://mvnrepository.com/artifact/io.searchbox/jest -->         <dependency>             <groupId>io.searchbox</groupId>             <artifactId>jest</artifactId>             <version>5.3.3</version>         </dependency>

1.2、配置服务:

spring.elasticsearch.jest.uris=http://192.168.142.201:9200

1.3、索引操作:

@Autowired JestClient jestClient;

 

public void saveTest() {         //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("atguigu").type("news").build();

        try {             //执行             jestClient.execute(index);         } catch (IOException e) {             e.printStackTrace();         } }

1.4、搜索操作:

public void searchTest(){

        //查询表达式         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("atguigu").addType("news").build();

        //执行         try {             SearchResult result = jestClient.execute(search);             System.out.println(result.getJsonString());         } catch (IOException e) {             e.printStackTrace();         } }

2、SpringData ElasticSearch

版本适配说明:https://github.com/spring-projects/spring-data-elasticsearch

1)、通过 ElasticsearchTemplate 组件操作ES; 2)、编写一个 ElasticsearchRepository 的子接口来操作ES;

2.1、引入依赖:

<!--SpringBoot默认使用SpringData ElasticSearch模块进行操作-->         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-data-elasticsearch</artifactId>         </dependency>

2.1、配置服务:

节点elasticsearch即为访问http://192.168.142.201:9200的返回页面中cluster_name的值.

spring.data.elasticsearch.cluster-name=elasticsearch spring.data.elasticsearch.cluster-nodes=192.168.142.201:9300

2.3、索引操作:

@Autowired BookRepository bookRepository;

 

public void saveTest(){

Book book = new Book();         book.setId(1);         book.setBookName("西游记");         book.setAuthor("吴承恩");         bookRepository.index(book);

}

public interface BookRepository extends ElasticsearchRepository<Book,Integer> {

}

@Document(indexName = "atguigu",type = "book") public class Book {     private Integer id;     private String bookName;     private String author;

    ...

}

2.4、搜索操作:

@Autowired BookRepository bookRepository;

 

public void searchTest(){

        for (Book book : bookRepository.findByBookNameLike("游")) {             System.out.println(book);         }

}

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);

}

 

转载请注明原文地址: https://www.6miu.com/read-5032959.html

最新回复(0)