代码编织梦想

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

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

1.mapping映射属性

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

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

例如下面的json文档:

{
    "age": 21,
    "weight": 52.1,
    "isMarried": false,
    "info": "Java程序员",
    "email": "dc@dcxuexi.cn",
    "score": [99.1, 99.5, 98.9],
    "name": {
        "firstName": "操",
        "lastName": "曹"
    }
}

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

  • age:类型为integer;参与搜索,因此需要indextrue;无需分词器
  • weight:类型为float;参与搜索,因此需要indextrue;无需分词器
  • isMarried:类型为boolean;参与搜索,因此需要indextrue;无需分词器
  • info:类型为字符串,需要分词,因此是text;参与搜索,因此需要indextrue;分词器可以用ik_smart
  • email:类型为字符串,但是不需要分词,因此是keyword;不参与搜索,因此需要indexfalse;无需分词器
  • score:虽然是数组,但是我们只看元素的类型,类型为float;参与搜索,因此需要indextrue;无需分词器
  • name:类型为object,需要定义多个子属性
    • name.firstName:类型为字符串,但是不需要分词,因此是keyword;参与搜索,因此需要indextrue;无需分词器
    • name.lastName:类型为字符串,但是不需要分词,因此是keyword;参与搜索,因此需要indextrue;无需分词器

2.索引库的CRUD

这里我们统一使用Kibana编写DSL的方式来演示。

环境:ElasticSearch7.X

2.1.创建索引库和映射

基本语法:

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

格式:

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

示例:

PUT /dcxuexi
{
  "mappings": {
    "properties": {
      "info":{
        "type": "text",
        "analyzer": "ik_smart"
      },
      "email":{
        "type": "keyword",
        "index": falsae
      },
      "name":{
        "properties": {
          "firstName": {
            "type": "keyword"
          }
        }
      },
      // ... 略
    }
  }
}

在这里插入图片描述

2.2.查询索引库

基本语法

  • 请求方式:GET

  • 请求路径:/索引库名

  • 请求参数:无

格式

GET /索引库名

示例

在这里插入图片描述

2.3.修改索引库

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

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

语法说明

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

示例

在这里插入图片描述

2.4.删除索引库

语法:

  • 请求方式:DELETE

  • 请求路径:/索引库名

  • 请求参数:无

格式:

DELETE /索引库名

kibana中测试:

在这里插入图片描述

2.5.总结

索引库操作有哪些?

  • 创建索引库:PUT /索引库名
  • 查询索引库:GET /索引库名
  • 删除索引库:DELETE /索引库名
  • 添加字段:PUT /索引库名/_mapping
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_37726813/article/details/129824717

SpringCloud——ElasticSearch的使用-爱代码爱编程

在项目中,门户网站展示我们提供的商品。这时,如果商品过多,就需要为普通用户提供一个搜索和查询的功能,ElasticSearch为我们提供了该服务。 1.导入jar包,创建启动类,配置application文件 <dependencies> <!--微服务基础依赖--> <dependenc

springCloud整合Elasticsearch 之 es相关概念-爱代码爱编程

ES中有几个基本概念:索引(index)、类型(type)、文档(document)、映射(mapping)等。  ES数据架构的主要概念(与关系数据库Mysql对比)   (1)关系型数据库中的数据库(DataBase),等价于ES中的索引(Index)  (2)一个数据库下面有N张表(Table),等价于1个索引Index下面有N多类型(Type)

springCloud整合Elasticsearch 之 Elasticsearch配置-爱代码爱编程

配置文件所在的目录路径如下:$ES_HOME/config/elasticsearch.yml。  $ES_HOME es安装目录。 下面介绍一些重要的配置项及其含义。 (1)cluster.name: elasticsearch 配置elasticsearch的集群名称,默认是elasticsearch。elasticsearch会自动发

springCloud整合Elasticsearch 之 Springboot整合ES-爱代码爱编程

引入依赖 maven         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-data-elasticsearch</

springCloud整合Elasticsearch 之 ES索引存储原理-爱代码爱编程

索引(Index)    ES将数据存储于一个或多个索引中,索引是具有类似特性的文档的集合。类比传统的关系型数据库领域来说,索引相当于SQL中的一个数据库,或者一个数据存储方案(schema)。索引由其名称(必须为全小写字符)进行标识,并通过引用此名称完成文档的创建、搜索、更新及删除操作。一个ES集群中可以按需创建任意数目的索引。   我们了解索引的

07SpringCloud-Elasticsearch-爱代码爱编程

1.初识Elasticsearch 什么是elasticsearch elasticsearch是一款非常强大的开源搜索引擎,可以帮助我们从海量数据中快速找到需要的内容 elasticsearch结合kibana、Logstash、 Beats, 也就是elastic stack(ELK)。被广泛应用在日志数据分析、实时监控等领域。 el

spring cloud 集成elasticsearch 实现findByIds查询-爱代码爱编程

我们使用elasticsearch常使用它的分词查询,但是因为业务需要也需要使用多id查询,elasticsearch提供了MultiGetRequest实现多ids,多索引查询: /** * 根据多个索引查询 */ public <T> List<T> findByIds(List<String

从零搭建springcloud项目- elasticsearch(7)-爱代码爱编程

1、springcloud集成es,es的搭建不多说,直接配置项目,依赖,es选择版本是7.7 <!--elasticsearch需要依赖--> <dependency> <groupId>org.elasticsearch.client</groupId>

spring cloud 整合elasticsearch 创建索引支持ik中文分词和拼音分词-爱代码爱编程

 环境:jdk1.8、spring cloud Greenwich.SR6、spring boot 2.1.9、elasticsearch-7.5.0(整合ik,拼音分词) 下载 elasticsearch: 官网:下载 Elastic 产品 | Elastic 如果不想自己集成分词器或者官网下载太慢可通过这个地址下载:elasticsearch下载

springcloud(五):elasticsearch搜索引擎_horinjsor的博客-爱代码爱编程

编写:HorinJsor 文章目录 一、ElasticSearch是什么?1.ES与Mysql概念对比 二、ElasticSearch环境和基础(索引库)1.安装ES2.部署kibana和安装IK分词

springcloud elasticsearch基础介绍-爱代码爱编程

 哈喽~大家好,这篇来看看Springcloud elasticsearch基础介绍。  🥇个人主页:个人主页​​​​​                🥈 系列专栏: 【微服务】         🥉与这篇相关的文章:               SpringCloud Sentinel 使用SpringCloud

spring5-爱代码爱编程

一、索引库操作 elasticsearch底层是基于lucene来实现的。Lucene是一个Java语言的搜索引擎类库,是Apache公司的顶级项目,由DougCutting于1999年研发。官网地址:https://lu

spring cloud如何集成elasticsearch_springcloud集成elasticsearch-爱代码爱编程

一、导入elasticsearch依赖 其他依赖自行导入 <dependencies> <dependency> <groupId>org.sp

springcloud:初识es(elasticsearch)_springcloud es-爱代码爱编程

1.1.了解ES(ElasticSearch) 1.1.1.ElasticSearch的作用 ElasticSearch是一款非常强大的开源搜索引擎,具备非常多强大功能,可以帮助我们从海量数据中快速找到需要的内容 例如