elasticsearch 學習記錄

es基於lucene實現。https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.htmlhtml

es文檔元數據
一個文檔不三個必須的元數據元素以下:
_index
文檔在哪存放。一個 索引 應該是因共同的特性被分組到一塊兒的文檔集合。
_type
文檔表示的對象類別。索引子分區。
_id
文檔惟一標識。能夠用戶自定義或者自動生成.自動生成的 ID 是 URL-safe、 基於 Base64 編碼且長度爲20個字符的 GUID 字符串。 這些 GUID 字符串由可修改的 FlakeID 模式生成,這種模式容許多個節點並行生成惟一 ID ,且互相之間的衝突機率幾乎爲零。mysql

非必須的元素:web

_version,記錄文檔版本號。在 Elasticsearch 中每一個文檔都有一個版本號。當每次對文檔進行修改時(包括刪除), _version 的值會遞增。sql

 

es查詢結果標識json

took:查詢話費時間;ubuntu

shards:查詢過程參與的分片數。數組

timeout:是否超時,timeout時間能夠設置:併發

GET /_search?timeout=10ms

hits:app

total:它 包含 total 字段來表示匹配到的文檔總數,而且一個 hits 數組包含所查詢結果的前十個文檔。curl

每一個結果還有一個 _score ,它衡量了文檔與查詢的匹配程度。默認狀況下,首先返回最相關的文檔結果,就是說,返回的文檔是按照 _score 降序排列的。在這個例子中,咱們沒有指定任何查詢,故全部的文檔具備相同的相關性,所以對全部的結果而言 1 是中性的 _score 。max_score 值是與查詢所匹配文檔的 _score 的最大值。

 

t@ubuntu:~$ curl -XGET 'localhost:9200/_search?pretty'
{
  "took" : 759,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "website",
        "_type" : "blog",
        "_id" : "123",
        "_score" : 1.0,
        "_source" : {
          "title" : "My first blog entry",
          "text" : "I am starting to get the hang of this...",
          "date" : "2014/01/02"
        }
      },
      {
        "_index" : "website",
        "_type" : "blog",
        "_id" : "AV6ZDic0Gtb1Pf5XS4Nu",
        "_score" : 1.0,
        "_source" : {
          "title" : "My second blog entry",
          "text" : "Still trying this out...",
          "date" : "2014/01/01"
        }
      },
      {
        "_index" : "website",
        "_type" : "blog",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "title" : "My first blog entry",
          "text" : "Starting to get the hang of this...",
          "views" : 2,
          "tags" : [
            "testing"
          ]
        }
      }
    ]
  }
}

es增刪改查

1,保存文檔

PUT /website/blog/123
{
  "title": "My first blog entry",
  "text":  "Just trying this out...",
  "date":  "2014/01/01"
}

2,查詢文檔

  根據id查詢

GET /website/blog/123?pretty

查詢部分字段,僅查詢title和text字段

GET /website/blog/123?_source=title,text

查詢結果不須要元數據

GET /website/blog/123/_source

查詢文檔是否存在

curl -i -XHEAD http://localhost:9200/website/blog/123

存在:200

不存在:404

搜索返回指定字段

http://ip:9200/index/type/_search?_source=createTime

查詢多個文檔:

GET /_mget
{
   "docs" : [
      {
         "_index" : "website",
         "_type" :  "blog",
         "_id" :    2
      },
      {
         "_index" : "website",
         "_type" :  "pageviews",
         "_id" :    1,
         "_source": "views"
      }
   ]
}

多索引,多類型查詢:

/_search
在全部的索引中搜索全部的類型
/gb/_search
在 gb 索引中搜索全部的類型
/gb,us/_search
在 gb 和 us 索引中搜索全部的文檔
/g*,u*/_search
在任何以 g 或者 u 開頭的索引中搜索全部的類型
/gb/user/_search
在 gb 索引中搜索 user 類型
/gb,us/user,tweet/_search
在 gb 和 us 索引中搜索 user 和 tweet 類型
/_all/user,tweet/_search
在全部的索引中搜索 user 和 tweet 類型

分頁:

GET /_search?size=5&from=10

http://localhost:9200/ct_ws/type/_search?sort=createTime:desc&pretty&size=20000&from=0&_source=createTime,url

3,修改文檔

根據id,再傳一次文檔就好,version值會自動遞增

PUT /website/blog/123
{
  "title": "My first blog entry",
  "text":  "I am starting to get the hang of this...",
  "date":  "2014/01/02"
}
t@ubuntu:~$ curl -XPUT 'localhost:9200/website/blog/123?pretty' -H 'Content-Type: application/json' -d'
> {
>   "title": "My first blog entry",
>   "text":  "I am starting to get the hang of this...",
>   "date":  "2014/01/02"
> }
> '
{
  "_index" : "website",
  "_type" : "blog",
  "_id" : "123",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "created" : false
}

在es內部,舊文檔會被刪除,新文檔從新索引 ;

根據id新增字段

POST /website/blog/1/_update
{
   "doc" : {
      "tags" : [ "testing" ],
      "views": 0
   }
}

根據id修改字段,讓views值增1

POST /website/blog/1/_update
{
   "script" : "ctx._source.views+=1"
}

eg:

t@ubuntu:~$ curl -XPOST 'localhost:9200/website/blog/1/_update?pretty' -H 'Content-Type: application/json' -d'
{
   "script" : "ctx._source.views+=1"
}
'
{
  "_index" : "website",
  "_type" : "blog",
  "_id" : "1",
  "_version" : 5,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  }
}
t@ubuntu:~$ curl -XGET 'localhost:9200/website/blog/1?pretty'                                                 
{
  "_index" : "website",
  "_type" : "blog",
  "_id" : "1",
  "_version" : 5,
  "found" : true,
  "_source" : {
    "title" : "My first blog entry",
    "text" : "Starting to get the hang of this...",
    "views" : 2,
    "tags" : [
      "testing"
    ]
  }
}
t@ubuntu:~$

 

4,刪除文檔

刪除文檔不會當即將文檔從磁盤中刪除,只是將文檔標記爲已刪除狀態

根據id刪除文檔

DELETE /website/blog/123

批量操做

POST /_bulk
{ "delete": { "_index": "website", "_type": "blog", "_id": "123" }} 
{ "create": { "_index": "website", "_type": "blog", "_id": "123" }}
{ "title":    "My first blog post" }
{ "index":  { "_index": "website", "_type": "blog" }}
{ "title":    "My second blog post" }
{ "update": { "_index": "website", "_type": "blog", "_id": "123", "_retry_on_conflict" : 3} }
{ "doc" : {"title" : "My updated blog post"} }

清空索引數據,相似於mysql的drop talble操做

curl -XPOST 'http://ip:9600/megacorp/employee/_delete_by_query' -H 'Content-Type: application/json' -d'
{
     "query": {
        "match_all": {}
    }
}
'

併發控制

es經過version對併發讀寫進行控制:

對已經建立的文檔,只有versin=1時才修改

PUT /website/blog/1?version=1 
{
  "title": "My first blog entry",
  "text":  "Starting to get the hang of this..."
}

若是version值不一致,會引起報錯:

t@ubuntu:~$ 
t@ubuntu:~$ curl -XPUT 'localhost:9200/website/blog/1?version=1&pretty' -H 'Content-Type: application/json' -d'
> {
>   "title": "My first blog entry",
>   "text":  "Starting to get the hang of this..."
> }
> '
{
  "error" : {
    "root_cause" : [
      {
        "type" : "version_conflict_engine_exception",
        "reason" : "[blog][1]: version conflict, current version [2] is different than the one provided [1]",
        "index_uuid" : "aGPDbTmcTjKyhQ4fEJ6NEw",
        "shard" : "3",
        "index" : "website"
      }
    ],
    "type" : "version_conflict_engine_exception",
    "reason" : "[blog][1]: version conflict, current version [2] is different than the one provided [1]",
    "index_uuid" : "aGPDbTmcTjKyhQ4fEJ6NEw",
    "shard" : "3",
    "index" : "website"
  },
  "status" : 409
}
t@ubuntu:~$

使用外部版本號:

PUT /website/blog/2?version=5&version_type=external
{
  "title": "My first external blog entry",
  "text":  "Starting to get the hang of this..."
}
相關文章
相關標籤/搜索