Elasticsearch入門

以前搭建logstash的時候使用過elasticsearch。 恰好最近在公司也用到了es,寫篇水文記錄一下也當作筆記吧。web

Elasticsearch是一個基於Lucene的搜索服務器。它提供了一個分佈式多用戶能力的全文搜索引擎,使用RESTful web暴露接口。數據庫

它有許多特性,好比如下幾個屬性:服務器

1.實時數據
2.實時分析
3.分佈式設計
4.高可用性
5.全文搜索
6.面向文檔app

索引

索引Index是es中的一個存儲數據的地方。至關於關係型數據庫中的數據庫。elasticsearch

建立一個員工索引的例子以下,建立索引還有不少選項,就不一一說明了:分佈式

POST $HOST/employee

{
  "mappings": {
    "employee": {
      "_ttl": {
        "enabled": true,
        "default": "5d"
      },
      "_timestamp": {
        "enabled": true,
        "format": "yyyy-MM-dd HH:mm:ss"
      },
      "properties": {
        "name": {
          "type": "string",
          "store": "no",
          "index": "not_analyzed",
          "index_options": "docs"
        },
        "birth_date": {
          "type": "date",
          "store": "no",
          "index": "not_analyzed",
          "index_options": "docs",
          "format": "yyyy-MM-dd HH:mm:ss"
        },
        "age": {
          "type": "date",
          "store": "no",
          "index": "not_analyzed",
          "index_options": "docs",
          "format": "yyyy-MM-dd HH:mm:ss"
        }
      }
    }
  }
}

索引建立完以後還能夠修改(添加一個hobby屬性),須要注意的是,修改mapping不容許修改屬性的類型:post

PUT $HOST/employee/employee/_mapping

{
"employee": {
    "properties": {
        "name": {
            "type": "string",
            "store": "no",
            "index": "not_analyzed",
            "index_options": "docs"
        },
        "birth_date": {
            "type": "date",
            "store": "no",
            "index": "not_analyzed",
            "index_options": "docs",
            "format": "yyyy-MM-dd HH:mm:ss"
        },
        "age": {
            "type": "date",
            "store": "no",
            "index": "not_analyzed",
            "index_options": "docs",
            "format": "yyyy-MM-dd HH:mm:ss"
        },
        "hobby" : {
            "type" : "string",
            "index_options": "docs"
        }
    }
}
}

文檔

es存儲的數據叫作文檔,文檔存儲在索引中。 每一個文檔都有4個元數據,分別是_id, _type,_index和_version。搜索引擎

_id表明文檔的惟一標識符。設計

_type表示文檔表明的對象種類。code

_index表示文檔存儲在哪一個索引。

_version表示文檔的版本,文檔被修改過一次,_version就會+1。

在員工索引中建立文檔:

POST $HOST/employee/employee

{
    "name": "format",
    "age": 100,
    "birth_date": "1900-01-01 00:00:00"
}

返回:

{
    "_index": "employee",
    "_type": "employee",
    "_id": "AU5-epuwslU6QVfs_UoX",
    "_version": 1,
    "created": true
}

修改文檔:

POST $HOST/employee/employee/AU5-epuwslU6QVfs_UoX

{
    "name": "format",
    "age": 200,
    "birth_date": "1900-01-01 00:00:00"
}

返回:

{
    "_index": "employee",
    "_type": "employee",
    "_id": "AU5-epuwslU6QVfs_UoX",
    "_version": 2,
    "created": false
}

刪除文檔:

DELETE $HOST/employee/employee/AU5-epuwslU6QVfs_UoX

返回:

{
    "found": true,
    "_index": "employee",
    "_type": "employee",
    "_id": "AU5-epuwslU6QVfs_UoX",
    "_version": 3
}

總結

寫了篇水文記錄一下es,es還有不少很強大的功能,好比一些query,filter,aggregations等。官方文檔上已經寫的很是清楚了。這裏就不講了。 - -||

相關文章
相關標籤/搜索