一、数据存储结构

elasticsearch数据存储结构,springboot集成elasticsearch_java


结合数据库的结构理解起来就会比较清楚:

1)索引(Index)=>数据库(Database)。

2)类型(Type)=>表(Table)。

3)文档(Document)=>表中的一行记录(Row)。

4)属性(Field)=>字段列(Column)。

  1. 索引索引是含义相同的属性文档的集合,是Elasticsearch的一个逻辑存储,可以理解为关系型数据库中的数据库。
  2. 类型文档可以分组,比如员工信息。这种分组就叫类型,它是虚拟的逻辑分组,用来过滤文档数据。
  3. 文档文档是可以被索引的基本数据单位,存储在Elasticsearch中的主要实体叫文档,可以理解为关系型数据库中表的一行记录。

二、springboot集成elasticsearch

1.安装elasticsearch

ES的安装环境最好是类Linux操作系统,本书中使用mac。从ES 7.x版本开始,其发行的安装包中就已经内置了JDK。如果用户对JDK有独特的要求,可以修改其启动脚本进行依赖配置。为简单起见,本节安装ES时使用内置的JDK。

​brew安装jdk8​​安装brew包管理工具,就可以直接命令安装es

​es安装步骤、这个我失败了,我用下面的步骤成功​

es安装步骤

es启动失败,指定es启动jdk版本


上面的博客我都安装失败了

我安装成功用的这个方法

启动后无法访问

elasticsearch数据存储结构,springboot集成elasticsearch_Elastic_02

Linux系统安装,参考这本书

elasticsearch数据存储结构,springboot集成elasticsearch_spring_03


2.引入依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

elasticsearch数据存储结构,springboot集成elasticsearch_java_04

3.配置连接地址

elasticsearch数据存储结构,springboot集成elasticsearch_spring_05

spring.elasticsearch.rest.uris=http://127.0.0.1:9200

4.创建文档对象
创建实体对象类Book,然后使用@Document注解定义文档对象,示例代码如下:

package com.yangjunbo.JPADemo.pojo;

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

@Document( indexName = "book" , replicas = 0)
public class Book {
@Id
private Long id;
@Field(analyzer = "ik_max_word",type = FieldType.Text)
private String bookName;
@Field(type = FieldType.Keyword)
private String author;
private float price;
private int page;
@Field(type = FieldType.Keyword, fielddata = true)
private String category;

// 省略get、set方法


public Long getId() {
return id;
}

public void setId(Long 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;
}

public float getPrice() {
return price;
}

public void setPrice(float price) {
this.price = price;
}

public int getPage() {
return page;
}

public void setPage(int page) {
this.page = page;
}

public String getCategory() {
return category;
}

public void setCategory(String category) {
this.category = category;
}

public Book(){

}
public Book(Long id,String bookName, String author,float price,int page,String category) {
this.id = id;
this.bookName = bookName;
this.author = author;
this.price = price;
this.page = page;
this.category = category;
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder( "{\"Book\":{" );
sb.append( "\"id\":" )
.append( id );
sb.append( ",\"bookName\":\"" )
.append( bookName ).append( '\"' );
sb.append( ",\"page\":\"" )
.append( page ).append( '\"' );
sb.append( ",\"price\":\"" )
.append( price ).append( '\"' );
sb.append( ",\"category\":\"" )
.append( category ).append( '\"' );
sb.append( ",\"author\":\"" )
.append( author ).append( '\"' );
sb.append( "}}" );
return sb.toString();
}
}

elasticsearch数据存储结构,springboot集成elasticsearch_Elastic_06

5.创建操作的repository

package com.yangjunbo.JPADemo.repository;

import com.yangjunbo.JPADemo.pojo.Book;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import java.util.List;

public interface BookRepository extends ElasticsearchRepository<Book, Integer> {
List<Book> findByBookNameLike(String bookName);
}

elasticsearch数据存储结构,springboot集成elasticsearch_elasticsearch_07

通过上面的示例代码,我们发现其使用方式和JPA的语法是一样的。

6.创建测试方法测试

package com.yangjunbo.JPADemo;

import com.yangjunbo.JPADemo.pojo.Book;
import com.yangjunbo.JPADemo.repository.BookRepository;
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;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestSave {

@Autowired
private BookRepository repository;

@Test
public void testSave() {
Book book = new Book();
book.setId((long) 1);
book.setBookName("西游记");
book.setAuthor("吴承恩");
repository.save(book);
Book newbook=repository.findById(1).orElse(null);
System.out.println(newbook);
}
}

elasticsearch数据存储结构,springboot集成elasticsearch_java_08

elasticsearch数据存储结构,springboot集成elasticsearch_spring_09

结果表明索引数据保存成功,并且通过id能查询到保存的索引数据信息,说明在Spring Boot中成功集成Elasticsearch。7.遇到的问题

当使用springboot和es版本不匹配就会报下面的错

Unable to parse response body for Response{requestLine=PUT /book/_doc/1?timeout=1m HTTP/1.1, host=http://127.0.0.1:9200, response=HTTP/1.1 201 Created}

​​springboot和elasticsearch的版本​​ 如果启动7点几版本的es失败,提示jdk不清楚开发者,请注意下图的地方点击仍要打开。

elasticsearch数据存储结构,springboot集成elasticsearch_spring boot_10


参考书籍《springboot从入门到实战-章为忠著》