Chrome安裝Sense插件執(zhí)行curl請(qǐng)求
查詢(xún)ES中所有索引
curl -XPOST "http://localhost:9200/_search" -d'

image.png
CRUD
創(chuàng)建索引和文檔
索引的名稱(chēng)為“movies”,類(lèi)型名稱(chēng)(“movie”)和id(“1”),pretty美化輸出json
curl -XPUT "http://localhost:9200/movies/movie/1?pretty" -d'
{
"title": "The Godfather",
"director": "Francis Ford Coppola",
"year": 1972
}'
更新文檔
curl -XPUT "http://localhost:9200/movies/movie/1?pretty" -d'
{
"title": "The Godfather",
"director": "Francis Ford Coppola",
"year": 1972,
"genres": ["Crime", "Drama"]
}'
或者
curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/movies/movie/1/_update?pretty' -d '
{
"title": "The Godfather",
"director": "Francis Ford Coppola",
"year": 1972,
"genres": ["Crime", "Drama"]
}'
根據(jù)ID獲取文檔
curl -XGET "http://localhost:9200/movies/movie/1?pretty" -d''
刪除文檔
curl -XDELETE "http://localhost:9200/movies/movie/1" -d''
批處理(_bulk端點(diǎn))
curl -H "Content-Type: application/json" -XPOST "http://localhost:9200/movies/movie/_bulk?pretty" -d '{"index":{"_id":"2"}}{"title":"The Godfather"}{"index":{"_id":"3"}} {"title":"ABCD"} '
搜索(_search端點(diǎn))
- 簡(jiǎn)單字符串查詢(xún)
curl -XPOST "http://localhost:9200/movies/movie/_search?pretty" -d '
{
"query": {
"query_string": {
"query": "Drama"
}
}
}'
- 指定搜索的字段
curl -XPOST "http://localhost:9200/movies/movie/_search?pretty" -d'
{
"query": {
"query_string": {
"query": "Crime",
"fields": ["genres"]
}
}
}'
- 過(guò)濾
curl -XPOST "http://localhost:9200/_search" -d'
{
"query": {
"filtered": {
"query": {
"query_string": {
"query": "drama"
}
},
"filter": {
"term": { "year": 1962 }
}
}
}
}'
匹配查詢(xún)
match_all匹配一切,如果沒(méi)有指定size的值,則默認(rèn)返回前10個(gè)文檔,from指定返回從哪個(gè)文檔開(kāi)始
curl -XPOST "http://localhost:9200/_search" -d'
{
"query": {
"filtered": {
"query": {
"match_all": {
}
},
"filter": {
"term": { "year": 1962 }
}
}
},
//"sort" : [{ "排序字段" : { "order" : "desc"}},
"from":10,
"size": 1
}'
當(dāng)文檔title字段滿(mǎn)足godfather godmother abcd中任意一個(gè),則匹配成功
operator:用來(lái)控制match查詢(xún)匹配詞條的邏輯條件,默認(rèn)值是or,如果設(shè)置為and,表示查詢(xún)滿(mǎn)足所有條件;
minimum_should_match:當(dāng)operator參數(shù)設(shè)置為or時(shí),該參數(shù)用來(lái)控制應(yīng)該匹配的分詞的最少數(shù)量;
curl -XPOST "http://localhost:9200/movies/movie/_search?pretty" -d '
{
"query": {
"match": {
"title": "Godfather GodMother ABCD",
"operator":"or",
"minimum_should_match":2
}
}
}'
查詢(xún)title是The Godfather且1972年的記錄
bool參數(shù), 除了must,還有should(或),must_not(不包括),filter
curl -XPOST "http://localhost:9200/movies/movie/_search?pretty" -d '
{
"query": {
"bool": {
"must": [
{ "match": { "title": "The Godfather" ,"operator":"and"} }, //或者用match_phase
{ "match": { "year": 1972 } }
]
}
}
}'
聚合查詢(xún)
curl -XPOST "http://localhost:9200/movies/movie/_search?pretty" -d '
{
"size": 0, //表示查詢(xún)多少條文檔,聚合只需就和結(jié)果,輸出文檔可以設(shè)置為0條
"aggs": {
"price_of_sum": { //自行取名作為結(jié)果集
"sum": { //min max terms:分組聚合
"field": "price"
}
}
}
}'
查詢(xún)price范圍在(-無(wú)窮,10)、[10, 20)、[20, +無(wú)窮)這3個(gè)區(qū)間的price總和
curl -XPOST "http://localhost:9200/movies/movie/_search?pretty" -d '
{
"aggs":{
"my_agg_name":{
"range":{
"field":"price",
"ranges":[
{"to":10},
{"from":10,"to":20},
{"from":20}
]
},
"aggs":{
"my_agg_name_child":{
"sum":{"field":"price"}
}
}
}
}
}'