Elasticsearch基本概念
基于Spring Data ElasticSearch的配置
POM
<?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 https://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.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>test</groupId>
<artifactId>springboot-03-elasticsearch</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-03-elasticsearch</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
<version>5.3.3</version>
</dependency>
<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.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
spring:
elasticsearch:
jest:
uris: http://192.168.47.133:9200 # linux服务器地址
data:
elasticsearch:
cluster-name: my-application
cluster-nodes: 192.168.47.133:9300
Bean
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Article {
@JestId
private Integer id;
private String author;
private String title;
private String content;
}
@AllArgsConstructor
@NoArgsConstructor
@Data
@Document(indexName = "test",type = "book")
public class Book {
private Integer id;
private String bookName;
private String author;
}
Repository
public interface BookRep extends ElasticsearchRepository<Book,Integer> {
public List<Book> findByBookNameLike(String bookName);
}
Test
@SpringBootTest
class Springboot03ElasticsearchApplicationTests {
@Autowired
JestClient jestClient;
@Test
void contextLoads() {
//给es索引中创建一个文档
Article article = new Article();
article.setId(1);
article.setTitle("好消息");
article.setAuthor("张三");
article.setContent("hello world");
// 构建一个索引功能
Index index = new Index.Builder(article).index("test").type("news").build();
try {
jestClient.execute(index);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void search() {
String json = " \"query\":{ \"term\":{ \"content\":hello } }";
Search search = new Search.Builder(json).addIndex("test").addType("news").build();
try {
SearchResult result = jestClient.execute(search);
System.out.println(result.getJsonString());
} catch (IOException e) {
e.printStackTrace();
}
}
@Autowired
BookRep bookRep;
@Test
public void repositoryTest(){
Book book = new Book(1,"三国","罗贯中");
bookRep.index(book);
}
@Test
public void repositoryTest2(){
//Book book = new Book(1,"三国","罗贯中");
List<Book> books = bookRep.findByBookNameLike("三");
System.out.println(books);
}
}
/**
* springboot默认支持两种技术和ES交互
* 1.Jest(默认不生效),需要导入Jest的工具包
* 2.SpringData ElasticSearch:(es版本有可能不合适)
* Client节点信息
* ElasticSearchTemplate操作es
* 编写ElasticSearchRepository的子接口来操作es:
* interface BookRep extends ElasticsearchRepository<Book,Integer(主键类型)>
*/
@SpringBootApplication
public class Springboot03ElasticsearchApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot03ElasticsearchApplication.class, args);
}
}