• 二、ElasticSearch中索引库与文档操作


    二、索引库与文档

    2.1 mapping映射属性

    • mapping映射属性

    官方网址:https://www.elastic.co/guide/en/elasticsearch/reference/7.12/dynamic-mapping.html

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

    • type: 字段数据类型,常见的简单类型有:
      • 字符串: text (可分词的文本);keyword (精确值,例如: 品牌、国家、ip地址)。如下:email字段就是不可拆分字段。info属于可拆分字段。
      • 数值: long、integer、short、byte、double、float、
      • 布尔: boolean
      • 日期: date
      • 对象: object
    • index:是否创建索引,默认为true
    • analyzer:使用哪种分词器
    • properties:该字段的子字段
    {
        "age": 21,
        "weight": 52.1,
        "info":"我们在学ES",
        "isMarried": false,
        "email":"zy@itcast.cn",
        "score": [99.199.598.9],
        "name":{
    		"firstName":"云",
            "LastName":"赵"
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2.2 操作索引库

    ES中通过Restful请求操作索引库、文档。请求内容用DSL语句来表示。创建索引库和mappingDSL语法如下:

    PUT /索引库名称
    "mappings": {
        "properties": {
            "字段名":{
                "type":"text",
                "analyzer":"ik_smart"
            }
    		"字段名2":{
            	"type":"keyword",
            	"index":"false"
        	}
    		"字段名3":{
        		"properties": {
        			"子字段": {"type":"keyword"}
    			}
    		}
    	},
    	// 略
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    索引库的CRUD

    # 创建索引库
    PUT /hhyy
    {
      "mappings": {
        "properties": {
          "info":{
            "type":"text",
            "analyzer": "ik_smart"
          },
          "email":{
            "type": "keyword",
            "index": false
          },
          "name":{
            "type": "object",
            "properties": {
              "firstName":{
                "type":"keyword"
              },
              "lastName":{
                "type":"keyword"
              }
            }
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    # 创建
    PUT /hhyy
    # 删除
    DELETE /hhyy
    # 查询
    GET /hhyy
    
    # 修改[添加新字段],只能添加,不能更新
    # 更新索引过于消耗资源
    PUT /hhyy/_mapping
    {
      "properties":{
        "age":{
          "type":"integer"
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    2.3 文档操作

    • 添加文档

    新增文档的DSL语法如下:

    POST /索引库名/_doc/文档id
    {
      "字段1":"值1",
      "字段2":"值2",
      "字段3":{
        "子属性1":"值3",
        "子属性2":"值4"
      },
    }		
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    示例:

    # 插入文档
    POST /hhyy/_doc/1
    {
      "info":"我们学习ES",
      "email":"hhh@out.com",
      "name":{
        "firstName":"Jack",
        "lastName":"hh"
      }
    }
    
    # 获取
    GET /hhyy/_doc/1
    # 删除
    DELETE /hhyy/_doc/1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    修改

    # 修改文档
    # 全量修改,会删除旧文档,添加新文档
    # 若id存在就修改,不存在就新增
    PUT /hhyy/_doc/2
    {
      "info":"我们学习ES1111",
      "email":"hhh@out.com",
      "name":{
        "firstName":"Jack",
        "lastName":"hh"
      }
    }
    GET /hhyy/_doc/2
    
    # 增量修改,
    POST /hhyy/_update/2
    {
      "doc":{
        "info":"我们都要努力学习ES"
      }
    }
    
    GET /hhyy/_doc/2
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    上一篇:一、初识 Elasticsearch:概念,安装,设置分词器

    下一篇:三、RestClient(writing)

  • 相关阅读:
    Oracle死锁问题: enq: TX - row lock contention
    详细Ubuntu16~20TLS安装NVIDIA驱动教程
    2023年8月京东大家电市场数据分析(京东数据开放平台)
    在 macOS 上使用 Homebrew 安装和配置 Python 及 Tk 库
    多个JSON文件中目标Key值检索
    Vxlan协议原理及基本配置——网络测试仪实操手册
    数据资产入表-数据治理-标签设计标准
    Centos7部署django项目
    【腾讯云 Cloud Studio 实战训练营】在Cloud Studio上使用React实现学生管理系统
    【C++】模板初阶
  • 原文地址:https://blog.csdn.net/Array_dear/article/details/133903994