zoukankan      html  css  js  c++  java
  • 02.Elasticsearch入门

        Elasticsearch支持Http类型的Restful风格API请求,需要打开9200端口。Elasticsearch服务会监听两个端口9200和9300,9200提供Http Restful访问,9300端口用于集群内节点内部通信。
        关于Elasticsearch Http Restful API可参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html 

    1.Elasticsearch基本概念

    1.索引(index)文档类型(document type)、映射(mapping)文档(document)
        索引(index):Elasticsearch中创建索引的概念与数据库中的创建数据库类似,索引就是文档类型的集合。
        文档类型(document type):文档类型的概念与数据库中的表类似,文档类型用来区分不同文档。
        映射(mapping):映射的概念与数据库中的表结构类似,每一个文档类型都有一个映射,映射用来定义文档类型的结构。
        文档(document):文档的概念与数据库中的表中的一条记录类似,一个文档就是索引中的一条数据,一个文档也可以包含多个字段。
    总结:一个Elasticsearch服务中可以有多个索引,一个索引中可以有多个文档类型,一个文档类型可以有一个映射,一个文档类型中可以有多个文档。
    2.集群(cluster)、节点(node)、分片(shard)、副本(relica)
        集群(cluster): Elasticsearch支持集群以提供高可用的服务,一个集群由多个Elasticsearch服务节点组成。
        节点(node):一个Elasticsearch服务就是一个节点。
        分片(shard):在Elasticsearch中的索引可以分片,每一个分片是一个完整的Lucene索引。给索引分片的目的就是为了提高索引的可用性。
        副本(relica):副本其本质就是一个分片,副本是逻辑上的概念,它是分片的一个备份。当其分片由于某种原因不可用用时,副本就会顶替原来的分片。
    补充:Elasticsearch的索引通常会有多个分片,默认是一个索引有5个分片,每个分片有1个副本。有时候也把副本称为分片,为了区分分片与副本就有了主分片之说。

    2.使用Restful API简单操作ElasticSearch

    1.关于curl命令
        curl是利用URL语法在命令行方式下工作的开源文件传输工具。它被广泛应用在Unix、多种Linux发行版中,并且有DOS和Win32、Win64下的移植版本。
        在学习Elasticsearch时为了方便可以使用该命令。
        在Linux系统中经常自带了curl命令,而Windows系统中默认没有,需要下载。下载地址:http://curl.haxx.se/download.html
    使用示例:
    1. [lizhiwei@localhost ~]$ curl -XGET http://192.168.110.100:9200
    2. {
    3. "status" : 200,
    4. "name" : "node000",
    5. "cluster_name" : "elasticsearchTest",
    6. "version" : {
    7. "number" : "1.7.2",
    8. "build_hash" : "e43676b1385b8125d647f593f7202acbd816e8ec",
    9. "build_timestamp" : "2015-09-14T09:49:53Z",
    10. "build_snapshot" : false,
    11. "lucene_version" : "4.10.4"
    12. },
    13. "tagline" : "You Know, for Search"
    14. }
    2.索引创建与删除
        默认配置下,在创建一个文档时若这个文档所在的索引不存在就会创建这个索引。如果你不自动创建索引可以修改配置文件“elasticsearch.yml”的action.auto_create_index属性值。
    创建索引设置分片数、副本数,格式 curl -XPUT http://IP:9200/<index> -d <Json数据> ,如下
    1. [lizhiwei@localhost ElasticSearch]$ curl -XPUT http://192.168.110.100:9200/test -d @data.json
    2. {"acknowledged":true}
    3. ---------------------------------------------data.json内容
    4. {
    5. "settings" : {
    6. "index" : {
    7. "number_of_shards" : "4",
    8. "number_of_replicas" : "2"
    9. }
    10. }
    11. }
        number_of_shards:设置分片数
        number_of_replicas:设置副本数
    创建索引设置映射,格式 curl -XPUT http://IP:9200/<index> -d <Json数据> ,如下:
    1. [lizhiwei@localhost ElasticSearch]$ curl -XPUT http://192.168.110.100:9200/test -d @data.json
    2. {"acknowledged":true}
    3. ---------------------------------------------data.json内容
    4. {
    5. "settings" : {
    6. "index" : {
    7. "number_of_shards" : "4",
    8. "number_of_replicas" : "1"
    9. }
    10. },
    11. "mappings" : {
    12. "DocType001" : {
    13. "_source" : {
    14. "enabled" : false
    15. },
    16. "properties" : {
    17. "field1" : {
    18. "type" : "string",
    19. "index" : "not_analyzed"
    20. },
    21. "field2" : {
    22. "type" : "string",
    23. "store" : "yes"
    24. }
    25. }
    26. }
    27. }
    28. }
        此次创建索引时除了设置分片和副本还设置了映射,这个映射设置了一个文档类型DocType001
    删除索引,格式 curl -XDELETE http://IP:9200/<index> ,如下
    1. [lizhiwei@localhost ElasticSearch]$ curl -XDELETE http://192.168.110.100:9200/test
    2. {"acknowledged":true}
    3.文档的增删查改
    CURD的URL格式:http://IP:9200/<index>/<type>/[<id>]
    id是可选的,不提供的话Elasticsearch会自动生成。index和type将信息进行分层,便于管理。可以将index理解为数据库,type理解为数据表。
    (1).创建
    # 使用自动生成ID的方式新建纪录
    curl -XPOST http://IP:9200/<index>/<type> -d '{ "tag" : "bad" }'
    1. [lizhiwei@localhost ElasticSearch]$ curl -XPOST http://192.168.110.100:9200/test/People?pretty -d '{ "tag" : "bad" }'
    2. {
    3. "_index" : "test",
    4. "_type" : "People",
    5. "_id" : "AVBRAKASiFg2t1Ow-SEW",
    6. "_version" : 1,
    7. "created" : true
    8. }
    # 使用指定的ID新建记录
    curl -XPOST http://IP:9200/<index>/<type>/3 -d '{ "tag" : "bad" }'
    1. [lizhiwei@localhost ElasticSearch]$ curl -XPOST http://192.168.110.100:9200/test/People/3?pretty -d '{ "tag" : "bad" }'
    2. {
    3. "_index" : "test",
    4. "_type" : "People",
    5. "_id" : "3",
    6. "_version" : 1,
    7. "created" : true
    8. }
    (2).查询
    1. # 查询所有的index和type的记录
    2. curl -XGET http://IP:9200/_search?pretty
    3. # 查询某个index下所有type的记录
    4. curl -XGET http://IP:9200/<index>/_search?pretty
    5. # 查询某个index下某个type下所有的记录
    6. curl -XGET http://IP:9200/<index>/<type>/_search?pretty
    7. # 使用参数查询所有的记录
    8. curl -XGET http://IP:9200/_search?q=tag:bad&pretty;
    9. # 使用参数查询某个index下的所有记录
    10. curl -XGET http://IP:9200/<index>/_search?q=tag:bad&pretty;
    11. # 使用参数查询某个index下某个type下所有的记录
    12. curl -XGET http://IP:9200/<index>/<type>/_search?q=tag:bad&pretty;
    13. # 使用JSON参数查询所有的记录,-d代表一个JSON格式的对象
    14. curl -XGET http://IP:9200/_search?pretty -d '{ "query" : { "term" : { "tag" : "bad" } } }'
    15. # 使用JSON参数查询某个index下的所有记录
    16. curl -XGET http://IP:9200/<index>/_search?pretty -d '{ "query" : { "term" : { "tag" : "bad" } } }'
    17. # 使用JSON参数查询某个index下某个type下所有的记录
    18. curl -XGET http://IP:9200/<index>/<type>/_search?pretty -d '{ "query" : { "term" : { "tag" : "bad" } } }'
    例子:
    1. [lizhiwei@localhost ElasticSearch]$ curl -XGET http://192.168.110.100:9200/test/People/_search?pretty
    2. {
    3. "took" : 4,
    4. "timed_out" : false,
    5. "_shards" : {
    6. "total" : 5,
    7. "successful" : 5,
    8. "failed" : 0
    9. },
    10. "hits" : {
    11. "total" : 2,
    12. "max_score" : 1.0,
    13. "hits" : [ {
    14. "_index" : "test",
    15. "_type" : "People",
    16. "_id" : "AVBRBKcOiFg2t1Ow-SEy",
    17. "_score" : 1.0,
    18. "_source":{ "tag" : "bad" }
    19. }, {
    20. "_index" : "test",
    21. "_type" : "People",
    22. "_id" : "3",
    23. "_score" : 1.0,
    24. "_source":{ "tag" : "bad" }
    25. } ]
    26. }
    27. }
    (3).更新
    更新操作格式:curl -XPUT http://IP:9200/<index>/<type>/<id> -d '{ "tag" : "good" }'
    1. [lizhiwei@localhost ElasticSearch]$ curl -XPUT http://192.168.110.100:9200/test/People/3?pretty -d '{ "tag" : "good" }'
    2. {
    3. "_index" : "test",
    4. "_type" : "People",
    5. "_id" : "3",
    6. "_version" : 3,
    7. "created" : false
    8. }
    (4).删除
    删除操作格式:curl -XDELETE http://IP:9200/<index>/[<type>]/[<id>],<type>、<id>可选,如果不存在type删除的是整个索引,如果id不存在删除的是整个文档类型。
    1. [lizhiwei@localhost ElasticSearch]$ curl -XDELETE http://192.168.110.100:9200/test/People/3?pretty
    2. {
    3. "found" : true,
    4. "_index" : "test",
    5. "_type" : "People",
    6. "_id" : "3",
    7. "_version" : 4
    8. }
    4.其他操作介绍
    使用Restful API也可以监控ElasticSearch服务,例如查看集群健康:
    1. [lizhiwei@localhost ElasticSearch]$ curl -XGET http://192.168.110.100:9200/_cluster/health?pretty
    2. {
    3. "cluster_name" : "elasticsearchTest",
    4. "status" : "green",
    5. "timed_out" : false,
    6. "number_of_nodes" : 4,
    7. "number_of_data_nodes" : 4,
    8. "active_primary_shards" : 7,
    9. "active_shards" : 14,
    10. "relocating_shards" : 0,
    11. "initializing_shards" : 0,
    12. "unassigned_shards" : 0,
    13. "delayed_unassigned_shards" : 0,
    14. "number_of_pending_tasks" : 0,
    15. "number_of_in_flight_fetch" : 0
    16. }

    3.常用的Restful API

    1. # 检查集群健康:
    2. curl -XGET http://127.0.0.1:9200/_cluster/health?pretty
    3. # 关闭整个集群:
    4. curl -XPOST http://127.0.0.1:9200/_cluster/nodes/_shutdown
    5. # 关闭单台节点:
    6. curl -XPOST http://127.0.0.1:9200/_cluster/nodes/{node.name}/_shutdown
    7. # 查看集群节点:
    8. curl -XGET http://127.0.0.1:9200/_cluster/nodes?pretty
    9. # 查看集群状态:
    10. curl -XGET http://127.0.0.1:9200/_cluster/state?pretty
    11. # 查看节点状态:
    12. curl -XGET http://127.0.0.1:9200/_nodes/stats?pretty
    13. # 查看本机节点:
    14. curl -XGET http://127.0.0.1:9200/_nodes/_local?pretty
    15. # 查看集群节点信息:
    16. curl -XGET http://127.0.0.1:9200/_cluster/state/nodes
    17. # 查看索引映射:
    18. curl -XGET http://127.0.0.1:9200/.marvel-kibana/_mapping?pretty
    19. 以上所有查询都可以针对json节点的子节点进行查询,关键词如:settings, os, process, jvm, thread_pool, network, transport, http , plugins
    20. 例如:
    21. curl -XGET 'http://localhost:9200/_nodes?pretty'
    22. curl -XGET 'http://localhost:9200/_nodes/process?pretty'
    23. curl -XGET 'http://localhost:9200/_nodes/os?pretty'
    24. curl -XGET 'http://localhost:9200/_nodes/settings?pretty'
    -------------------------------------------------------------------------------------------------------------------------------



  • 相关阅读:
    约瑟夫
    用过的ps操作
    guns框架试用笔记
    让使用WebForm的.aspx文件写的WebApi能够跨域访问
    DevExpress的GridView的行变和列变
    SSMS18.0缺少调试功能
    EF_CodeFirst框架版本问题
    微信小程序框架了解2---js的写法
    微信小程序框架了解1---总体了解
    Chrome浏览器写代码片段的地方
  • 原文地址:https://www.cnblogs.com/LiZhiW/p/4867727.html
Copyright © 2011-2022 走看看