elasticsearch基础

elasticsearch基础

基础概念

elasticsearch中的文档数据会被序列化为json格式后存储在elasticsearch中。

索引(index):相同类型的文档的集合

映射(mapping):索引中文档的字段约束信息,类似表的结构约束

文档(Document),就是一条条的数据,类似数据库中的行(Row),文档都是JSON格式

字段(Field),就是JSON文档中的字段,类似数据库中的列(Column)

DSL是elasticsearch提供的JSON风格的请求语句,用来操作elasticsearch,实现CRUD

文档和词条?

每一条数据就是一个文档 对文档中的内容分词,得到的词语就是词条

正向索引?

于文档id创建索引。根据id查询快,但是查询词条时必须先找到文档,而后判断是否包含词条

倒排索引?

对文档内容分词,对词条创建索引,并记录词条所在文档的id。查询时先根据词条查询到文档id,而后根据文档id查询文档

IK分词器(采用正向迭代最细粒度算法)

中文分词往往需要根据语义分析,比较复杂,这就需要用到中文分词器,例如IK分词器。

分词器的作用是什么?

创建倒排索引时,对文档分词 用户搜索时,对输入的内容分词

IK分词器有几种模式?

ik_smart:智能切分,粗粒度 ik_max_word:最细切分,细粒度IK分词器

如何拓展分词器词库中的词条?

利用config目录的IkAnalyzer.cfg.xml文件添加拓展词典 在词典中添加拓展词条

Mapping映射属性

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

type:字段数据类型,

常见的简单类型有: 字符串:text(可分词的文本)、keyword(精确值,例如:品牌、国家、ip地址)

数值:long、integer、short、byte、double、float

布尔:boolean

日期:date

对象:object

index:是否创建索引,默认为true

analyzer:使用哪种分词器

properties:该字段的子字段

索引库操作

创建索引库和mapping的请求语法如下:

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

创建索引库

复制代码
PUT /wbn
{
  "mappings": {
    "properties": {
      "info":{
        "type": "text",
        "analyzer": "ik_smart",
        "index": true
      },
      "age":{
        "type": "byte",
        "index": true
      },
      "email":{
        "type": "keyword",
        "index": false
      },
      "name":{
        "type": "object",
        "properties": {
          "firstname":{
            "type": "keyword"
          },
          "lastname":{
            "type": "keyword"
          }
        }
      }
    }
  }
}

查询索引库操作

复制代码
GET /wbn

{
  "wbn" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "age" : {
          "type" : "byte"
        },
        "email" : {
          "type" : "keyword",
          "index" : false
        },
        "info" : {
          "type" : "text",
          "analyzer" : "ik_smart"
        },
        "name" : {
          "properties" : {
            "firstname" : {
              "type" : "keyword"
            },
            "lastname" : {
              "type" : "keyword"
            }
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "wbn",
        "creation_date" : "1729975477550",
        "number_of_replicas" : "1",
        "uuid" : "hiGQDD0BTV-ApQBQpZ5NlQ",
        "version" : {
          "created" : "7120199"
        }
      }
    }
  }
}

删除索引库

复制代码
DELETE /wbn

{
  "acknowledged" : true
}

索引库修改

以上对比添加字段可以修改字段不行。

索引库操作有哪些?

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

查询索引库:GET /索引库名

删除索引库:DELETE /索引库名

添加字段:PUT /索引库名/_mapping

文档操作

新增文档

复制代码
POST /索引库名/_doc/文档id
{
    "字段1": "值1",
    "字段2": "值2",
    "字段3": {
        "子属性1": "值3",
        "子属性2": "值4"
    },
    // ...
}

POST /wbn/_doc/1
{
  "info":"AAAA超级景区",
  "email":"[email protected]",
  "name":
  {
    "firstname":"西",
    "lastname":"安"
  }
}

查询文档

GET /索引库名/_doc/文档id

复制代码
GET /wbn/_doc/1

删除文档

DELETE /索引库名/_doc/文档id

复制代码
DELETE /wbn/_doc/1

修改文档

全量修改:会删除旧文档,添加新文档

格式

复制代码
PUT /索引库名/_doc/文档id
{
    "字段1": "值1",
    "字段2": "值2",
    // ... 略
}

样例

复制代码
PUT /wbn/_doc/1
{
   "info":"AAAA超级景区",
  "email":"[email protected]",
  "name":
  {
    "firstname":"西",
    "lastname":"安"
  }
}

增量修改,修改指定字段值

格式

复制代码
POST /索引库名/_update/文档id
{
    "doc": {
         "字段名": "新的值",
    }
}

样例

复制代码
POST /wbn/_update/1
{
  "doc":{
    "info":"AAAAA兵马俑超级景区1"
  }
   
}

文档操作有哪些?

创建文档:POST /索引库名/_doc/文档id { json文档 }

查询文档:GET /索引库名/_doc/文档id

删除文档:DELETE /索引库名/_doc/文档id

修改文档:

全量修改:PUT /索引库名/_doc/文档id { json文档 }

增量修改:POST /索引库名/_update/文档id { "doc": {字段}}

批量处理

elasticsearch中允许通过一次请求中携带多次文档操作,也就是批量处理,语法格式如下:

复制代码
POST /_bulk
{ "index" : { "_index" : "索引库名", "_id" : "1" } }
{ "字段1" : "值1", "字段2" : "值2" }
{ "index" : { "_index" : "索引库名", "_id" : "1" } }
{ "字段1" : "值1", "字段2" : "值2" }
{ "index" : { "_index" : "索引库名", "_id" : "1" } }
{ "字段1" : "值1", "字段2" : "值2" }
{ "delete" : { "_index" : "test", "_id" : "2" } }
{ "update" : {"_id" : "1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }

批量新增

复制代码
POST /_bulk
{"index":{"_index":"wbn","_id":2}}
{"info":"AAAA超级景区A","email":"[email protected]","name":{"firstname":"西", "lastname":"安"}}
{"index":{"_index":"wbn","_id":3}}
{"info":"AAAA超级景区B","email":"[email protected]","name":{"firstname":"西", "lastname":"安"}}

批量删除

复制代码
POST /_bulk
{"delete":{"_index":"wbn","_id":2}}
{"delete":{"_index":"wbn","_id":3}}
相关推荐
uyeonashi1 小时前
【Boost搜索引擎】构建Boost站内搜索引擎实践
开发语言·c++·搜索引擎
jiedaodezhuti6 小时前
ElasticSearch重启之后shard未分配问题的解决
笔记·elasticsearch
jiedaodezhuti7 小时前
为什么elasticsearch配置文件JVM配置31G最佳
大数据·jvm·elasticsearch
思通数据7 小时前
AI全域智能监控系统重构商业清洁管理范式——从被动响应到主动预防的监控效能革命
大数据·人工智能·目标检测·机器学习·计算机视觉·数据挖掘·ocr
lilye668 小时前
精益数据分析(55/126):双边市场模式的挑战、策略与创业阶段关联
大数据·人工智能·数据分析
white.tie8 小时前
Docker部署单节点Elasticsearch
elasticsearch·docker·jenkins
码上地球8 小时前
因子分析基础指南:原理、步骤与地球化学数据分析应用解析
大数据·数据挖掘·数据分析
胡小禾8 小时前
ES常识7:ES8.X集群允许4个 master 节点吗
大数据·elasticsearch·搜索引擎
火龙谷9 小时前
【hadoop】Kafka 安装部署
大数据·hadoop·kafka
强哥叨逼叨9 小时前
没经过我同意,flink window就把数据存到state里的了?
大数据·flink