zoukankan      html  css  js  c++  java
  • ElasticSearch入门简介

    ElasticSearch是基于Apache Lucene的分布式搜索引擎, 提供面向文档的搜索服务。本文以6.2.3版本为例介绍ElasticSearch的应用。

    本文首先介绍ElasticSearch中的索引和文档的概念,并在系列的其它文章进行更进一步的介绍。

    目录:

    可以在官网下载压缩包, 在解压目录中执行bin/elasticsearch来启动服务, 或者使用包管理器来安装启动.

    ES默认端口为9200, 本地启动ES后向http://localhost:9200发送GET请求可以查看ES的基本信息:

    GET 'localhost:9200'
    {
      "name" : "hiTUe19",
      "cluster_name" : "elasticsearch_finley",
      "cluster_uuid" : "cfKnyFL1Rx6URmrmAuMBFw",
      "version" : {
        "number" : "5.1.2",
        "build_hash" : "c8c4c16",
        "build_date" : "2017-01-11T20:18:39.146Z",
        "build_snapshot" : false,
        "lucene_version" : "6.3.0"
      },
      "tagline" : "You Know, for Search"
    }
    

    ElasticSearch采用三层数据结构来管理数据:

    • 索引(index): 索引是最高层的数据结构,可以定义独立的搜索索引和分片存储策略
    • 类型(type): 每个index可以拥有多个type, 用于存储不同类型的文档
    • 文档:文档是最基本的数据结构,存储和搜索都是围绕文档展开的

    ElasticSearch中的文档是一个Json对象,搜索的结果是文档的集合因此被称为面向文档的搜索。

    与三层数据结构相对应,我们可以使用三个字段来唯一标识文档:

    • _index: 代表文档所在的索引。索引名必须小写, 不能以下划线开头, 不能包含逗号.
    • _type: 代表文档所在的类型集。type名可以为大小写, 不能以下划线开头, 不能包含逗号.
    • _id: 用于唯一标识某个type中的文档

    空查询请求可以列出某个数据结构中所有文档:

    • 列出所有文档: GET /_search
    • 列出索引blog下的所有文档: GET /blog/_search
    • 列出类型/blog/user下的所有文档: GET /blog/user/_search

    创建文档

    IndexAPI可以用于创建文档:

    $ POST 'localhost:9200/blog/user/'
    content-type: application/json
    body:
    {
      "id": 1,
      "nickname": "finley"
    }
    response:
    {
        "_index": "blog",
        "_type": "user",
        "_id": "AV5WoO0MdsHuOObNBTWU",
        "_version": 1,
        "result": "created",
        "_shards": {
            "total": 2,
            "successful": 1,
            "failed": 0
        },
        "created": true
    }
    

    使用POST请求创建文档, 在url中指定_index_type, 在请求体中使用json格式提交文档数据。

    _index_type不存在ES会自动创建。上述请求中文档的_id字段由ElasticSearch创建,我们也可以自己指定_id:

    POST localhost:9200/blog/user/2/
    content-type: application/json
    {
      "id": 2,
      "nickname": "easy"
    }
    
    response:
    {
        "_index": "blog",
        "_type": "user",
        "_id": "2",
        "_version": 1,
        "result": "created",
        "_shards": {
            "total": 2,
            "successful": 1,
            "failed": 0
        },
        "created": true
    }
    

    访问文档

    使用GET请求访问文档,需要提供_index, _type_id三个参数唯一标识文档。

    GET localhost:9200/blog/user/2/
    response:
    {
        "_index": "blog",
        "_type": "user",
        "_id": "2",
        "_version": 2,
        "found": true,
        "_source": {
            "id": 2,
            "nickname": "easy"
        }
    }
    

    更新文档

    因为修改文档后难以更新索引,因此ElasticSearch修改文档的操作是通过删除原文档后重新添加新文档来进行的。

    使用IndexAPI对已存在的文档发送POST请求则会更新文档:

    POST localhost:9200/blog/user/2/
    content-type: application/json
    {
      "nickname": "easy",
      "gender": "male"
    }
    
    response:
    {
        "_index": "blog",
        "_type": "user",
        "_id": "2",
        "_version": 2,
        "result": "updated",
        "_shards": {
            "total": 2,
            "successful": 1,
            "failed": 0
        },
        "created": false
    }
    

    注意_version, created, result字段显示文档已被更新。通过GET请求查看更新后的文档:

    GET localhost:9200/blog/user/2/
    {
      "_index": "blog",
      "_type": "user",
      "_id": "2",
      "_version": 2,
      "found": true,
      "_source": {
        "nickname": "easy2",
        "gender": ”male“
      }
    }
    

    注意到原文档中的_id字段已经不见了,文档完全由我们发送的上一个POST请求定义。

    修改文档也可以通过PUT方法:

    PUT localhost:9200/blog/user/2/
    content-type: application/json
    {
      "id": 2,
      "nickname": "easy3"
    }
    
    {
        "_index": "blog",
        "_type": "user",
        "_id": "2",
        "_version": 3,
        "result": "updated",
        "_shards": {
            "total": 2,
            "successful": 1,
            "failed": 0
        },
        "created": false
    }
    

    再次通过GET请求确认文档已被修改:

    GET localhost:9200/blog/user/2/
    {
      "_index": "blog",
      "_type": "user",
      "_id": "2",
      "_version": 3,
      "found": true,
      "_source": {
        "id": 2
        "nickname": "easy3",
      }
    }
    

    删除文档

    删除文档需要发送DELETE请求:

    DELETE localhost:9200/blog/user/2/
    response:
    {
        "found": true,
        "_index": "blog",
        "_type": "user",
        "_id": "2",
        "_version": 4,
        "result": "deleted",
        "_shards": {
            "total": 2,
            "successful": 1,
            "failed": 0
        }
    }
    
  • 相关阅读:
    使用海康IP摄像头接入RTSP/RTMP视频平台如何修改默认H.265编码格式?
    视频会议系统EasyRTC常见的几种架构方式及应用场景:MCU/SFU、视频会议、应急指挥、即时通信
    网页全终端视频直播/点播H5播放器EasyPlayer.js正式发布,支持H.265网页播放
    海康&青犀合作RTMP推流摄像头接入RTSP/RTMP网页无插件直播平台EasyDSS忘记密码如何处理?
    视频远程通话会议EasyRTC通过SSH部署,关闭SSH后进程停止运行如何解决?
    【智慧医疗】如何通过视频流媒体平台EasyNVR+EasyNTD搭建医疗行业视频监控平台?
    【解决方案】视频“云-边-端”协同在智慧园区项目中的场景应用
    摄像头智能云组网EasyNTS网络穿透设备如何进行设备配置?
    Web网页无插件播放RTSP、RTMP、HLS、HTTP视频流的可行方案
    RTSP协议视频平台EasyNVR接入H.265视频直播流能显示快照吗?
  • 原文地址:https://www.cnblogs.com/Finley/p/9499532.html
Copyright © 2011-2022 走看看