文章目錄
增加
PUT index/type/1
{
"name":"your name",
"age":100
"tags":"帥"
}
刪除
DELETE index # 刪除整個索引 等於刪除整個數據庫
DELETE index/type/1 # 刪除某個文檔
# 還可以使用POST刪除,不推薦
POST index/type/_delete_by_query?q=name:'xxx'
更新
如果使用 PUT 將會更新整個文檔內容
更新個別字段 需要使用POST
POST index/type/1
{
"name":"new name"
}
查詢
兩種查詢方式:
- 字符串 query string
- DSL
GET index/type/_search #查詢所有
GET index/type/_search
{
"query":{
"match_all":{}
}
} #查詢所有
GET index/type/_search?q=name:"xxx" # query string 查詢
GET index/type/_search
{
"query":{
"match":{
"name":"xxx"
}
}
} # DSL查詢
排序
GET index/type/_search
{
"query":{
"match_all":{}
},
"sort":{
"age":{
"order":"desc" # 還可以是asc,與mysql一樣。
}
}
}
分頁
GET index/type/_search
{
"query":{
"match_all":{}
},
"from":0, # 從第一條開始,包含第一條數據
"size":2, # 返回兩條
}
GET index/type/_search
{
"query":{
"match_all":{}
},
"from":1, # 從第二條開始,包含第二條數據
"size":2, # 返回兩條
}
布爾查詢
- should
- must
- must_not
# 布爾查詢:should(or) must(and) must_not(not)
# or
GET index/type/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "liunan5"
}
},
{
"match": {
"age": "11"
}
}
]
}
}
}
# 查詢性別是男的,年齡是44的
# and
GET index/type/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"age": 44
}
},
{
"match": {
"sex": "男"
}
}
]
}
}
}
# 查詢性別不是男的,年齡也不是55的
# not
GET index/type/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"sex": "男"
}
},
{
"match": {
"age": 55
}
}
]
}
}
}
比較運算
gt 大於
gte 大於等於
lt 小於
lte 小於等於
# 查詢年齡大於20的男的文檔
GET index/type/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"sex": "男"
}
}
],
"filter": {
"range": {
"age": {
"gt": 22
}
}
}
}
}
}
高亮查詢
# 查詢name是liunan1的文檔,並高亮顯示關鍵字
GET index/type/_search
{
"query": {
"match": {
"name": "liunan1"
}
},
"highlight": {
"fields": {
"name": {
}
}
}
}
# 自定義高亮樣式
GET index/type/_search
{
"query": {
"match": {
"desc": "厲害" # 關鍵字查詢
}
},
"highlight": {
"pre_tags": "<b style='color:red;font-size:20px;'>", # 關鍵字前面的標籤
"post_tags": "</b>", # 關鍵字後面的標籤
"fields": {
"desc":{} # 需要高亮顯示的字段
}
}
}
結果過濾
GET index/type/_search
{
"query": {
"match": {
"name": "liunan1"
}
},
"_source": ["name","age"] # 過濾
}
聚合查詢
- SUM
- MAX
- MIN
- AVG
# sum 查詢所有的男生年齡和
GET index/type/_search
{
"query": {
"match": {
"sex": "男"
}
},
"aggs": {
"my_sum": {
"sum": {
"field": "age"
}
}
}
}
# max 查詢最大的男生的年齡
GET index/type/_search
{
"query": {
"match": {
"sex": "男"
}
},
"aggs": {
"my_max": {
"max": {
"field": "age"
}
}
}
}
# min 查詢最小的男生的年齡
GET index/type/_search
{
"query": {
"match": {
"sex": "男"
}
},
"aggs": {
"my_min": {
"min": {
"field": "age"
}
}
}
}
# avg 查詢所有人的平均年齡
GET index/type/_search
{
"aggs": {
"my_avg": {
"avg": {
"field": "age"
}
}
}
}
分組
# 年齡10-20,20-30,30-100分組
GET index/type/_search
{
"query": {
"match": {
"sex": "男"
}
},
"aggs": {
"my_group": {
"range": {
"field": "age",
"ranges": [
{
"from": 10,
"to": 20
},
{
"from": 20,
"to": 30
},
{
"from": 30,
"to": 100
}
]
}
}
}
}
分組後聚合
# 分組,年齡10-20,20-30,30-100分組
# 對每組年齡求和
GET index/type/_search
{
"aggs": {
"my_group": {
"range": {
"field": "age",
"ranges": [
{
"from": 10,
"to": 20
},
{
"from": 20,
"to": 30
},
{
"from": 30,
"to": 100
}
]
},
"aggs": {
"my_sum": {
"sum": {
"field": "age"
}
}
}
}
}
}
mapping 映射
用 dynamic 來設置 . 有三種映射類型:
- dynamic : true
- dynamic : false
- dynamic : strict
true : 默認的映射類型,不需要手動設置, 當向文檔裏添加了新字段時,會自動爲該自動添加映射關係
false : 當 dynamic 設置爲 false 時, 可以向文檔裏添加新字段 , 但是不會爲添加的新字段建立映射關係. 新字段無法作爲查詢條件進行查詢(查詢也沒有結果) , 新增的字段只能當作伴隨字段 隨查詢結果一起返回.
strict : 當 dynamic 設置爲 strict 時 , 不可以 向文檔裏添加新字段 , 添加會 報錯
PUT s4
{
"mappings": {
"doc":{
"dynamic":true,
"properties":{
"name":{
"type":"text"
}
}
}
}
}
GET s4/_mapping
PUT s5
{
"mappings": {
"doc":{
"dynamic":false,
"properties":{
"name":{
"type":"text"
}
}
}
}
}
GET s5/_mapping
PUT s6
{
"mappings": {
"doc":{
"dynamic":"strict",
"properties":{
"name":{
"type":"text"
}
}
}
}
}
GET s6/_mapping
ignore_above
超過 ignore_above 設置的字符串將不會被索引 或者 存儲 , 對於字符串數組, ignore_above 將分別應用於每一個數組元素 .
PUT s7
{
"mappings": {
"doc":{
"properties":{
"title":{
"type":"keyword",
"ignore_above":10 # 這裏默認是256,可以自己設置
}
}
}
}
}
index
index 默認爲 true , 如果設置成 false , es則不會爲該屬性建立索引, 也就是說, 無法當作主查詢條件
PUT s8
{
"mappings": {
"doc":{
"properties":{
"t1":{
"type":"text",
"index":true
},
"t2":{
"type":"text",
"index":false
}
}
}
}
}
PUT s8/doc/1
{
"t1":"好餓",
"t2":"不是很餓"
}
GET s8/doc/_search
{
"query": {
"match": {
"t1": "餓"
}
}
}
GET s8/doc/_search
{
"query": {
"match": {
"t2": "餓" # 報錯!
}
}
}
copy_to
PUT s9
{
"mappings": {
"doc":{
"properties":{
"t1":{
"type":"text",
"copy_to":"full_name" # 這裏也可以是一個數組["full_name1","full_name2"]
},
"t2":{
"type":"text",
"copy_to":"full_name"
},
"full_name":{
"type":"text"
}
}
}
}
}
PUT s9/doc/1
{
"t1":"ooo",
"t2":"xxx"
}
PUT s9/doc/2
{
"t1":"oooxxx",
"t2":"xxx"
}
GET s9/doc/_search
{
"query": {
"match": {
"t1": "ooo"
}
}
}
GET s9/doc/_search
{
"query": {
"match": {
"full_name": "xxx"
}
}
}
對象類型,嵌套
PUT w1
{
"mappings": {
"doc":{
"properties":{
"name":{
"type":"text"
},
"age":{
"type":"long"
},
"info":{
"properties":{
"addr":{
"type":"text"
},
"tel":{
"type":"long"
}
}
}
}
}
}
}
PUT w1/doc/1
{
"name":"liunan",
"age":19,
"info":{
"addr":"北京",
"tel":110
}
}
GET w1/doc/_search
{
"query": {
"match": {
"info.addr": "北"
}
}
}
settings設置
設置主,複製分片
主分片默認是5
複製分片默認是1
主分片一經定義不可以修改,複製分片後期可以改。
PUT w2
{
"mappings": {
"doc":{
"properties":{
"title":{
"type":"text"
}
}
}
},
"settings": {
"number_of_shards": 3,
"number_of_replicas": 3
}
}
一個mapping示例
PUT w3
{
"mappings": {
"doc":{
"properties":{
"name":{
"type":"text",
"index":true
},
"age":{
"type":"long",
"index":false
},
"desc":{
"type":"keyword",
"ignore_above":128,
"copy_to":["t1","t2"]
},
"tags":{
"type":"keyword",
"index":false,
"copy_to":["t1","t2"]
},
"info":{
"properties":{
"addr":{
"type":"text"
},
"tel":{
"type":"long"
}
}
},
"t1":{
"type":"text"
},
"t2":{
"type":"text"
}
}
}
},
"settings": {
"number_of_shards": 3,
"number_of_replicas": 3
}
}
match 系列
關鍵字查詢 match
- 返回所有匹配的分詞
match 查詢的是散列映射,包含了我們希望搜索的字段和字符串。
也就是說,只要文檔中 只要有 我們希望的那個關鍵字 ,就會返回結果。
關鍵字會被分詞 之後進行查詢
GET index/type/_search
{
"query":{
"match":{
"name":"xxx"
}
}
} # DSL查詢
# 標準的分析器
# 查看對中國的分析過程
GET _analyze
{
"analyzer": "standard",
"text":"中國"
}
# 分析結果,發現分析器將 “中國” 分詞了
{
"tokens" : [
{
"token" : "中",
"start_offset" : 0,
"end_offset" : 1,
"type" : "<IDEOGRAPHIC>",
"position" : 0
},
{
"token" : "國",
"start_offset" : 1,
"end_offset" : 2,
"type" : "<IDEOGRAPHIC>",
"position" : 1
}
]
}
查詢全部 match_all
- 查詢全部
GET index/type/_search
{
"query":{
"match_all":{}
}
}
短語查詢 match_phrase
- 短語查詢,在match 的基礎上進一步查詢詞組,可以指定slop分詞間隔。
將關鍵字 作爲 短語 進行查詢,不會對關鍵字進行分詞後查詢。
GET t1/doc/_search
{
"query": {
"match_phrase": {
"title": "中國"
}
}
}
# 這條查詢只能查出 完整 包含 “中國” 關鍵字的結果。
短語分割
用slop來分割 短語
以下語句表示 允許 中國 世界 之間有一個間隔。
GET t1/doc/_search
{
"query": {
"match_phrase": {
"title": {
"query": "中國世界",
"slop": 1
}
}
}
}
最左前綴查詢 match_phrase_prefix
- 前綴查詢,根據短語中最後一個詞組做前綴匹配,可以應用於搜索提示
PUT t2/doc/1
{
"title":"beautiful girl"
}
PUT t2/doc/2
{
"title":"beautiful so"
}
GET t2/doc/_search
{
"query": {
"match_phrase_prefix": {
"title": "so"
}
}
}
多字段查詢 multi_match
- 多字段查詢,使用相當靈活,可以完成 match_phrase 和 match_phrase_prefix 的工作。
# 多字段聯合查詢
GET t2/doc/_search
{
"query": {
"multi_match": {
"query": "beautiful",
"fields": ["t1","t2"]
}
}
}
GET t2/doc/_search
{
"query": {
"multi_match": {
"query": "beautiful",
"fields": ["t1","t2"],
"type":"phrase" # 還可以phrase_prefix
}
}
}