ES 基本数据与MySQL的对应关系: * 索引:相当于MySQL中很多个数据库 * 类型:相当于MySQL中的很多个表 * 文档:相当于MySQL中表中的很多条数据 * 属性:相当于MySQL中每条数据中有很多属性
elasticsearch是什么,elasticsearch怎么用 1. 参考官方文档:https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html 2. 参考之前学习的笔记:Elastic Stack——Elastic Stack简介和Elasticsearch核心详解 3. 参考之前的学习笔记:Elasticsearch 中文分词、全文搜索、分布式集群搭建和java客户端操作
SpringBoot默认使用SpringData ElasticSearch模块进行操作 SpringBoot默认支持两种技术来和ES交互; 1、Jest(默认不生效) 需要导入jest的工具包(io.searchbox.client.JestClient) 2、SpringData ElasticSearch 【ES版本有可能不合适】 版本适配说明:https://github.com/spring-projects/spring-data-elasticsearch
如果版本不适配: 1)、升级SpringBoot版本 2)、安装对应版本的ES:使用docker安装低版本(elsaticsearch2.4.6)的镜像: 下载镜像:docker pull elasticsearch:2.4.6 运行镜像:docker run -e ES_JAVA_OPTS="-Xms256m -Xms265m" -d -p 9201:9200 -p 9301:9300 --name ES02 5e9d896dc62c
<dependencies>
<!--SpringBoot默认使用SpringData ElasticSearch模块进行操作-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/io.searchbox/jest -->
<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
<version>5.3.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>4.1.0</version>
</dependency>
</dependencies>
spring.elasticsearch.jest.uris=http://192.168.147.131:9201
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=192.168.147.131:9301
注: 9301 是docker映射的 Java 客户端的端口。9201是docker映射的、支持 Restful HTTP 的接口。 更多的配置:
spring.data.elasticsearch.cluster-name Elasticsearch 集群名。(默认值: elasticsearch)
spring.data.elasticsearch.cluster-nodes 集群节点地址列表,用逗号分隔。如果没有指定,就启动一个客户端节点。
spring.data.elasticsearch.propertie 用来配置客户端的额外属性。
spring.data.elasticsearch.repositories.enabled 开启 Elasticsearch 仓库。(默认值:true。)
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;
}
}
@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot03ElasticApplicationTests {
@Autowired
JestClient jestClient;
@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("hello").type("news").build();
try {
//执行
jestClient.execute(index);
} catch (IOException e) {
e.printStackTrace();
}
}
//测试搜索
@Test
public void search(){
//查询表达式(json是elasticsearch的查询表达式)
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("hello").addType("news").build();
//执行
try {
SearchResult result = jestClient.execute(search);
System.out.println(result.getJsonString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
测试查询操作:
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "hello",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 + '\'' +
'}';
}
}
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);
}
相当于dao层,具体接口实现和规则可以参考官方文档:https://docs.spring.io/spring-data/elasticsearch/docs/3.0.6.RELEASE/reference/html/ * 编写测试方法
@Autowired
BookRepository bookRepository;
@Test
public void test02(){
Book book = new Book();
book.setId(1);
book.setBookName("西游记");
book.setAuthor("吴承恩");
bookRepository.index(book);
}
@Test
public void test03(){
for (Book book : bookRepository.findByBookNameLike("游")) {
System.out.println(book);
}
}
详细的SpringData ElasticSearch Template使用可以参考文档:https://docs.spring.io/spring-data/elasticsearch/docs/3.0.6.RELEASE/reference/html/