elasticsearch 初步使用經常使用語法總結(依據官方文檔)

GET /_cat/health?v  查看健康狀態  ?v 顯示字段名javascript

GET /_cat/nodes?v  查看節點狀況java

GET /_cat/indices?v   查看索引node

PUT /customer?pretty 添加名爲customer的索引json

添加數據:數組

PUT /customer/doc/1?pretty    爲customer 添加標識爲1的文檔,內容爲"name":"John doe"  pretty打印響應json
{
  "name": "John Doe"
}
若是客戶索引事先不存在,則Elasticsearch將自動建立索引。標識同理。
POST /customer/doc?pretty  使用post將自動建立標識
{
  "name": "Jane Doe"
}
若屢次執行PUT /customer/doc/1?pretty,則提交的json內容將替換標識1下的文檔
GET /customer/doc/1?pretty    查看customer索引下標識id爲1的文檔內容
DELETE /customer?pretty  刪除customer索引

  更新:緩存

POST /customer/doc/1/_update?pretty  更新標識爲1的文檔,添加年齡字段
{
  "doc": { "name": "Jane Doe" ,"age":20}
}
POST /customer/doc/1/_update?pretty  簡單腳本,年齡將增長5,ctx.source指即將更新的當前源文檔(沒有修改源文檔)
{ 
  「script」:「ctx._source.age += 5」 
}

  批處理:post

POST /customer/doc/_bulk?pretty 更新一、2兩篇文檔
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
POST /customer/doc/_bulk?pretty更新文檔1,刪除文檔2
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}

  精確刪除操做、排序spa

POST twitter/_delete_by_query    對匹配查詢的每一個文檔執行刪除操做
{
  "query": { 
    "match": {
      "message": "some message"
    }
  }
}_delete_by_query
GET /bank/_search?q=*&sort=account_number:asc&pretty 
q=*標識全部文檔,sort=account_number:asc表示按照account_number進行排序
結果:
took - Elasticsearch執行搜索的時間(以毫秒爲單位)
timed_out - 告訴咱們搜索是否超時
_shards - 告訴咱們搜索了多少個分片,以及搜索成功/失敗分片的數量
hits - 搜索結果
hits.total - 符合咱們搜索條件的文檔總數
hits.hits - 實際的搜索結果數組(默認爲前10個文檔)
hits.sort - 對結果進行排序鍵(若是按分數排序則丟失)
hits._score並max_score- 暫時忽略這些字段
GET /bank/_search         { "query": { "match_all": {} }, "sort": [ { "account_number": "asc" } ] }

  設置查找屬性code

GET /bank/_search   
{
  "query": { "match_all": {} },
  "from": 10,   起始文檔位置
  "size": 10  展現文檔數量
}

  精確查找blog

GET /bank/_search
{
  「query」:{「match_all」:{}},
  「_source」:[「account_number」,「balance」]  只顯示bank索引的account_number和balance對應字段
}

  精確匹配及模糊查找

GET / bank / _search
{
  「query」:{「match」:{「address」:「mill lane」}} 精確匹配「address」:「maill lane」
}
若要模糊匹配,則須要將「match」更換成「match_phrase」匹配詞組

  多項匹配

GET / bank / _search / bank / _search 
{ 
  「query」:{ 
    「bool」:{      #bool與must配套使用
      「must」:[       
        {「match」:{「address」 :「mill」}},
        {「match」:{「address」:「lane」}} 
      ] 
    } 
  }         
} 
#must 必須出如今文檔中 must字句中match全部都必須爲真
#filter 必須出如今文檔中,忽略scor, filter 字句中全部match都必須爲真
#should 字句應出如今文檔中,bool中如有must或者filter子句, should中任何一個match匹配都視爲匹配
#must_not 不得出如今匹配的文檔。字句在過濾上下文中執行,忽略scor使用子句進行高速緩存。 全部match都不匹配

  

GET / bank / _search 
{ 
  「query」:{ 
    「bool」:{ 
      「must」:{「match_all」:{}}, 
      「filter」:{ 
        「range」:{   #範圍
          「balance」:{  
            「gte」:20000,  #大於
            「lte」:30000    #小於
          } 
        } 
      } 
    } 
  } 
}

  

GET /bank/_search
{
  "size": 0,
  "aggs": {
    "group_by_state": {   按狀態對全部帳戶分組
      "terms": {
        "field": "state.keyword"
      }
    }
  }
}
相似於SELECT state, COUNT(*) FROM bank GROUP BY state ORDER BY COUNT(*) DESC

  

GET /bank/_search
{
  "size": 0,
  "aggs": {
    "group_by_age": {    按年齡20-29 。。。分組 最後獲得每一個年齡段的平均帳戶餘額
      "range": {
        "field": "age",
        "ranges": [
          {
            "from": 20,
            "to": 30
          },
          {
            "from": 30,
            "to": 40
          },
          {
            "from": 40,
            "to": 50
          }
        ]
      },
      "aggs": {
        "group_by_gender": {
          "terms": {
            "field": "gender.keyword",
	"order":{    #排序
    "average_balance":"desc"  
    }
          },
          "aggs": {
            "average_balance": {  基於組合進行計算
              "avg": {
                "field": "balance"
              }
            }
          }
        }
      }
    }
  }
}
相關文章
相關標籤/搜索