ElasticSearch入門簡介

ElasticSearch是基於Apache Lucene的分佈式搜索引擎, 提供面向文檔的搜索服務。本文以6.2.3版本爲例介紹ElasticSearch的應用。json

本文首先介紹ElasticSearch中的索引和文檔的概念,並在系列的其它文章進行更進一步的介紹。bash

目錄:數據結構

能夠在官網下載壓縮包, 在解壓目錄中執行bin/elasticsearch來啓動服務, 或者使用包管理器來安裝啓動.app

ES默認端口爲9200, 本地啓動ES後向http://localhost:9200發送GET請求能夠查看ES的基本信息:elasticsearch

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對象,搜索的結果是文檔的集合所以被稱爲面向文檔的搜索。ui

與三層數據結構相對應,咱們可使用三個字段來惟一標識文檔:搜索引擎

  • _index: 表明文檔所在的索引。索引名必須小寫, 不能如下劃線開頭, 不能包含逗號.
  • _type: 表明文檔所在的類型集。type名能夠爲大小寫, 不能如下劃線開頭, 不能包含逗號.
  • _id: 用於惟一標識某個type中的文檔

空查詢請求能夠列出某個數據結構中全部文檔:url

  • 列出全部文檔: GET /_search
  • 列出索引blog下的全部文檔: GET /blog/_search
  • 列出類型/blog/user下的全部文檔: GET /blog/user/_search

建立文檔

IndexAPI能夠用於建立文檔:code

$ 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
    }
}
相關文章
相關標籤/搜索