spring boot 整合 elasticsearch 进行检索

  作者:记性不好的阁主

0、准备


使用spring初始化器,选择web和elasticsearch模块




I.使用jest操作elasticsearch 


导入依赖


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


106.54.196.44服务器上用docker 安装 elasticsearch


docker pull elasticsearch
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 --name ES01 elasticsearch


访问:http://106.54.196.44:9200/




安装成功


1、配置连接地址


application.properties


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



测试启动项目




打印地址说明启动成功


2、创建测试类Article




package com.laoxu.elastic.bean;

import io.searchbox.annotations.JestId;

public class Article {

@JestId
private Integer id;
private String author;
private String title;
private String content;

public Article() {
}

public Article(Integer id, String author, String title, String content) {
this.id = id;
this.author = author;
this.title = title;
this.content = 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;
}

@Override
public String toString() {
return "Article{" +
"id=" + id +
", author='" + author + '\'' +
", title='" + title + '\'' +
", content='" + content + '\'' +
'}';
}
}




给es保存一个文档


package com.laoxu.elastic;

import com.laoxu.elastic.bean.Article;
import io.searchbox.client.JestClient;
import io.searchbox.core.Index;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;


import java.io.IOException;



@SpringBootTest
class SpringBoot03ElasticApplicationTests {

@Autowired
JestClient jestClient;

@Test
void contextLoads() throws IOException {
//1、给es中索引(保存)一个文档
Article article = new Article(1,"张三","好消息","hello world");
//构建一个索引功能
Index index = new Index.Builder(article).index("laoxu").type("news").build();
jestClient.execute(index);
}

}


搜索文档


package com.laoxu.elastic;

import com.laoxu.elastic.bean.Article;
import io.searchbox.client.JestClient;
import io.searchbox.core.Index;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;


import java.io.IOException;



@SpringBootTest
class SpringBoot03ElasticApplicationTests {

@Autowired
JestClient jestClient;

@Test
void contextLoads() throws IOException {
//1、给es中索引(保存)一个文档
Article article = new Article(1,"张三","好消息","hello world");
//构建一个索引功能
Index index = new Index.Builder(article).index("laoxu").type("news").build();
jestClient.execute(index);
}


//搜索功能
@Test
void search() throws IOException {
String json = "{\n" +
" \"query\" : {\n" +
" \"match\" : {\n" +
" \"content\" : \"hello\"\n" +
" }\n" +
" }\n" +
"}";
//构建搜索功能
Search search = new Search.Builder(json).addIndex("laoxu").addType("news").build();

SearchResult result = jestClient.execute(search);
System.out.println(result.getJsonString());
}

}



II.使用springData操作elasticsearch 




1、配置application.properties


spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=106.54.196.44:9200



2、创建Book类


package com.laoxu.elastic.bean;

import org.springframework.data.elasticsearch.annotations.Document;

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

public Book() {
}

public Book(Integer id, String bookName, String author) {
this.id = id;
this.bookName = bookName;
this.author = 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 + '\'' +
'}';
}
}



3、创建Book管理类




package com.laoxu.elastic.repository;

import com.laoxu.elastic.bean.Book;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;



public interface BookRepository extends ElasticsearchRepository<Book,Integer> {

}



package com.laoxu.elastic;

import com.laoxu.elastic.bean.Article;
import com.laoxu.elastic.bean.Book;
import com.laoxu.elastic.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.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;


import java.io.IOException;



@SpringBootTest
class SpringBoot03ElasticApplicationTests {

@Autowired
JestClient jestClient;

@Autowired
BookRepository bookRepository;

@Test
void test02(){
Book book = new Book();
bookRepository.index(book);
}




}




相关推荐

评论 抢沙发

表情

分类选择