ElasticSearch 7.6.0+

官网地址
https://www.elastic.co/

ES下载地址
https://www.elastic.co/downloads/elasticsearch

ElasticSearch 启动服务器

进入

/Users/linker/elastic search/elasticsearch-7.6.0/bin

启动

bin % ./elasticsearch

访问验证是否启动

http://127.0.0.1:9200/

结果

{
name: "LinkerMacBook-Pro.local",
cluster_name: "elasticsearch",
cluster_uuid: "Ey7K0tOmTieZPtdXgpSgUQ",
version: {
number: "7.6.0",
build_flavor: "default",
build_type: "tar",
build_hash: "7f634e9f44834fbc12724506cc1da681b0c3b1e3",
build_date: "2020-02-06T00:09:00.449973Z",
build_snapshot: false,
lucene_version: "8.4.0",
minimum_wire_compatibility_version: "6.8.0",
minimum_index_compatibility_version: "6.0.0-beta1"
},
tagline: "You Know, for Search"
}

Kibana 7.6.0

IK Analysis for Elasticsearch GitHub: https://github.com/medcl/elasticsearch-analysis-ik

进入bin目录

/Users/linker/kibana/kibana-7.6.0-darwin-x86_64/bin

启动

bin % ./kibana

访问验证是否启动

http://localhost:5601/app/kibana

进入 Dev Tools

GET /_cat/health?v

用来查看服务器状态(健康度),green表示一切ok

增加索引

PUT /linker?pretty

返回

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "linker"
}

查询

GET /_cat/indices?v

返回

health status index                    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   linker                   jqpbQGmYRdSYepgEFYPsqw   1   1          0            0       230b           230b

删除

DELETE /linker?pretty

返回

{
  "acknowledged" : true
}

再运行

GET /_cat/indices?v

安装中文分词器

进入

/Users/linker/elastic search/elasticsearch-7.6.0/bin

安装分词器

bin % ./elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.6.0/elasticsearch-analysis-ik-7.6.0.zip

重启ElasticSearch

退出服务并启动

测试中文分词效果
进入

http://localhost:5601/app/kibana#/dev_tools/console?_g=()

输入

GET _analyze
{
  "analyzer":"ik_max_word",
  "text":"你好Linker"
}

返回

{
  "tokens" : [
    {
      "token" : "你好",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "linker",
      "start_offset" : 2,
      "end_offset" : 8,
      "type" : "ENGLISH",
      "position" : 1
    }
  ]
}

增加文档

PUT /linker/_doc/1?pretty
{
  "name":"tank"
}

返回

{
  "_index" : "linker",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}

获取文档

GET /linker/_doc/1?pretty

返回

{
  "_index" : "linker",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 2,
  "_seq_no" : 1,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "tank"
  }
}

修改文档

PUT /linker/_doc/1?pretty
{
  "name":"tanker"
}

返回

{
  "_index" : "linker",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 4,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 3,
  "_primary_term" : 1
}

再次查看

GET /linker/_doc/1?pretty

返回

{
  "_index" : "linker",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 4,
  "_seq_no" : 3,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "tanker"
  }
}

删除文档

DELETE /linker/_doc/1?pretty

返回

{
  "_index" : "linker",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 5,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 4,
  "_primary_term" : 1
}

Kibana-批量导入

输入

POST _bulk
{"index":{"_index":"linker","_id":10001}}
{"code":"540785126782","price":398,"name":"房屋卫士自流平美缝剂瓷砖地砖专用双组份真瓷胶防水填缝剂镏金色","place":"上海","category":"品质建材"}
{"index":{"_index":"linker","_id":10002}}
{"code":"24727352473","price":21.799999237060547,"name":"艾瑞泽手工大号小号调温热熔胶枪玻璃胶枪硅胶条热溶胶棒20W-100W","place":"山东青岛","category":"品质建材"}
{"index":{"_index":"linker","_id":10003}}

返回

{
  "took" : 26,
  "errors" : false,
  "items" : [
    {
      "index" : {
        "_index" : "linker",
        "_type" : "_doc",
        "_id" : "10001",
        "_version" : 5,
        "result" : "updated",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 10,
        "_primary_term" : 1,
        "status" : 200
      }
    },
    {
      "index" : {
        "_index" : "linker",
        "_type" : "_doc",
        "_id" : "10002",
        "_version" : 2,
        "result" : "updated",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 11,
        "_primary_term" : 1,
        "status" : 200
      }
    }
  ]
}

验证插入的数据

GET /linker/_search
{
  "query": {"match_all":{}}
}

返回

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "linker",
        "_type" : "_doc",
        "_id" : "10001",
        "_score" : 1.0,
        "_source" : {
          "code" : "540785126782",
          "price" : 398,
          "name" : "房屋卫士自流平美缝剂瓷砖地砖专用双组份真瓷胶防水填缝剂镏金色",
          "place" : "上海",
          "category" : "品质建材"
        }
      },
      {
        "_index" : "linker",
        "_type" : "_doc",
        "_id" : "10002",
        "_score" : 1.0,
        "_source" : {
          "code" : "24727352473",
          "price" : 21.799999237060547,
          "name" : "艾瑞泽手工大号小号调温热熔胶枪玻璃胶枪硅胶条热溶胶棒20W-100W",
          "place" : "山东青岛",
          "category" : "品质建材"
        }
      }
    ]
  }
}

Kibana-查询操作

查询所有

GET /linker/_search
{
  "query": {"match_all":{}}
}

price 倒排序 (不赞成使用_id,需要额外定义ID)

GET /linker/_search
{
  "query": { "match_all": {} },
  "sort": [
    { "price": "desc" }
  ]
}

只返回部分字段

GET /linker/_search
{
  "query": {"match_all":{}},
  "_source": ["name","price"]
}

条件查询

GET /linker/_search
{
  "query": { "match": { "place": "山东" } }
}

分页查询

GET /linker/_search
{
  "query": { "match_all": {} },
  "from": 1,
  "size": 3,
  "sort": { "price": { "order": "desc" } }
}

Kibana-聚合操作

GET /linker/_search
{
  "size": 0,
  "aggs": {
    "group_by_place": {
      "terms": {
        "field": "place.keyword",
        "size": 3
      }
    }
  }
}

等价于

select count(*),place from product group by place limit 0,3  

返回

{
  "took" : 48,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "group_by_place" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "上海",
          "doc_count" : 1
        },
        {
          "key" : "山东青岛",
          "doc_count" : 1
        }
      ]
    }
  }
}

参考