Elasticsearch入门

安装

一,elasticsearch安装

  • 下载elasticsearch包:curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.4.tar.gz
  • 解压:tar -xvf elasticsearch-6.5.4.tar.gz
  • 如果需要设置外网访问需要修改配置文件绑定外网ip(修改elasticsearch-6.5.4/config/elasticsearch.yml):

    network.host: 192.168.60.201
  • 进入elasticsearch包下运行启动脚本:sh bin/elasticsearch -d
  • 检查:浏览器请求http://xxx:9200/_cat/health?v&pretty

    epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
    1568168189 02:16:29  elasticsearch yellow          1         1     11  11    0    0       10             0                  -                 52.4%

二,kibana安装(注意需要和elasticsearch版本保持一致)

  • 下载kibana包:wget https://artifacts.elastic.co/downloads/kibana/kibana-6.5.4-linux-x86_64.tar.gz
  • 解压:tar -xvf kibana-6.5.4-linux-x86_64.tar.gz
  • 修改配置文件kibana.yml:

    # 绑定外网ip,提供外网访问
    server.host: "192.168.60.201"
    # 绑定elasticsearch地址
    elasticsearch.url: "http://192.168.60.201:9200"
  • 启动kibana:sh kibana
  • 检验kibana:访问:http://192.168.60.201:5601
  • 打开左侧Dev Tools,开始elasticsearch之旅

Elasticsearch入门

基本概念

Node 与 Cluster

Elastic 本质上是一个分布式数据库,允许多台服务器协同工作,每台服务器可以运行多个 Elastic 实例。单个 Elastic 实例称为一个节点(node)。一组节点构成一个集群(cluster)。

Index

Elastic 会索引所有字段,经过处理后写入一个反向索引(Inverted Index)。查找数据的时候,直接查找该索引。所以,Elastic 数据管理的顶层单位就叫做 Index(索引)。它是单个数据库的同义词。每个 Index (即数据库)的名字必须是小写。

//查询当前节点的所有Index
GET _cat/indices

Document

Index 里面单条的记录称为 Document(文档)。许多条 Document 构成了一个 Index。Document 使用 JSON 格式表示,下面是一个例子。

{
    "_index" : "bank",
    "_type" : "account",
    "_id" : "10",
    "_score" : null,
    "_source" : {
        "account_number" : 10,
        "balance" : 46170,
        "firstname" : "Dominique",
        "lastname" : "Park",
        "age" : 37,
        "gender" : "F",
        "address" : "100 Gatling Place",
        "employer" : "Conjurica",
        "email" : "dominiquepark@conjurica.com",
        "city" : "Omar",
        "state" : "NJ"
 }

Elasticsearch入门操作

1,Index使用

//列出所有index
GET _cat/indices
-----------------------
yellow open customer  8F4rLcOeQBG3URa0Qtslew 5 1    2 0   7.6kb   7.6kb
green  open .kibana_1 KBZkmY9gRAmy4V8WjvAsUQ 1 0    3 0  11.9kb  11.9kb
yellow open bank      0WgLknyiSjiVe5XFj9UrSg 5 1 1000 0 483.4kb 483.4kb
//创建索引
PUT /customer
-----------------------
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "customer"
}
//删除索引
DELETE /customer
-----------------------
{
  "acknowledged" : true
}

2,探索数据

数据准备 导入测试数据 这里 加载它到我们集群中,如下所示 :

curl -H "Content-Type: application/json" -XPOST '192.168.60.201:9201/bank/account/_bulk?pretty&refresh' --data-binary "@accounts.json"

Search API

根据account_number升序排序查询所有的数据

GET /bank/_search?q=*&sort=account_number:desc

DSL(domain-specific language 领域特定语言)

同样是执行上面的逻辑,使用DSL,类似http请求请求体,更加直观

GET /bank/_search
  {
    "query": {"match_all": {}},
    "sort": [
      {
        "account_number": {
          "order": "asc"
        }
      }
    ]
  }

更加复杂的查询

// 全文检索获取所有balance在20000到30000之间的用户
  GET /bank/_search
  {
    "query":{
      "bool":{
        "must": [
          {"match_all": {}}
        ],
        "filter": {
          "range": {
            "balance": {
              "gte": 20000,
              "lte": 30000
            }
          }
        }
      }
    }
  }

// 全文检索所有地址包含Crosby或Avenue的用户(全文匹配)
  GET /bank/_search
  {
    "query": {
      "match": {
        "address": "Crosby Avenue"
      }
    }
  }

// 短语匹配 match_phrase 短语匹配"Crosby Avenue"
  GET /bank/_search
  {
    "query": {
      "match_phrase": {
        "address": "Crosby Avenue"
      }
    }
  }

高亮搜索

//高亮匹配地址为"Crosby Avenue"
GET /bank/_search
{
  "query": {
    "match_phrase": {
      "address": "Crosby Avenue"
    }
  },
  "highlight":{
    "fields": {
      "address": {}
    }
  }
}
-----------结果带有<em>标签的样式---------
"highlight" : {
      "address" : [
        "364 <em>Crosby</em> <em>Avenue</em>"
      ]
}

聚合查询(aggregations)

// 根据城市聚合查询居住最多的城市
  GET /bank/_search
  {
    "size": 10,
    "aggs": {
      "group_by_city": {
        "terms": {
          "field": "city.keyword"
        }
      }
    }
  }

  //---------结果---------
  "aggregations" : {
      "group_by_city" : {
          "doc_count_error_upper_bound" : 5,
          "sum_other_doc_count" : 989,
          "buckets" : [
              {
                  "key" : "Belvoir",
                  "doc_count" : 2
              },
              {
                  "key" : "Aberdeen",
                  "doc_count" : 1
              },
              {
                  "key" : "Abiquiu",
                  "doc_count" : 1
              },
              {
                  "key" : "Abrams",
                  "doc_count" : 1
              }
          ]
      }
  }

总结

​ 本文是本人学习elasticsearch的学习总结,主要参考 Elasticsearch权威指南 和官网,只是一个关于Elasticsearch 基础描述的教程。

相关推荐