目录
一、Elasticseach介绍
1.简单介绍
2.对比关系:
3.详细说明:
4.查出数据的解释
二、SpringBoot集成Elasticseach
1.引入依赖
2.添加配置
3.创建pojo类与索引对应
4.SpringData封装了基础的增删改查,自定义增删改查
5.测试方法--增删改查
如果本篇博客对您有一定的帮助,大家记得留言+点赞+收藏哦。
一、Elasticseach介绍
1.简单介绍
官网:开源搜索:Elasticsearch、ELK Stack 和 Kibana 的开发者 | Elastichttps://www.elastic.co/cn/
ElasticSeach详细安装教程--图文介绍超详细:张有博-CSDN博客">ElasticSeach详细安装教程--图文介绍超详细_小小张自由—>张有博-CSDN博客ElasticSeach详细安装教程--图文介绍超详细。Elasticsearch 是一个分布式、RESTful 风格的搜索和数据分析引擎,能够解决不断涌现出的各种用例。 作为 Elastic Stack 的核心,它集中存储您的数据,帮助您发现意料之中以及意料之外的情况。Elasticsearch 是一个基于JSON的分布式搜索和分析引擎。https://blog.csdn.net/promsing/article/details/122722302
令人记忆深刻的口号:能够发现意料之中以及意料之外的情况
Elasticsearch也是基于Lucene的全文检索库,本质也是存储数据,很多概念与MySQL类似的。是一种全文检索技术。
Elasticsearch 是位于 Elastic Stack 核心的分布式搜索和分析引擎。Logstash 和 Beats 有助于收集、聚合和丰富您的数据并将其存储在 Elasticsearch 中。Kibana 使您能够以交互方式探索、可视化和分享对数据的见解,并管理和监控堆栈。Elasticsearch 是索引、搜索和分析魔法发生的地方。
Elasticsearch 是一个基于JSON的分布式搜索和分析引擎。
Elasticsearch是用Java语言开发的,并作为Apache许可条款下的开放源码发布,是一种流行的企业级搜索引擎。Elasticsearch用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。
2.对比关系:
索引(indices)--------------------------------Databases 数据库
类型(type)-----------------------------Table 数据表
文档(document)----------------Row 行
字段(Field)-------------------Columns 列
3.详细说明:
| 概念 | 说明 |
| 索引库(indices) | indices是index的复数,代表许多的索引, |
| 类型(type) | 类型是模拟mysql中的table概念,一个索引库下可以有不同类型的索引,比如商品索引,订单索引,其数据格式不同。不过这会导致索引库混乱,因此未来版本中会移除这个概念 |
| 文档(document) | 存入索引库原始的数据。比如每一条商品信息,就是一个文档 |
| 字段(field) | 文档中的属性 |
| 映射配置(mappings) | 字段的数据类型、属性、是否索引、是否存储等特性 |
4.查出数据的解释
took:查询花费时间,单位是毫秒
time_out:是否超时
_shards:分片信息
hits:搜索结果总览对象
total:搜索到的总条数
max_score:所有结果中文档得分的最高分
hits:搜索结果的文档对象数组,每个元素是一条搜索到的文档信息
_index:索引库
_type:文档类型
_id:文档id
_score:文档得分
_source:文档的源数据
二、SpringBoot集成Elasticseach
1.引入依赖
org.springframework.boot
spring-boot-starter-data-elasticsearch
org.springframework.boot
spring-boot-starter-test
test
2.添加配置
spring:
data:
elasticsearch:
cluster-name: elasticsearch
cluster-nodes: 192.168.7.132:9300
3.创建pojo类与索引对应
package com.leyou.elasticsearch.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 = "item", type = "docs", shards = 1, replicas = 0)
public class Item {
@Id
private Long id;
@Field(type = FieldType.Text,analyzer = "ik_max_word")
private String title;
@Field(type = FieldType.Keyword)
private String category;
@Field(type = FieldType.Keyword)
private String brand;
@Field(type = FieldType.Double)
private Double price;
@Field(type = FieldType.Keyword)
private String images;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getImages() {
return images;
}
public void setImages(String images) {
this.images = images;
}
public Item() {
}
public Item(Long id, String title, String category, String brand, Double price, String images) {
this.id = id;
this.title = title;
this.category = category;
this.brand = brand;
this.price = price;
this.images = images;
}
@Override
public String toString() {
return "Item{" +
"id=" + id +
", title='" + title + ''' +
", category='" + category + ''' +
", brand='" + brand + ''' +
", price=" + price +
", images='" + images + ''' +
'}';
}
}
4.SpringData封装了基础的增删改查,自定义增删改查
took:查询花费时间,单位是毫秒
time_out:是否超时
_shards:分片信息
hits:搜索结果总览对象
total:搜索到的总条数
max_score:所有结果中文档得分的最高分
hits:搜索结果的文档对象数组,每个元素是一条搜索到的文档信息
_index:索引库
_type:文档类型
_id:文档id
_score:文档得分
_source:文档的源数据
1.引入依赖
org.springframework.boot
spring-boot-starter-data-elasticsearch
org.springframework.boot
spring-boot-starter-test
test
2.添加配置
spring:
data:
elasticsearch:
cluster-name: elasticsearch
cluster-nodes: 192.168.7.132:9300
3.创建pojo类与索引对应
package com.leyou.elasticsearch.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 = "item", type = "docs", shards = 1, replicas = 0)
public class Item {
@Id
private Long id;
@Field(type = FieldType.Text,analyzer = "ik_max_word")
private String title;
@Field(type = FieldType.Keyword)
private String category;
@Field(type = FieldType.Keyword)
private String brand;
@Field(type = FieldType.Double)
private Double price;
@Field(type = FieldType.Keyword)
private String images;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getImages() {
return images;
}
public void setImages(String images) {
this.images = images;
}
public Item() {
}
public Item(Long id, String title, String category, String brand, Double price, String images) {
this.id = id;
this.title = title;
this.category = category;
this.brand = brand;
this.price = price;
this.images = images;
}
@Override
public String toString() {
return "Item{" +
"id=" + id +
", title='" + title + ''' +
", category='" + category + ''' +
", brand='" + brand + ''' +
", price=" + price +
", images='" + images + ''' +
'}';
}
}
4.SpringData封装了基础的增删改查,自定义增删改查
spring:
data:
elasticsearch:
cluster-name: elasticsearch
cluster-nodes: 192.168.7.132:9300
3.创建pojo类与索引对应
package com.leyou.elasticsearch.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 = "item", type = "docs", shards = 1, replicas = 0)
public class Item {
@Id
private Long id;
@Field(type = FieldType.Text,analyzer = "ik_max_word")
private String title;
@Field(type = FieldType.Keyword)
private String category;
@Field(type = FieldType.Keyword)
private String brand;
@Field(type = FieldType.Double)
private Double price;
@Field(type = FieldType.Keyword)
private String images;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getImages() {
return images;
}
public void setImages(String images) {
this.images = images;
}
public Item() {
}
public Item(Long id, String title, String category, String brand, Double price, String images) {
this.id = id;
this.title = title;
this.category = category;
this.brand = brand;
this.price = price;
this.images = images;
}
@Override
public String toString() {
return "Item{" +
"id=" + id +
", title='" + title + ''' +
", category='" + category + ''' +
", brand='" + brand + ''' +
", price=" + price +
", images='" + images + ''' +
'}';
}
}
4.SpringData封装了基础的增删改查,自定义增删改查
这里需要继承接口-ItemRepository
public interface ItemRepository extends ElasticsearchRepository- { List
- findByTitle(String title); List
- findByPriceBetween(Double d1,Double d2); }



