在上手使用前,须要先了解一些基本的概念。html
推荐
能够到 https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html 阅读《Elastic Search 权威指南》,有很是详细和全面的说明。mysql
至关于mysql中的数据库web
至关于mysql中的一张表sql
至关于mysql中的一行(一条记录)数据库
至关于mysql中的一列(一个字段)服务器
一个服务器,由一个名字来标识app
一个或多个节点组织在一块儿运维
将一份数据划分为多小份的能力,容许水平分割和扩展容量。多个分片能够响应请求,提升性能和吞吐量。elasticsearch
复制数据,一个节点出问题时,其他节点能够顶上。ide
可参考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
可参考: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 API 容许在单个步骤中进行屡次 create 、 index 、 update 或 delete 请求。
详细参考:https://www.elastic.co/guide/cn/elasticsearch/guide/current/bulk.html
bulk批量操做的请求比较特殊,格式为:
{ action: { metadata }}\n
{ request body }\n
{ action: { metadata }}\n
{ request body }\n ...
通常两行为一条请求,第一行说明操做和元数据,第二行是操做数据。不过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中。下一篇将总结如何进行搜索和查找。