Elasticsearch 入门: Hello World

  1. 安装 Elasticserach:

    1. 下载最新的elasticsearch:官网地址: https://www.elastic.co/downloads/elasticsearch

    2. 解压缩之后,把 elasticsearch-<version>/bin 路径放到 bash_profile 里

    3. 运行

      elasticsearch
      
    4. 用命令行测试

      curl 'http://localhost:9200/'
      

      应给得到类似下面的响应:

      {
        "name" : "VJ6rpak",
        "cluster_name" : "elasticsearch",
        "cluster_uuid" : "enuYtqaGTaqoSYwErlyZBw",
        "version" : {
          "number" : "6.2.4",
          "build_hash" : "ccec39f",
          "build_date" : "2018-04-12T20:37:28.497551Z",
          "build_snapshot" : false,
          "lucene_version" : "7.2.1",
          "minimum_wire_compatibility_version" : "5.6.0",
          "minimum_index_compatibility_version" : "5.0.0"
        },
        "tagline" : "You Know, for Search"
      }
      
  2. 安装图形界面kibana

    1. 下载kibana:官网地址: https://www.elastic.co/downloads/kibana

    2. 解压缩之后,把 kibana-<version>/bin 路径放到 bash_profile 里

    3. 中文版教程中还有安装 sense 的步骤,但 Kibana 从 5.0.1 版本开始就已经自带了调试工具,可以在 kibana 左侧工具栏 DevTools 中找到。

    4. 启动kibana

      kibana
      
    5. 用浏览器访问图形界面:http://localhost:5601

  3. Hello World

    1. 在 DevTools 查询集群中文档总数量

      GET /_count
      {
          "query":{
              "match_all":{}
          }
      }
      
    2. 返回:

      {
        "count": 1,
        "_shards": {
          "total": 5,
          "successful": 5,
          "skipped": 0,
          "failed": 0
        }
      }
      
  4. Python 插件 elasticsearch

    1. 安装elasticsearch

      pip3 install elasticsearch
      
    2. hello world

      from elasticsearch import Elasticsearch
      import pprint
      es = Elasticsearch()
      doc = {
          "query":{
              "match_all":{}
          }
      }
      res = es.search(body=doc)
      pprint.pprint(res)
      
    3. 应该得到下面的结果:

      {'_shards': {'failed': 0, 'skipped': 0, 'successful': 5, 'total': 5},
       'hits': {'hits': [{'_id': '1',
                          '_index': 'test-index',
                          '_score': 1.0,
                          '_source': {'author': 'kimchy',
                                      'text': 'Elasticsearch: cool. bonsai cool.',
                                      'timestamp': '2018-05-10T10:24:58.396432'},
                          '_type': 'tweet'}],
                'max_score': 1.0,
                'total': 1},
       'timed_out': False,
       'took': 1}
      

我的部分技术博客将会同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=3hp099lflrokw

推荐阅读更多精彩内容