#建立索引
PUT jj
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
}
}
#檢視索引
GET jj
#查詢所有索引
GET *
#測試索引是否已存在
HEAD jj
#刪除索引
DELETE jj
#新增資料
POST /jj/_doc/
{
"title":"手機",
"images":"http://image.jj.com/12479122.jpg",
"price":2999.00
}
#ES7已經將_mapping型別刪除,7.0 開始,就被 Deprecated 了。一個索引只能建立一個 type,值為 _doc
POST /jj/_doc/
{
"title":"小米手機",
"images":"http://image.jj.com/12479122.jpg",
"price":2999.00
}
POST /jj/_doc/
{
"title":"超米手機",
"images":"http://image.jj.com/12479122.jpg",
"price":2899.00,
"stock": 200,
"saleable":true
}
POST /jj/_doc/
{
"title":"小米電視",
"images":"http://image.jj.com/12479122.jpg",
"price":3899.00
}
#自定義id
POST /jj/_doc/5
{
"title":"華為手機",
"images":"http://image.jj.com/12479122.jpg",
"price":3999.00
}
#加副標題
POST /jj/_doc/6
{
"title":"華為電視",
"subTitle":"小米合作出品",
"images":"http://image.jj.com/12479122.jpg",
"price":1999.00
}
POST /jj/_doc/7
{
"title":"OPPO手機",
"subTitle":"華為合作出品",
"images":"http://image.jj.com/12479122.jpg",
"price":1899.00
}
#刪除資料
DELETE jj/_doc/GmSfRnIBr3O86y0Oj9p-
#查詢全部
GET jj/_search
{
"query":{
"match_all":{}
}
}
#_index 文件所屬索引名稱
#_type 文件所屬型別名
#_id 文件唯一 ID
#_score 文件相關性打分
#_source 文件 JSON 資料
#_version 文件版本資訊
#其中 _type 文件所屬型別名,需要關注版本不同之間區別:
#7.0 之前,一個索引可以設定多個 types
#7.0 開始,被 Deprecated 了。一個索引只能建立一個 type,值為 _doc
#求分詞(“小米”,“手機”)的並集
GET jj/_search
{
"query": {
"match": {
"title": "小米手機"
}
}
}
#求分詞(“小米”,“手機”)的交集
GET jj/_search
{
"query": {
"match": {
"title":{
"query": "小米手機",
"operator": "and"
}
}
}
}
#求分詞(“小米”,“手機”,“電視”)的交集,加最小匹配度
GET jj/_search
{
"query": {
"match": {
"title":{
"query": "小米手機電視",
"minimum_should_match": "75%"
}
}
}
}
#多欄位查詢
GET jj/_search
{
"query": {
"multi_match": {
"query": "小米",
"fields": ["title","subTitle"]
}
}
}
#詞條匹配 詞條不可分割的
#詞條查詢和多詞條查詢使用與一些對查詢結果要求精確的資料,如價格等等
#新增了中文就查不到結果,似乎對中文的支援較差
GET jj/_search
{
"query": {
"term": {
"price": {
"value": "2899.00"
}
}
}
}
#多詞條匹配 詞條不可分割的
#新增了中文就查不到結果,似乎對中文的支援較差
GET jj/_search
{
"query": {
"terms": {
"price": [
"2899",
"2999"
]
}
}
}
#結果集過濾
GET jj/_search
{
"query": {
"multi_match": {
"query": "小米",
"fields": ["title","subTitle"]
}
}
, "_source": ["title","price"]
}
#結果集過濾(包含指定欄位第二種寫法)
GET jj/_search
{
"query": {
"multi_match": {
"query": "小米",
"fields": ["title","subTitle"]
}
}
, "_source": {
"includes": ["price","title"]
}
}
#結果集過濾(不包含指定欄位)
GET jj/_search
{
"query": {
"multi_match": {
"query": "小米",
"fields": ["title","subTitle"]
}
}
, "_source":{
"excludes": "price"
}
}
#布林查詢
#match:匹配查詢
#must+must 求交集
#should+should 求並集
#mustnot 求非
#其他兩個不演示
GET jj/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "小米"
}
},{
"terms": {
"price": [
"2999",
"2699",
"3899" ]
}
}
]
}
}
}
#範圍查詢
#gte 大於或大於等於 gt:大於
#lte 小於或小於等於 lt:小於
GET jj/_search
{
"query": {
"range": {
"price": {
"gte": 1999,
"lte": 2999
}
}
}
}
#模糊查詢
#fuzziness 支援輸入出最大錯誤數量 預設是0.5,最大是2
#新增了中文就查不到結果,似乎對中文的支援較差
GET jj/_search
{
"query": {
"fuzzy": {
"title": {
"value": "eppe"
, "fuzziness": 2
}
}
}
}
#過濾(不影響查詢物件得分)
GET jj/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "手機"
}
}
],"filter": [
{
"range": {
"price": {
"gte": 1999,
"lte": 2999
}
}
}
]
}
}
}
#單欄位排序
#降序是desc 升序是asc
GET jj/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "手機"
}
}
]
}
},"sort": [
{
"price": {
"order": "desc"
}
}
]
}
#多欄位排序(在價格相同的情況下再按id排序)
#降序是desc 升序是asc
GET jj/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "手機"
}
}
]
}
},"sort": [
{
"price": {
"order": "desc"
}
},
{
"_id":{
"order": "desc"
}
}
]
}
#聚合
#建立測試索引
PUT cars
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
}
}
DELETE cars
GET cars
#新增資料
POST /cars/_bulk?pretty
{ "index": {}}
{ "price" : 10000, "color" : "red", "make" : "honda", "sold" : "2014-10-28" }
{ "index": {}}
{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 30000, "color" : "green", "make" : "ford", "sold" : "2014-05-18" }
{ "index": {}}
{ "price" : 15000, "color" : "blue", "make" : "toyota", "sold" : "2014-07-02" }
{ "index": {}}
{ "price" : 12000, "color" : "green", "make" : "toyota", "sold" : "2014-08-19" }
{ "index": {}}
{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 80000, "color" : "red", "make" : "bmw", "sold" : "2014-01-01" }
{ "index": {}}
{ "price" : 25000, "color" : "blue", "make" : "ford", "sold" : "2014-02-12" }
GET cars
#新增對映
PUT cars/_mapping
{
"properties": {
"color": {
"type": "keyword"
},
"make": {
"type": "keyword"
}
}
}
#檢視全部資料
GET cars/_search
#根據顏色聚合
#aggs裡第一個是聚合名稱,隨便寫
#第二個是聚合型別,根據需求劃分
#加size是設定結果集的數量
GET cars/_search
{
"size": 0,
"aggs": {
"popular_color": {
"terms": {
"field": "color"
}
}
}
}
#計算每種顏色的平均價格,(巢狀度量)
GET cars/_search
{
"size": 0,
"aggs": {
"popular_color": {
"terms": {
"field": "color"
},
"aggs": {
"price_avg": {
"avg": {
"field": "price"
}
}
}
}
}
}
#計算每種顏色的平均價格及每種顏色的製造商(桶巢狀桶)
GET cars/_search
{
"size": 0,
"aggs": {
"popular_color": {
"terms": {
"field": "color"
},
"aggs": {
"price_avg": {
"avg": {
"field": "price"
}
},
"make":{
"terms": {
"field": "make"
}
}
}
}
}
}
#階梯分桶(沒一價格段的商品數量)
#min_doc_count 設定最小數量的段才顯示
GET cars/_search
{
"size": 0
, "aggs": {
"price_ histogram": {
"histogram": {
"field": "price",
"interval": 5000
, "min_doc_count": 1
}
}
}
}