---------------------------
單模式下索引的建立,更新以及刪除操做
---------------------------數組
---------------------------
初始化索引
建立索引以前能夠對索引作初始化操做
好比指定shards數量以及replicas的數量app
PUT http://localhost:9200/library/ { 「settings」:{ 「index」:{ 「number_of_shards」: 5, 「number_of_replicas」: 1 } } }
上面的number_of_replicas還能夠換成:
blocks.read_only: 設爲true,則當前索引只容許讀,不容許寫或更新
blocks.read: 設爲true,則禁止讀操做
blokcs.write: 設爲true,則禁止寫操做
blocks.metadata: 設爲true,則禁止對metadata操做elasticsearch
---------------------------
建立一個索引ide
PUT /library/books/1 { 「title」: 「Elasticsearch: The Definitive Guide」, 「name」: { 「first」: 「Zachary」, 「last」: 「Tong」 }, 「publish_data」: 「2015-02-06」, 「price」: 「49.99」 }
---------------------------
ID能夠選擇不設置性能
POST /library/books/ { 「title」: 「Elasticsearch: Blueprints」, 「name」: { 「first」: 「Vineeth」, 「last」: 「Mohan」 }, 「publish_data」: 「2015-06-06」, 「price」: 「35.99」 }
---------------------------
經過ID獲取文檔信息
GET /library/books/1
GET /library/books/2
GET /library/books/AU_A-EDnU9duEv19TRm8ui
---------------------------
經過_source獲取指定的字段
GET /library/books/1?_source=title
GET /library/books/1?_source=title,price
GET /library/books/1?_sourcethis
---------------------------
更新同一個ID下的文檔,能夠經過覆蓋的方式更新code
PUT /library/books/1 { 「title」: 「Elasticsearch: The Definitive Guide」, 「name」: { 「first」: 「Zachary」, 「last」: 「Tong」 }, 「publish_data」: 「2015-02-06」, 「price」: 「59.99」 }
-----------------------------
經過_update API的方式單獨更新你想要更新的字段orm
POST /library/books/1/_update { 「doc」: { 「price」: 10 } }
------------------------------
刪除一個文檔的方法
DELETE /library/books/1
DELETE /library/books
DELETE /library排序
------------------------------
同時獲取多個文檔
GET /_megt{ 「docs」: [ { 「_index」: 「shakespeare」, 「_type」: 「line」, 「_id」: 6, 「_source」: 「play_name」 }, { 「_index」: 「shakespeare」, 「_type」: 「line」, 「_id」: 28, 「_source」: 「play_name」 } ] }
------------------------------
指定多個_source字段,數組的形式
GET /_mget { 「docs」: [ { 「_index」: 「shakespeare」, 「_type」: 「line」, 「_id」: 6 }, { 「_index」: 「shakespeare」, 「_type」: 「line」, 「_id」: 28, 「_source」: [「play_name」, 「speaker」, 「text_entry」] } ] }
-------------------------------
獲取相同index相同type下不一樣ID的文檔
GET /shakespeare/line/_megt { 「ids」: [「6」, 「28」] }
-------------------------------
多重模式
-------------------------------
-------------------------------
同時操做bulk
POST /library/books/_bulk {「index」: {「_id」: 1}} {「title」: 「Elasticsearch: The Definitive Guide」, 「price」: 5} {「index」: {「_id」: 2}} {「title」: 「The ElasticSearch cookbook」, 「price」: 12}
-------------------------------
還能夠delete,update等操做
注意delete下面沒有具體的request body
POST /library/books/_bulk {「delete」: {「_index」: 「library」, 「type」: 「books」, 「_id」: 「1」}} {「create」: {「_index」: 「music」, 「_type」: 「classical」, 「_id」: 「1」}} {「title」: 「Ave Verum Corpus」} {「index」: {「_index」: 「music」, 「_type」: 「classical」}} {「title」: 「Litaniac de Venerabili Altaris Sacromento」} {「update」: {「_index」: 「library」, 「_type」: 「books」, 「_id」: 「2」}} {「doc」: {「price」: 「18」}}
-------------------------------
Mapping映射
定義類型:
定義屬性:
-------------------------------
創建映射
POST /library { 「settings」: { 「number_of_shards」: 5, 「number_of_replicas」: 1 }, 「mappings」: { 「books」: { 「properties」: { 「title」: {「type」: 「string」}, 「name」: {「type」: 「string」, 「index」: 「not_analyzed」}, 「publish_date」: {「type」: 「date」, 「index」: 「not_analyzed」}, 「price」: {「type」, 「double」}, 「number」: {「type」: 「integer」} } } } }
-------------------------------
動態映射
PUT /library { 「mappings」: { 「books」: { 「dynamic」: 「strict」, 「properties」: { 「title」: {「type」: 「string」}, 「name」: {「type」: 「string」, 「index」: 「not_analyzed」}, 「publish_date」: {「type」: 「date」, 「index」: 「not_analyzed」}, 「price」: {「type」, 「double」}, 「number」: { 「type」: 「object」, 「dynamic」: true } } } } }
-------------------------------
獲取某個索引的映射信息
GET /library/_mapping
-------------------------------
獲取某個索引下某個type的映射信息
GET /library/_mapping/books
-------------------------------
獲取這個集羣內全部的映射信息
GET /_all/_mapping
-------------------------------
獲取之個集羣內某兩個或多個type的映射信息
GET /_all/_mapping/books,bank_account
-------------------------------
更新修改Mapping映射
mapping一旦創建,就不能修改現有的字段映射
若是要推倒現有的映射,你得從新創建一個索引,而後從新定義映射
而後把以前索引裏的數據導入到新創建的索引裏
具體的方法:
1.給現有的索引裏的數據定義一個別名,而且把現有的索引指向這個別名
2.運行:PUT /現有索引/_alias/別名A
3.新建立一個索引,定義好最新的映射
4.將別名指向新的索引,而且取消以前索引的指向
5.運行:
POST /_aliases { 「actions」: [ {「remove」: {「index」: 「現有索引名」, 「alias」: 「別名A」}}, {「add」: {「index」: 「新建索引名」, 「alias」: 「別名A」}} ] }
-------------------------------
刪除映射
DELETE /library/books DELETE /library/books/_mapping DELETE /library/_mapping/books
-------------------------------
基本查詢
-------------------------------
-------------------------------
簡單的查詢
指定index名以及type名的搜索
GET /library/books/_search?q=title:elasticsearch
指定index名沒有type的搜索
GET /llibrary/_search?q=title:MongoDB
即沒有index名也沒有type名的搜索
GET /_search?q=title:elasticsearch
-------------------------------
term查詢:查詢某字段裏有某個關鍵詞的文檔
GET /library/books/_search { 「query」: { 「term」: { 「preview」: 「elasticsearch」 } } }
terms查詢:查詢某個字段裏有多個關鍵詞的文檔
minimum_match:最小匹配集:1-說明兩個關鍵詞裏最少有一個
2-說明文檔裏這兩個關鍵詞都得存在
GET /library/books/_search { 「query」: { 「terms」: { 「preview」: [「elasticsearch」, 「book」], 「minimum_match」: 1 } } }
--------------------------------
控制查詢返回的數量
from和size
至關於MySQL裏的limit
form:從哪一個結果開始返回
size:定義返回最大的結果數
GET /library/books/_search?q=title:elasticsearch GET /library/books/_search { 「from」: 1, 「size」: 2, 「query」: { 「term」: { 「title」: 「elasticsearch」 } } }
-------------------------------
返回版本號_version
GET /library/books/_search { 「version」: true, 「query」: { 「term」: { 「perview」: 「elasticsearch」 } } }
-------------------------------
match查詢
match查詢可接受文字,數字日期等數據類型
match跟term的區別是,match查詢的時候,elasticsearch會根據你給定的字段提供合適的分析器,
而term查詢不會有分析器分析的過程
GET /library/books/_search { 「query」: { 「match」: { 「preview」: 「elasticsearch」 } } }
經過match_all查詢
查詢指定索引下的全部文檔
GET /library/books/_search { 「query」: { 「match_all」: {} } }
經過match_phrase查詢
短語查詢,slop定義的是關鍵詞之間隔多少未知單詞
GET /library/books/_search { 「query」: { 「match_phrase」: { 「preview」: { 「query」: 「elasticsearch, distributed」, 「slop」: 1 } } } }
muti_match查詢
能夠指定多個字段
好比查詢title和preview這兩個字段裏都包含Elasticsearch關鍵詞的文檔
GET /library/books/_search { 「query」: { 「multi_match」: { 「query」: 「Elasticsearch」, 「fields」: [「title」, 「preview」] } } }
---------------------------------
指定返回的字段
注意只能返連回store爲yes的字段
GET /library/books/_search { 「fields」: [「preview」, 「title」] 「query」: { 「match」: { 「preview」: 「elasticsearch」 } } }
---------------------------------
經過partial_fields控制加載的字段
GET /library/books/_search { 「partial_fields」: { 「partial」: { 「include」: [「preview」], 「exclude」: [「title」, 「price」] } }, 「query」: { 「match_all」: {} } }
---------------------------------
排序
經過sort把結果排序
GET /library/books/_search { 「query」: { 「match_all」: {} }, sort: [ { 「price」: { 「order」: 「desc」 } } ] }
-------------------------------
prefix前綴匹配查詢
GET /library/books/_search { 「query」: { 「prefix」: { 「title」: { 「value」: 「r」 } } } }
----------------------------
range查詢:範圍查詢
有from,to,include_lower,inculde_upper,boost這些參數
include_lower:是否包含範圍的左邊界,默認是true
include_upper:是否包含範圍的右邊界,默認是true
GET /library/books/_search { 「query」: { 「range」: { 「publish_date」: { 「from」: 「2015-01-01」, 「to」: 「2015-06-30」 } } } } GET /library/books/_search { 「query」: { 「range」: { 「price」: { 「from」: 「10」, 「to」: 「20」, 「include_lower」: true, 「include_upper」: false } } } }
------------------------------
wildcard查詢:容許你使用通配符*和?不進行查詢
注意:這個查詢很影響性能
GET /library/books/_search { 「query」: { 「wildcard」: { 「preview」: 「rab*」 } } }
------------------------------
fuzzy模糊查詢
value:查詢的關鍵字
boost:設置查詢的權值,默認爲1.0
min_similarity:設置匹配的最小類似度
默認值爲0.5; 對於字符串,取值爲0-1; 對於數值,取值可能大於1; 對於日期型,取值爲1d,2d,1m這樣,1d就表明一天
prefix_length: 指明區分項的共同前綴長度,默認是0
max_expansions:指明查詢中的詞項可擴展的數目,默承認以無限大
GET /library/books/_search { 「query」: { 「fuzzy」: { 「preview」: { 「value」: 「rabit」, 「min_similarity」: 0.5 } } } }
fuzzy_like_this查詢
查詢獲得與給定內容類似的全部文檔
fileds: 字段組,默認是_all
like_text: 設置關鍵詞
ignore_tf: 設置忽略詞項的頻次,默認是false
max_query_terns: 指明在生成的查詢中查詢詞項的最大數目,默認是25
min_similarity: 指明區分詞項最小的類似度,默認是0.5
prefix_length: 指明區分詞項共同前綴的長度,默認是0
boost: 設置權值,默認是1.0
analyze: 指明用於分析給定內容的分析器
GET /library/books/_search { 「query」: { 「fuzzy_like_this」: { 「fields」: [「preview」], 「like_text」: 「open source software」, 「min_similarity」: 0.5, 「prefix_length」: 0.2 } } }
fuzzy_like_this_field查詢
只做用在一個字段裏
其餘與fuzzy_like_this功能同樣
GET /library/books/_search { 「query」: { 「fuzzy_like_this_field」: { 「preview」: { 「like_text」: 「open source software」, 「min_similarity」: 0.5, 「prefix_length」: 0.2 } } } }
more_like_this查詢
fields: 定義字段組,默認是_all
like_text: 定義要查詢的關鍵詞
precent_terms_to_match: 該參數指明一個文檔必須匹配多大比例的詞項才被視爲類似。默認值是0.3,意思是30%的比例
min_term_freq: 該參數指明在生成的查詢中查詢詞項的最大數目,默認值25
stop_words: 該參數指明將被忽略的單詞集合
min_doc_freq: 該參數指明詞項應至少在多少個文檔中出現纔不會被忽略,默認是5
max_doc_freq: 該參數指明出現詞項的最大數目,以免詞項被忽略,默認是無限大
min_word_len: 該參數指明單個單詞的最大長度,高於該值的單詞將被忽略,默認是無限大
max_word_len: 該參數提高每一個單詞的權重時使用的權值。默認是1
boost: 指明提高一個查詢的權值。默認爲1.0
analyer: 指定用於分析的分析器
GET /library/books/_search { 「query」: { 「more_like_this」: { 「fields」: [「preview」], 「like_text」: 「Apache open source」, 「min_term_freq」: 1, 「min_doc_freq」: 1 } } }
more_like_this_field查詢
只做用在一個字段裏
其餘與more_like_this功能同樣
GET /library/books/_search { 「query」: { 「more_like_this_field」 { 「preview」: { 「like_text」: 「Apache open source」, 「min_trem_freq」: 1, 「min_doc_freq」: 1 } } } }
-------------------------------
filter過濾查詢
SELECT document FROM products WHERE price=20
filtered查詢價格是20的商品
GET /store/products/_search { 「query」: { 「filtered」: { 「query」: { 「match_all」: {} }, 「filter」: { 「term」: { 「price」: 20 } } } } }
也能夠指定多個值
GET /store/products/_search { 「query」: { 「filtered」: { 「filter」: { 「terms」: { 「price」: [10, 20] } } } } }
-----------------------------
bool過濾查詢,能夠作組合過濾查詢
SELECT product FROM products WHERE (price = 20 OR productID = 「SD1002136」) AND (price != 30)
相似的,elasticsearch也有and, or, not這樣的組合條件的查詢方式
{ 「bool」: { 「must」: [], 「should」: []’ 「must_not」: [] }
must: 條件必須知足,至關於AND
should: 條件能夠知足也能夠不知足,至關於OR
must_not: 條件不須要知足,至關於NOT
GET /store/products/_search { 「query」: { 「filtered」: { 「filter」: { 「bool」: { 「should」: [ {「term」: {「price」: 20}}, {「term」: {「productID」: 「SD1002136」}} ], 「must_not」: { 「term」: {「price」: 30} } } } } } }
嵌套查詢
SELECT document FORM products WHERE productID=」SD1002136」 OR (productID=」SD4535233」 AND price=30)
GET /store/products/_search { 「query」: { 「filtered」: { 「filter」: { 「bool」: { 「should」: [ {「term」: {「productID」: 「SD1002136」}}, {「bool」: { 「must」: [ {「term」: {「productID」: 「SD4535233」}}, {「term」: {「price」: 30}} ] }} ] } } } } }
另一種and, or, not查詢
沒有bool,直接使用and, or, not
查詢價格既是10元,productID又爲SD1002136的結果
GET /store/products/_search { 「query」: { 「filtered」: { 「filter」: { 「and」:[ { 「term」: { 「productID」: 「SD1002136」 } } ] }, 「query」: { 「match_all」: {} } } } }
range範圍過濾
SELECT document FROM products WHRE price BETWEEN 20 AND 40
gt: > 大於
lt: < 小於
gte: >= 大於等於
lte: <= 小於等於
GET /store/products/_search { 「query」: { 「filtered」: { 「filter」: { 「range」: { 「price」: { 「gt」: 20, 「lt」: 40 } } } } } }
--------------------------------
處理null空值的方法
SELECT tags FROM test WHERE tags IS NOT NULL SELECT tags FROM test WHERE tags IS NULL
GET /test_index/test/_search { 「query」: { 「filtered」: { 「filter」: { 「exists」: {field: 「tags」} } } } } GET /test_index/test/_search { 「query」: { 「filtered」: { 「filter」: { 「missing」: {「field」: 「tags」} } } } }
--------------------------------
跨索引查詢
GET /_search { 「query」: { 「indices」: { 「indices」: [library], 「query」: { 「term」: { 「title」: 「elasticsearch」 } }, 「no_match_query」: { 「term」: { 「price」: 10 } } } } }