Elasticsearch学习笔记2 -- 索引库的操作

索引库操作

索引库就类似数据库表,mapping映射就类似表的结构。

我们要向es中存储数据,必须先创建“库”和“表”。

1.1.mapping映射属性

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

  • type:字段数据类型,常见的简单类型有:
    • 字符串:text(可分词的文本)、keyword(精确值,例如:品牌、国家、ip地址)
    • 数值:long、integer、short、byte、double、float、
    • 布尔:boolean
    • 日期:date
    • 对象:object
  • index:是否创建索引,默认为true
  • analyzer:使用哪种分词器
  • properties:该字段的子字段

例如下面的json文档:

{
    "age": 21,
    "weight": 52.1,
    "isMarried": false,
    "info": "黑马程序员Java讲师",
    "email": "zy@itcast.cn",
    "score": [99.1, 99.5, 98.9],
    "name": {
        "firstName": "云",
        "lastName": "赵"
    }
}

对应的每个字段映射(mapping):

  • age:类型为 integer;参与搜索,因此需要index为true;无需分词器

  • weight:类型为float;参与搜索,因此需要index为true;无需分词器

  • isMarried:类型为boolean;参与搜索,因此需要index为true;无需分词器

  • info:类型为字符串,需要分词,因此是text;参与搜索,因此需要index为true;分词器可以用ik_smart

  • email:类型为字符串,但是不需要分词,因此是keyword;不参与搜索,因此需要index为false;无需分词器

  • score:虽然是数组,但是我们只看元素的类型,类型为float;参与搜索,因此需要index为true;无需分词器

  • name:类型为object,需要定义多个子属性

    • name.firstName;类型为字符串,但是不需要分词,因此是keyword;参与搜索,因此需要index为true;无需分词器
    • name.lastName;类型为字符串,但是不需要分词,因此是keyword;参与搜索,因此需要index为true;无需分词器

2.2.索引库的CRUD

2.2.1.创建索引库和映射

基本语法:

  • 请求方式:PUT
  • 请求路径:/索引库名,可以自定义
  • 请求参数:mapping映射

格式:

PUT /索引库名称
{
  "mappings": {
    "properties": {
      "字段名":{
        "type": "text",
        "analyzer": "ik_smart"
      },
      "字段名2":{
        "type": "keyword",
        "index": "false"
      },
      "字段名3":{
        "properties": {
          "子字段": {
            "type": "keyword"
          }
        }
      },
      // ...略
    }
  }
}

示例:

PUT /student
{
  "mappings": {
    "properties": {
      "id":{
        "type": "keyword",
        "index": false
      },
      "sname":{
        "type": "keyword"
      },
      "age":{
        "type": "integer",
        "index": false
      }
    }
  }
}

2.2.2.查询索引库

基本语法

  • 请求方式:GET

  • 请求路径:/索引库名

  • 请求参数:无

格式

GET /索引库名

示例

GET /student

2.2.3.修改索引库

倒排索引结构虽然不复杂,但是一旦数据结构改变(比如改变了分词器),就需要重新创建倒排索引,这简直是灾难。因此索引库一旦创建,无法修改mapping

虽然无法修改mapping中已有的字段,但是却允许添加新的字段到mapping中,因为不会对倒排索引产生影响。

语法说明

PUT /索引库名/_mapping
{
  "properties": {
    "新字段名":{
      "type": "integer"
    }
  }
}

示例

PUT /student/_mapping
{
  "properties": {
    "add":{
      "type": "text"
    }
  }
}

2.2.4.删除索引库

语法:

  • 请求方式:DELETE

  • 请求路径:/索引库名

  • 请求参数:无

格式:

DELETE /索引库名

2.3.Java对应实现方式

搭建环境:

在elasticsearch提供的API中,与elasticsearch一切交互都封装在一个名为RestHighLevelClient的类中,必须先完成这个对象的初始化,建立与elasticsearch的连接。

1.引入es的RestHighLevelClient依赖:

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>

2.因为SpringBoot默认的ES版本是7.6.2,所以我们需要覆盖默认的ES版本:

<properties>
    <java.version>1.8</java.version>
    <elasticsearch.version>7.12.1</elasticsearch.version>
</properties>

3.创建配置类,因为要使用RestHighLevelClient来发送请求,所以要提前交给spring来管理,如下:

@Configuration
public class RestHighLevelClientConfig {
    @Bean
    public RestHighLevelClient client(){
        return new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://192.168.118.128:9200")
        ));
    }
}

创建测试类

1.创建测试类,首先注入client对象:

@Resource
private RestHighLevelClient client;

2.发送创建索引库请求:

@Test
void testCreateIndex() throws IOException {
    // 1.1 创建request对象,指定索引库名称
    CreateIndexRequest request = new CreateIndexRequest("student");
    // 1.2 请求参数,第一个参数为创建索引库的DSL语句
    request.source(MAPPING_TEMPLATE, XContentType.JSON);
    // 1.3 使用client对象发送创建请求
    client.indices().create(request, RequestOptions.DEFAULT);
}

MAPPING_TEMPLATE:常量,内容就是创建索引库文档的DSL语句,如下:

{
  "mappings": {
    "properties": {
      "id":{
        "type": "keyword",
        "index": false
      },
      "sname":{
        "type": "keyword"
      },
      "age":{
        "type": "integer",
        "index": false
      }
    }
  }
}

3.发送删除索引库请求:

@Test
void testDeleteIndex() throws IOException {
    // 1.1 创建request对象,指定要删除的索引库名称
    DeleteIndexRequest request = new DeleteIndexRequest("student");
    // 1.2 发送请求
    client.indices().delete(request, RequestOptions.DEFAULT);
}

4.发送查询索引库是否存在请求:

@Test
void testExistsIndex() throws IOException {
    // 1.创建查询request对象
    GetIndexRequest request = new GetIndexRequest("student");
    // 2.发送请求,调用exists方法
    boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
    // 3.输出
    System.out.println(exists ? "存在" : "不存在");
}

总结

JavaRestClient操作elasticsearch的流程基本类似。核心是client.indices()方法来获取索引库的操作对象。

索引库操作的基本步骤:

  • 初始化RestHighLevelClient
  • 创建XxxIndexRequest。XXX是Create、Get、Delete
  • 准备DSL( Create时需要,其它是无参)
  • 发送请求。调用RestHighLevelClient#indices().xxx()方法,xxx是create、exists、delete
原文地址:https://www.cnblogs.com/dcy521/p/15394273.html