Elastic Search快速上手(2):将数据存入ES

前言

在上手使用前,需要先了解一些基本的概念。

推荐
可以到 https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html 阅读《Elastic Search 权威指南》,有非常详细和全面的说明。

ES中的一些概念

index(索引)

相当于mysql中的数据库

type(类型)

相当于mysql中的一张表

document(文档)

相当于mysql中的一行(一条记录)

field(域)

相当于mysql中的一列(一个字段)

节点

一个服务器,由一个名字来标识

集群

一个或多个节点组织在一起

分片

将一份数据划分为多小份的能力,允许水平分割和扩展容量。多个分片可以响应请求,提高性能和吞吐量。

副本

复制数据,一个节点出问题时,其余节点可以顶上。

倒排索引

可参考https://www.elastic.co/guide/cn/elasticsearch/guide/current/inverted-index.html。

索引&类型

对索引的基本操作

创建索引

通过以下命令可创建一个索引:

PUT job
{
  "settings":{
    "index":{
      "number_of_shards":5,
      "number_of_replicas":1
    }
  }
}

返回:

{
  "acknowledged": true,
  "shards_acknowledged": true
}

Elasticsearch 是利用分片将数据分发到集群内各处的。分片是数据的容器,文档保存在分片内,分片又被分配到集群内的各个节点里。
当你的集群规模扩大或者缩小时, Elasticsearch 会自动的在各节点中迁移分片,使得数据仍然均匀分布在集群里。

一个分片可以是 主 分片或者 副本 分片。 索引内任意一个文档都归属于一个主分片,所以主分片的数目决定着索引能够保存的最大数据量。

一个副本分片只是一个主分片的拷贝。 副本分片作为硬件故障时保护数据不丢失的冗余备份,并为搜索和返回文档等读操作提供服务。

在上面例子中,主分片为5,副本分片为1.

查看索引的信息

GET job

查看job这个索引的信息:

{
  "job": {
    "aliases": {},
    "mappings": {},
    "settings": {
      "index": {
        "creation_date": "1502342603160",
        "number_of_shards": "5",
        "number_of_replicas": "1",
        "uuid": "LGalsb3eRKeGb5SbWCxO8w",
        "version": {
          "created": "5010199"
        },
        "provided_name": "job"
      }
    }
  }
}

可以只查看某一项信息:

GET job/_settings

可以查看job这个索引的settings信息:

{
  "job": {
    "settings": {
      "index": {
        "creation_date": "1502342603160",
        "number_of_shards": "5",
        "number_of_replicas": "1",
        "uuid": "LGalsb3eRKeGb5SbWCxO8w",
        "version": {
          "created": "5010199"
        },
        "provided_name": "job"
      }
    }
  }
}

修改索引信息

例如,将副本分片数量修改为2:

PUT job/_settings
{
  "number_of_replicas":2
}

映射

在创建索引时,我们可以预先设定映射,规定好各个字段及其数据类型,便于es更好地进行管理。比如说,以文章库为例 ,一篇文章的关键词字段应当作为完整的词语,而文章的正文字段必须通过中文分词器进行分词。

通过设置映射mapping,可以告知es这些字段的规则。

更详细文档参见:https://www.elastic.co/guide/cn/elasticsearch/guide/current/mapping-intro.html

数据类型

Elasticsearch支持如下类型:

字符串: text, keyword(注:5之前的版本里有string类型,5之后不再支持此类型)
数字: byte, short, integer, long, float, double
布尔型:boolean
日期: date
复杂类型:如object, nested等

查看映射

输入

GET job/_mapping

可以查看job索引下的所有映射。

默认映射

在创建索引存入数据时,如果不指定类型,es会自动根据实际数据为其添加类型。
例如,通过下面的语句插入文档:

PUT job/type1/1
{
  "title":"abc",
  "words":123,
  "date":"2017-01-01",
  "isok":true
}

然后查看映射,结果为:

{
  "job": {
    "mappings": {
      "type1": {
        "properties": {
          "date": {
            "type": "date"
          },
          "isok": {
            "type": "boolean"
          },
          "title": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "words": {
            "type": "long"
          }
        }
      }
    }
  }
}

可见,es自动根据类型对字段进行了映射。

设置映射

在创建索引时,可以设置映射规则,具体格式形如上面查看映射时的返回结果。

PUT job
{
  "mappings":{
    "type2":{
      "properties":{
        "title":{
          "type":"keyword"
        },
        "salary":{
          "type":"integer"
        },
        "desc":{
          "type":"text",
          "analyzer": "ik_max_word"
        },
        "date":{
          "type":"date",
          "format":"yyyy-MM-dd"
        }
      }
    }
  }
}

注意,在上面为desc字段指定了analyzer,就是一个自定义分词器。在es-rtf中,默认给安装了ik_smart和ik_max_word两个分词器,区别在于后者会分出更多的词。
为text类型的字段会被进行分词,然后索引,而keyword字段不会被分词。

自动转换

创建索引和映射后,插入文档时,字段会自动转换成映射中规定的类型。比如,插入"123"到integer字段,会自动尝试对字符串进行类型转换。如果无法转换,则会报错,无法插入。

文档

一个“文档”即所谓的一条记录。可对文档进行增删改操作。

插入文档

可以指定文档id,即 PUT index_name/type_name/id。

PUT job/type2/1
{
  "title":"Python工程师",
  "salary":1000,
  "desc":"1. 参与devops相关系统开发,包括云资源管理平台,cmdb平台、资源申请流程、基础支撑平台开发;2. 参与公司业务系统及自动化运维平台的开发;3. 积累并规范化系统开发的最佳实践并文档化;4. 完善并遵守团队的编码规范,编写高质量、结构清晰、易读、易维护的代码。",
  "date":"2017-08-08"
}

返回:
{
"_index": "job",
"_type": "type2",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}

也可不指定id,则会自动分配id。注意这里要使用POST方式

POST job/type2/
{
  "title":"Python工程师2",
  "salary":1000,
  "desc":"1. 参与devops相关系统开发,包括云资源管理平台,cmdb平台、资源申请流程、基础支撑平台开发;2. 参与公司业务系统及自动化运维平台的开发;3. 积累并规范化系统开发的最佳实践并文档化;4. 完善并遵守团队的编码规范,编写高质量、结构清晰、易读、易维护的代码。",
  "date":"2017-08-08"
}

查看文档

只需通过GET方式查看,

GET job/type2/1

返回文档信息:

{
  "_index": "job",
  "_type": "type2",
  "_id": "1",
  "_version": 3,
  "found": true,
  "_source": {
    "title": "Java",
    "salary": 2000,
    "desc": "易维护的代码",
    "date": "2017-08-08"
  }
}

可以只查看_source中的部分字段:

GET job/type2/1?_source=title,salary

返回:

{
  "_index": "job",
  "_type": "type2",
  "_id": "1",
  "_version": 3,
  "found": true,
  "_source": {
    "title": "Java",
    "salary": 2000
  }
}

修改文档

一种是通过PUT的全覆盖方式,旧数据将被删除,以新的代替。

PUT job/type2/1
{
  "title":"Java",
  "salary":1400,
  "desc":"易维护的代码",
  "date":"2017-08-08"
}

另一种是通过POST方式,只对部分字段进行修改。

POST job/type2/1/_update
{
  "doc":{
    "salary":2000
  }
}

删除文档

通过DELETE方式可删除文档:

DELETE job/type2/1

mget取回多个文档

可参考:https://www.elastic.co/guide/cn/elasticsearch/guide/current/_Retrieving_Multiple_Documents.html
通过将查询合并,可以减少连接次数,提高效率。

GET _mget
{
   "docs" : [
      {
         "_index" : "job",
         "_type" :  "type2",
         "_id" :    1
      },
      {
         "_index" : "job",
         "_type" :  "type2",
         "_id" :    2,
         "_source": "salary"
      }
   ]
}

返回两个文档:

{
  "docs": [
    {
      "_index": "job",
      "_type": "type2",
      "_id": "1",
      "_version": 3,
      "found": true,
      "_source": {
        "title": "Java",
        "salary": 2000,
        "desc": "易维护的代码",
        "date": "2017-08-08"
      }
    },
    {
      "_index": "job",
      "_type": "type2",
      "_id": "2",
      "found": false
    }
  ]
}

还可进行简写,比如,index和type都相同,查找两个id,可以写作:

GET job/type2/_mget
{
  "ids":["1", "2"]
    
  }
}

bulk批量操作

bulk API 允许在单个步骤中进行多次 create 、 index 、 update 或 delete 请求。

详细参考:https://www.elastic.co/guide/cn/elasticsearch/guide/current/bulk.html

bulk批量操作的请求比较特殊,格式为:

{ action: { metadata }}
{ request body }
{ action: { metadata }}
{ request body } ...

一般两行为一条请求,第一行说明操作和元数据,第二行是操作数据。不过delete请求只有一行。

POST _bulk
{ "delete": { "_index": "website", "_type": "blog", "_id": "123" }} 
{ "create": { "_index": "website", "_type": "blog", "_id": "123" }}
{ "title":    "My first blog post" }
{ "index":  { "_index": "website", "_type": "blog" }}
{ "title":    "My second blog post" }
{ "update": { "_index": "website", "_type": "blog", "_id": "123", "_retry_on_conflict" : 3} }
{ "doc" : {"title" : "My updated blog post"} }

返回结果会列出每个请求的处理状态。

{
   "took": 4,
   "errors": false, 
   "items": [
      {  "delete": {
            "_index":   "website",
            "_type":    "blog",
            "_id":      "123",
            "_version": 2,
            "status":   200,
            "found":    true
      }},
      {  "create": {
            "_index":   "website",
            "_type":    "blog",
            "_id":      "123",
            "_version": 3,
            "status":   201
      }},
      {  "create": {
            "_index":   "website",
            "_type":    "blog",
            "_id":      "EiwfApScQiiy7TIKFxRCTw",
            "_version": 1,
            "status":   201
      }},
      {  "update": {
            "_index":   "website",
            "_type":    "blog",
            "_id":      "123",
            "_version": 4,
            "status":   200
      }}
   ]
}


通过以上操作,可以将数据以一定的组织方式,写入到es中。下一篇将总结如何进行搜索和查找。

原文地址:https://www.cnblogs.com/aaanthony/p/7380662.html