ElasticSearch学习之索引库,文档操作以及RestClient使用

索引库操作

mapping映射属性

mapping是对索引库中文档的约束,常见的mapping属性包括:

  • type:字段数据类型,常见的类型有:
    • 字符串:text(可分词文本),keyword(精确值)
    • 数值:long、integer、short、byte、double、float
    • 布尔:boolean
    • 日期:date
    • 对象:object
    • 地理坐标:geo_point、geo_shape
  • index:是都创建索引,默认为true
  • analyzer:使用哪种分词器
  • properties:该字段的子字段
{
	"id":"101",
	"name":"张三",
	"gender":"男",
	"phone":"18822346606",
	"account":1215524127,
	"email":"1215524127@qq.com",
	"birthday":"2000-08-21",
	"age":22
}

索引库的CRUD

ES通过Restful请求操作索引库、文档。请求内容用DSL语句来表示

  • 创建索引库
    语法:
PUT /索引库名
{
  "mappings":{
    "properties": {
      "字段1":{
        "type": "keyword"
      },
      "字段2":{
        "properties":{
        	"子字段1"{
        		"type": "keyword"
        	},
        	"子字段2"{
        		"type": "keyword"
        	}
        }
      },
      {。。。。。。}
    }
  }
}
DSL如下:创建一个名为product的索引
PUT /product
{
  "mappings":{
    "properties": {
      "id":{
        "type": "keyword"
      },
      "proName":{
        "type": "text",
        "analyzer": "ik_max_word",
        "copy_to": "all"
      },
      "proPrice":{
        "type": "text",
        "analyzer": "ik_max_word",
        "copy_to": "all"
      }
      "proPrice":{
        "type": "float"
      },
      "dateOfManufacture":{
        "type": "date"
      },
      "manufacturer":{
        "type": "keyword",
        "copy_to": "all"
      },
      "createTime":{
        "type": "date"
      },
      "lastModifyTime":{
        "type": "date"
      },
      "status":{
        "type": "boolean"
      },
      "all":{
        "type": "text",
        "analyzer": "ik_smart"
      }
    }
  }
}
  • 查看索引库
    语法:GET /indexName
    例:
GET /product

在这里插入图片描述

  • 删除索引库
    语法:DELETE /indexName
    DSL例:
DELETE /product
  • 修改索引库
    索引库和mapping一旦创建无法修改,但是可以添加新的字段,语法如下:
PUT /索引库名/_mapping
{
  "properties": {
    "新字段名":{
      "type": "integer"
    }
  }
}

例:

PUT /product/_mapping
{
  "properties":{
    "address":{
      "type":"text",
      "analyzer":"ik_smart"
    }
  }
}

文档操作

增删查文档

1,新增文档

POST /product/_doc/1001
{
    "id":"1001",
    "proName":"干脆面",
    "proType":"零食方便面",
    "proPrice":1.5,
    "dateOfManufacture":"2022-07-15 17:57:03",
    "manufacture":"东厂",
    "createTime":"2022-07-16 17:57:20",
    "lastModifyTime":"2022-07-17 17:57:25",
    "status":true
}

2,查询文档

GET /product/_doc/1001

3,删除文档

DELETE /product/_doc/1001

修改文档

1,全量修改

PUT /product/_doc/1001
{
    "id":"1001",
    "proName":"干脆面",
    "proType":"零食方便面",
    "proPrice":1.5,
    "dateOfManufacture":"2022-07-15 17:57:03",
    "manufacture":"东厂",
    "createTime":"2022-07-16 17:57:20",
    "lastModifyTime":"2022-07-17 17:57:25",
    "status":true
}

2,增量修改

POST /product/_update/1001
{
    "doc":{
      "proPrice":1.9
    }
}

RestClient操作

什么是RestClient?

RestClient是ES官方提供了各种不同语言的客户端,用来操作ES。这些客户端的本质就是组装DSL语句,通过http请求发送给ES。官方文档地址:https://www.elastic.co/guide/en/elasticsearch/client/index.html

准备工作

数据库表:
在这里插入图片描述
项目结构:
在这里插入图片描述
注意一点因为SpringBoot默认的ES版本是7.6.2,所以我们需要覆盖默认的ES版本:
在这里插入图片描述
初始化RestHighLevelClient:为了方便测试,我就直接写在测试里面了,执行run,没有报错则说明初始化成功。
在这里插入图片描述

操作索引库

1,创建索引库

/**
 * 创建索引库
 * @throws IOException
 */
@Test
void createProductIndex() throws IOException {
    //1,创建Request对象
    CreateIndexRequest request = new CreateIndexRequest("product");
    //2,准备请求的参数:DSL语句
    request.source(PRODUCT_CREATE_MAPPING, XContentType.JSON);
    //3,发送请求
    client.indices().create(request, RequestOptions.DEFAULT);
}

代码中的索引库创建常量就是DSL语句的这一部分:
在这里插入图片描述
2,删除索引库

/**
 * 删除索引库
 * @throws IOException
 */
@Test
void deleteHotelIndex() throws IOException {
    //1,创建Request对象
    DeleteIndexRequest request = new DeleteIndexRequest("product");
    //2,发送请求
    client.indices().delete(request, RequestOptions.DEFAULT);
}

3,判断索引库是否存在

/**
 * 判断索引库是否存在
 * @throws IOException
 */
@Test
void existsHotelIndex() throws IOException {
    //1,创建Request对象
    GetIndexRequest request = new GetIndexRequest("product");
    //2,发送请求
    boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
    System.out.println(exists?"索引库存在!":"索引库不存在!");
}

操作文档

1,创建文档

/**
 * 创建文档
 * @throws IOException
 */
@Test
void docCreate() throws IOException {
    //获取数据
    Product product = productService.getById(1001L);
    //获取请求对象
    IndexRequest request = new IndexRequest("product").id(product.getId().toString());
    //准备JSON文档
    request.source(JSON.toJSONString(product),XContentType.JSON);
    //发送请求
    client.index(request,RequestOptions.DEFAULT);
}

2,查询文档

/**
 * 查询文档
 * @throws IOException
 */
@Test
void docGet() throws IOException {
    //获取请求对象
    GetRequest request = new GetRequest("product","1001");
    //发送请求
    GetResponse response = client.get(request, RequestOptions.DEFAULT);
    //解析结果
    String json = response.getSourceAsString();
    System.out.println(json);
}

3,更新文档

/**
 * 修改文档
 * @throws IOException
 */
@Test
void docUpdate() throws IOException {
    //获取请求对象
    UpdateRequest request = new UpdateRequest("product","1001");
    //准备参数
    request.doc(
            "proPrice",1.9,
            "proType","小吃干脆面"
    );
    //发送请求
    client.update(request,RequestOptions.DEFAULT);
}

4,删除文档

/**
 * 删除文档
 * @throws IOException
 */
@Test
void docDelete() throws IOException {
    DeleteRequest request = new DeleteRequest("product", "1001");
    //发送请求
    client.delete(request,RequestOptions.DEFAULT);
}

5,文档批处理操作

/**
 * 文档批处理添加
 * @throws IOException
 */
@Test
void addDocBatch() throws IOException {
    //获取数据库数据
    List<Product> productList = productService.list();
    //创建bulk请求对象
    BulkRequest bulkRequest = new BulkRequest();
    //遍历操作
    for (Product product : productList) {
        //添加文档对象
        bulkRequest.add(new IndexRequest("product")
                .id(product.getId().toString())
                .source(JSON.toJSONString(product),XContentType.JSON));
    }
    //发送批处理请求
    client.bulk(bulkRequest,RequestOptions.DEFAULT);
}

需要源码可查看gitee源码中的es-learn文件夹

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值