以前已经说过最近正在作数据建设,爬取数据以后通过处理,最终导入到ElasticSearch中,并编写公共接口以提供给后台进行检索操做;原本想等把ElasticSearch官方API都看过一遍,造成思惟导图以后再整理出来,由于熟悉一个工具,它能作到的,比你知道它能作到的要全面也重要的多,可是整理了两章以后发现,内容真的太多了,这还仅仅只2类。。因此想仍是先把基础用法记录下来,先一步步来了。html
Elasticsearch 是一个分布式可扩展的近实时搜索和分析引擎,一个创建在全文搜索引擎 Apache Lucene(TM) 基础上的搜索引擎.固然 Elasticsearch 并不只仅是 Lucene 那么简单,它不只包括了全文搜索功能,还能够进行如下工做:node
- 分布式实时文件存储,并将每个字段都编入索引,使其能够被搜索。
- 实时分析的分布式搜索引擎。
- 能够扩展到上百台服务器,处理PB级别的结构化或非结构化数据。
安装比较简单,建议练手阶段安装Kibana,安装步骤参见以前写的博客:ElasticSearch安装数据库
关系数据库 ⇒ 数据库 ⇒ 表 ⇒ 行 ⇒ 列(Columns) Elasticsearch ⇒ 索引(Index) ⇒ 文档(Docments) ⇒ 字段(Fields)服务器
Elasticsearch ⇒ 索引(Index) ⇒ 文档(Docments) ⇒ 字段(Fields)app
既然删除了type,感受将Index理解为table是否是更加合理些。。elasticsearch
建立Index语法以下:分布式
PUT /userinfo?pretty { "mappings": { "_doc": { "properties": { "name": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word", "fields": {"raw":{"type":"keyword"}}}, "content": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" }, "org_type": { "type": "keyword"}, "create_time":{"type":"date", "format": "epoch_second"} } } } }
若是理解了上面的基本概念的话,这命令看起来应该不难理解,有几点须要提一下:ide
PUT /userinfo/_doc/1? { "name":"lctest", "age":29 }经过GET /userinfo/_mapping命令查看userinfo最新的字段,能够看到ES默认新增了一个类型为long的age字 段。可是通常建议关键字段在建立Index的时候进行指定字段;
{ "userinfo": { "mappings": { "_doc": { "properties": { "age": { "type": "long" }, "content": { "type": "text", "analyzer": "ik_max_word" }, "create_time": { "type": "date", "format": "epoch_second" }, "name": { "type": "text", "fields": { "raw": { "type": "keyword" } }, "analyzer": "ik_max_word" }, "org_type": { "type": "keyword" } } } } } }
PUT /userinfo/_mapping/_doc { "properties": { "params": { "type": "nested", "properties": { "update_time":{"type":"date", "format": "epoch_second"} } } } }
//删除指定索引 DELETE /userinfo //删除多个索引 DELETE /index1,index2 或者 DELETE /index* //删除全部索引 DELETE /_all 或者 DELETE /*
最基本的索引操做就到此结束,基本上能知足简单的基本需求,下面有一些扩展知识点,能够选择性的使用工具
索引别名就像一个快捷方式或软链接,或者是一个指向,都是最终指的同一个东西,别名 带给咱们极大的灵活性,容许咱们作下面这些:ui
POST /_aliases { "actions" : [ { "add" : { "index" : "test1", "alias" : "alias1" } } ] }
POST /_aliases { "actions" : [ { "remove" : { "index" : "test1", "alias" : "alias1" } } ] }
POST /_aliases { "actions" : [ { "remove" : { "index" : "test1", "alias" : "alias1" } }, { "add" : { "index" : "test2", "alias" : "alias1" } } ] }
PUT /{index}/_alias/{name} PUT /logs_201305/_alias/2013
以上即是Index别名的基本经常使用语法,完整API详见Aliases API
查看全部索引信息 GET /_cat/indices?v&pretty
查看某个索引信息 GET /{index}
删除索引单个索引 DELETE /{index}
删除全部索引 DELETE /_all 或者 DELETE /*
删除多个索引: DELETE /index1,index2 或者 DELETE /index*
查看索引的映射 GET /{index}/_mapping
查看某个索引的某个类型的映射 GET /{index}/_mapping/{type}
映射添加新字段 PUT /{index}/_mapping/{type}
为了防止误操做 ,形成删库跑路的状况,建议在elasticsearch.yml 作以下配置:action.destructive_requires_name: true 这个设置使删除只限于特定名称指向的数据, 而不容许经过指定 _all 或通配符来删除指定索引库。你一样能够经过 Cluster State API 动态的更新这个设置。
新手推荐使用Kibana工具,带命令提示,很适合不熟悉命令的初学者,我也一直在用,只是博客的话,命令的表现形式感受更好一些