首页 > 技术文章 > elasticsearch数据库

caicai920 2021-08-16 17:33 原文

Elasticsearch数据库

简介

Elasticsearch 最强大就就是它检索的索引能力。Elasticsearch是面向文档型数据库,一条数据在这里就是一个文档,用JSON作为文档序列化的格式

传统创建表与Elasticsearch 见表区别

关系数据库 ⇒ 数据库 ⇒ 表 ⇒ 行 ⇒ 列(Columns)

Elasticsearch ⇒ 索引(Index) ⇒ 类型(type) ⇒ 文档(Docments) ⇒ 字段(Fields)

入门案例

Springboot整合 添加相关依赖

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

类注解

Spring Data通过注解来声明字段的映射属性,有下面的三个注解:

  • @Document 作用在类,标记实体类为文档对象,一般前两个属性
    • indexName:对应索引库名称
    • type:对应在索引库中的类型
    • shards:分片数量,
    • replicas:副本数量
  • @Id 作用在成员变量,标记一个字段作为id主键
  • @Field 作用在成员变量,标记为文档的字段,并指定字段映射属性:
    • type:字段类型,是是枚举:FieldType,可以是text、long、short、date、integer、object等
    • text:存储数据时候,会自动分词,并生成索引
    • keyword:存储数据时候,不会分词建立索引
    • Numerical:数值类型,分两类
      • 基本数据类型:long、interger、short、byte、double、float、half_float
      • 浮点数的高精度类型:scaled_float
        • 需要指定一个精度因子,比如10或100。elasticsearch会把真实值乘以这个因子后存储,取出时再还原。
    • Date:日期类型
      • elasticsearch可以对日期格式化为字符串存储,但是建议我们存储为毫秒值,存储为long,节省空间。
  • index:是否索引,布尔类型,默认是true
  • store:是否存储,布尔类型,默认是false
  • analyzer:分词器名称
@Document(indexName = "product",type = "docs", shards = 1, replicas = 0)
public class Product{
    @Id
    private Long id;
    @Field(type = FieldType.Text, analyzer = "ik_max_word")
    private String productname; //产品名称
    @Field(type = FieldType.Keyword)
    private String category;// 分类
    @Field(type = FieldType.Double)
    private Double price; // 价格
    @Field(index = false, type = FieldType.Keyword)
    private String images; // 产品图片地址
}

ElasticsearchTemplate中提供了创建索引的API:

@Test
public void createIndex() {
    // 创建索引,会根据Item类的@Document注解信息来创建
    esTemplate.createIndex(product.class);
    // 配置映射,会根据Item类中的id、Field等字段来自动完成映射
    esTemplate.putMapping(product.class);
    // 删除索引
    esTemplate.deleteIndex(product.class);
    // 根据索引名字删除
    //esTemplate.deleteIndex("product");
}

操作数据

public interface ProductRepository extends ElasticsearchRepository<Item,Long> {
}

@Autowired
private ProductRepository productRepository;

@Test
public void index() {
	//添加一个
    Product= new Product(1L, "欧内各个手机7", " 手机","欧内各个", 3499.00, "http://xxxxxxxx/13123.jpg");
    productRepository.save(item);

	//添加多个
	List<Item> list = new ArrayList<>();
    list.add(new Item(2L, "手机橘子", " 手机", "橘子", 3699.00, "http://xxxxx/321321321321.jpg"));
    list.add(new Item(3L, "华为1110", " 手机", "华为", 4499.00, "http://xxxxxxx/13321321ss3.jpg"));
    productRepository.saveAll(list);

	//elasticsearch中本没有修改,它的是该是先删除在新增,修改和新增是同一个接口,区分的依据就是id。
	Item item = new Item(1L, "苹果7p", " 手机", "苹果", 3499.00, "http://xxxxxxx/21421421.jpg");
    productRepository.save(item);

	//查询所有
	Iterable<Product> list = this.productRepository.findAll();
    // 对某字段排序查找所有 Sort.by("price").descending() 降序 , Sort.by("price").ascending():升序
    Iterable<Product> list = this.productRepository.findAll(Sort.by("price").ascending());
    for (Product p:list){
        System.out.println(p);
    }
}

推荐阅读