Elaticsearch

2022-11-22 16:47:54 浏览数 (1)

(adsbygoogle = window.adsbygoogle || []).push({});

# 简介

分布式实时搜索和分析引擎,处理PB级别的结构化或非结构化数据

# 基本概念

文档型数据库,用JSON作为文档序列化的格式 索引对应数据库概念 可以用JAVA api,也可以用HTTP请求操作

# 安装

可视客户端kibana 默认英语 kibana-7.15.2-windows-x86_64configkibana.yml 末尾添加 i18n.locale: "zh-CN" bin目录下启动 打开后台url http://127.0.0.1:5601/app/home#/ 开发工具运行即可

分片 提高吞吐量

查看es中的索引: GET /_cat/indices?v

创建索引: PUT /索引名 PUT /products

PUT /products1 { "settings": { "number_of_shards": 1, #指定主分片数量 "number_of_replicas": 1 #指定副本分片数量 } } ES中索引健康状态,red(索引不可用),yellow(索引可用,存在风险),green(健康)

删除索引: DELETE /products

创建索引时候,一并创建映射: 常见类型: 字符串类型:keyword(关键词关键字)、text(一段文本) 数字类型:integer log 小数类型:float double 布尔类型:boolean 日期类型:date

代码语言:javascript复制
PUT /products
{
  "settings": {
    "number_of_replicas": 1,
    "number_of_shards": 1
  },
  "mappings": {
    "properties": {
      "id":{
        "type":"integer"
      },
      "title":{
        "type":"keyword"
      },
      "price":{
        "type":"double"
      },
      "create_at":{
        "type":"date"
      },
      "description":{
        "type":"text"
      }
    }
  }
}

查询索引映射信息: GET /索引名/_mapping GET /products/_mapping

文档操作:Json格式 添加文档操作 1、指定ID POST /products/_doc/1 { "id":1, "title":"薯条", "price":"10.5", "create_at":"2022-04-30", "description":"薯条真好吃啊" }

不指定ID POST /products/_doc/ { "title":"辣条", "price":"9.5", "create_at":"2022-04-30", "description":"辣条真是辣两头啊" }

# 删除文档(行)

根据ID 条件删除

# 更新文档(行)

代码语言:javascript复制
POST /products/_doc/1/_update
{
  "doc":{
    "price":"5.8",
    "title":"薯条"
  }
}

# 与spring boot集成

添加配置类

代码语言:javascript复制
package com.zr.config;

import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;

@Configuration
public class ElasticsearchConfig {
    @Bean
    RestHighLevelClient elasticsearchClient() {
        ClientConfiguration configuration = ClientConfiguration.builder()
                .connectedTo("127.0.0.1:9200")
                .build();
        RestHighLevelClient client = RestClients.create(configuration).rest();
        return client;
    }
}

注意:ID要使用String,日期要使用String 因为id如果没指定会随机生成类似"CJY5eoQBBwK1xW0ZOt5M"的

代码语言:javascript复制
package com.zr.vo;

import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;

@Document(indexName = "products")
public class Product {

    private  String id;

    private String title;

    private double price;

    @Field(name = "create_at")
    private String createAt;

    private String description;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public double getPrice() {
        return price;
    }

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

    public String getCreateAt() {
        return createAt;
    }

    public void setCreateAt(String createAt) {
        this.createAt = createAt;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

我的博客即将同步至腾讯云开发者社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=38dpnhkh4o8wo

0 人点赞