1.創建索引
1.1 指定分片數量和備份數量
1.2 創建默認
2. 查看索引
2.1 查看單個索引設置
2.2 查看所有索引設置
3.文檔管理
3.1 添加文檔
3.1.1 PUT
3.1.2 POST
方式
可以不指定ID,會自動生成一個ID
3.2 查看文檔
3.2.1 查看文檔全部內容
3.2.2 查看文檔部分內容
3.3 修改文檔
3.3.1 PUT
方式
覆蓋
3.3.2 POST
方式
只修改部分數據,而不是覆蓋
3.4 刪除文檔
4. 刪除索引
5.批量操作
5.1. _mget
5.1.1 同時獲取多個文檔
5.1.2 同時獲取多個文檔的部分內容
索引相同的話,可以簡寫為如下形式
5.2. _bulk
{action:{metadata}}
{requestbody}
action:(行為)
create:文檔不存在時創建
update: 更新文檔
index:創建新文檔或替換已有文檔
delete:刪除一個文檔
metadata:
_index,_type,_id
5.2.1 創建
6. Query查詢
6.1 簡單查詢
GET /lib3/user/_search?q=name:lisi
# 篩選出包含唱歌的,並且按照年齡從大到小排序
GET /lib3/user/_search?q=internets:changge&sort=age:desc
6.2 term
查詢和terms
查詢
會根據倒排索引尋找確切的term,並不知道分詞器的存在,適合
keyword
、numeric
、date
6.3 match
查詢
知道分詞器的存在,會對
field
進行分詞操作,然后再查詢
6.3.1 multi_match
可以從多個字段中篩選出query包含的詞
6.3.2 match_phrase
短語匹配
6.4 wildcard
查詢
支持使用通配符
*
和?
來進行查詢*
代表0或多個字符?
表示任意一個字符
GET /lib3/user/_search { "query": { "wildcard": { "name": "zhao*" } } }
GET /lib3/user/_search { "query": { "wildcard": { "name": "zhaol?u" } } }
6.5 fuzzy
查詢
實現模糊查詢,只能少一個字符,多個字符依然無法查詢到
高亮
篩選字段和高亮字段要一致
6.6 基於中文的查詢
安裝ik插件
ik_max_word : 會將文本做最細粒度的拆分;盡可能多的拆分出詞語 ik_smart: 做最粗粒度拆分;已經被分出的詞語不會再被其他詞語占有
# 環境構建
PUT /lib4 { "settings": { "number_of_replicas": 1, "number_of_shards": 5 }, "mappings": { "user" :{ "properties": { "name" : {"type": "text","analyzer": "ik_max_word"}, "address" : {"type": "text","analyzer": "ik_max_word"}, "age" : {"type": "integer"}, "internets" : {"type": "text", "analyzer": "ik_max_word"}, "birthday" : {"type" : "date"} } } } }
from:指定初始位置,size表示長度
6.7 指定返回字段
GET /lib4/user/_search { "_source": ["address","name"], "query": { "match": { "internets": "唱歌" } } }
# include 包含 GET /lib4/user/_search { "query": { "match": { "internets": "唱歌" } }, "_source": { "includes": ["name","address"] } } # 不包含 GET /lib4/user/_search { "query": { "match": { "internets": "唱歌" } }, "_source": { "excludes": ["age","birthday"] } }
6.8 排序
6.9 范圍篩選
默認值都為true,包含邊界值 "include_lower" : false 不包含下邊界 "include_upper" : false 不包含上邊界
命令行式API操作
2.xx
curl -XPUT localhost:9200/lib -d'{"number_of_replicas": 1}'
6.xx
curl -X PUT "localhost:9200/lib/" -H 'Content-type: application/json' -d ' { "settings" : { "number_of_shards" : 5, "number_of_replicas" : 1 } } '