es基本概念及操做

es基本概念:

Elasticsearch是面向文檔型數據庫,一條數據在這裏就是一個文檔,用JSON做爲文檔序列化的格式
NRT:Nearly Real Time近實時查詢,插入數據到查詢時間大概爲1秒延時
基於lucene開發的分佈式全文檢索框架,es存儲和查詢的數據格式是jsonnode

文檔:Document es中最小的數據單元,一個Document能夠是客戶端的一條數據,一條分類數據,一條訂單數據,一般用json數據表示,每一個index 的type中能夠存儲多個document,至關於數據庫的行
{"id":"1","name":"高露潔","price":"123","desc","xxxxxx"}數據庫


索引:index  至關於數據庫中的database
類型:type     至關於數據庫中的table
主鍵:ID        至關於數據庫中的主鍵json

關係數據庫 ⇒ 數據庫 ⇒ 表 ⇒ 行 ⇒ 列(Columns)restful

Elasticsearch ⇒ 索引 ⇒ 類型 ⇒ 文檔 ⇒ 字段(Fields)框架

 

Elasticsearch分詞器:

standard 分詞器:

默認分詞器,默認狀況下一個每一箇中文都會拆分
http://xupan003:9200/_analyze?analyzer=standard&pretty=true&text=神奇的搜索引擎curl

{
  "tokens" : [
    {
      "token" : "神",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "<IDEOGRAPHIC>",
      "position" : 0
    },
    {
      "token" : "奇",
      "start_offset" : 1,
      "end_offset" : 2,
      "type" : "<IDEOGRAPHIC>",
      "position" : 1
    },
    {
      "token" : "的",
      "start_offset" : 2,
      "end_offset" : 3,
      "type" : "<IDEOGRAPHIC>",
      "position" : 2
    },
    {
      "token" : "搜",
      "start_offset" : 3,
      "end_offset" : 4,
      "type" : "<IDEOGRAPHIC>",
      "position" : 3
    },
    {
      "token" : "索",
      "start_offset" : 4,
      "end_offset" : 5,
      "type" : "<IDEOGRAPHIC>",
      "position" : 4
    },
    {
      "token" : "引",
      "start_offset" : 5,
      "end_offset" : 6,
      "type" : "<IDEOGRAPHIC>",
      "position" : 5
    },
    {
      "token" : "擎",
      "start_offset" : 6,
      "end_offset" : 7,
      "type" : "<IDEOGRAPHIC>",
      "position" : 6
    }
  ]
}

 

ik_smart分詞器:

最簡化分詞
http://xupan003:9200/_analyze?analyzer=ik_smart&pretty=true&text=神奇的搜索引擎分佈式

{
  "tokens" : [
    {
      "token" : "神奇",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "搜索引擎",
      "start_offset" : 3,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 1
    }
  ]
}

 

 

ik_max_word分詞器:

細粒度分詞
http://xupan003:9200/_analyze?analyzer=ik_max_word&pretty=true&text=神奇的搜索引擎ide

{
  "tokens" : [
    {
      "token" : "神奇",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "搜索引擎",
      "start_offset" : 3,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 1
    },
    {
      "token" : "搜索",
      "start_offset" : 3,
      "end_offset" : 5,
      "type" : "CN_WORD",
      "position" : 2
    },
    {
      "token" : "索引",
      "start_offset" : 4,
      "end_offset" : 6,
      "type" : "CN_WORD",
      "position" : 3
    },
    {
      "token" : "索",
      "start_offset" : 4,
      "end_offset" : 5,
      "type" : "CN_WORD",
      "position" : 4
    },
    {
      "token" : "引擎",
      "start_offset" : 5,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 5
    },
    {
      "token" : "擎",
      "start_offset" : 6,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 6
    }
  ]
}

 

 

 

 

restful風格的API

restful風格格式:
    http://hostname:port/<index>/<type>/<id>
    restful風格的API,經過http的形式發送請求對es進行操做
    如: 查詢方式get    
    刪除delete
    添加put/post
    修改put/postpost

 

舉個栗子:ui

# -i:添加http請求頭
# ?pretty美化輸出
# d請求主體

curl -i -XGET 'http://xupan003:9200/_count?pretty' d '{
   "query" :{"match_all"{}}
}'

 

添加

向store索引中添加一些書籍:
curl -XPUT 'http://xupan001:9200/store/books/1' -d '{
 "title" : "Elasticsearch: The Definitive Guide",
 "name" : {
     "first" : "Zachary",
     "last"     :    "Tong"
   },
   "price" : "49.9"
}'


curl -XPUT 'http://xupan001:9200/store/books/2' -d '{
 "title" : "Elasticsearch: Blueprints",
 "name" : {
     "first" : "Vineeth",
     "last"     :    "Mohan"
   },
   "publish_date" : "2015-06-06",
   "price" : "49.9"
}'


curl -XPUT 'http://xupan001:9200/store/books/4' -d '{
 "title" : "Elasticsearch:  The Definitive Guide",
 "author" : "Guide",
 "publish_date" : "2015-06-06",
 "price" : "49.9"
}'

查詢


curl -XGET 'http://xupan001:9200/store/books/1'
指定字斷查詢:
curl -XGET 'http://xupan001:9200/store/books/1?_source'
curl -XGET 'http://xupan001:9200/store/books/1?_source=title'
curl -XGET 'http://xupan001:9200/store/books/1?_source=title,price'

 

 

覆蓋更新


curl -XPUT 'http://xupan001:9200/store/books/1' -d '{
 "title" : "Elasticsearch: The Definitive Guide",
 "name" : {
     "first" : "Zachary",
     "last"     :    "Tong"
   },
   "publish_date" : "2016-02-06",
   "price" : "99.99"
}'

 

 

經過_update方式單獨更新字斷


curl -XPOST 'http://xupan001:9200/store/books/1/_update' -d '{
   "doc" :{
      "price" : "88.88"
   }
}'

 


刪除:


curl -XDELETE 'http://xupan001:9200/store/books/1'


 

 

查看:集羣健康狀態


http://xupan001:9200/_cat/health?v

epoch      timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1514503732 07:28:52  my-es   green           3         3     47  20    0    0        0             0                  -                100.0%
  • green 
    最健康得狀態,說明全部的分片包括備份均可用
  • yellow 
    基本的分片可用,可是備份不可用(或者是沒有備份)
  • red 
    部分的分片可用,代表分片有一部分損壞。此時執行查詢部分數據仍然能夠查到,遇到這種狀況,仍是趕快解決比較好

================================================

相關文章
相關標籤/搜索