当前位置:首页> 正文

SpringBoot集成Elasticseach

SpringBoot集成Elasticseach
  一、Elasticseach介绍
 
  1.简单介绍
 
  官网:
 
  开源搜索:Elasticsearch、ELK Stack 和 Kibana 的开发者 | Elastic
 
  https://www.elastic.co/cn/
 
  ElasticSeach详细安装教程--图文介绍超详细:
 
  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.引入依赖
 
  <dependencies>
 
  <dependency>
 
  <groupId>org.springframework.boot</groupId>
 
  <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
 
  </dependency>
 
  <dependency>
 
  <groupId>org.springframework.boot</groupId>
 
  <artifactId>spring-boot-starter-test</artifactId>
 
  <scope>test</scope>
 
  </dependency>
 
  </dependencies>
 
  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;
 
  /**
 
  * 创建pojo类与索引对应
 
  *
 
  * @author Promsing(张有博)
 
  * @version 1.0.0
 
  * @since 2022/1/26 - 20:35
 
  */
 
  @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 + '\'' +
 
  '}';
 
  }
 
  }
展开全文阅读

相关内容