ElasticSearch操作命令

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"}
                }
            }
        }
    }
}'

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容