Elasticsearch 5.0-簡單查詢

Elasticsearch 5.0-簡單查詢

標籤 : Elasticsearch



本文是 Elasticsearch 5.0 系列博文的簡單查詢篇,主要介紹如何使用 Elasticsearch 進行簡單查詢

寫在前面

  • 本文以 Elasticsearch 5.0.1 版本爲例進行講解,不定期更新
  • 該系列主要參考的 Elasticsearch Reference: 5.0,儘量避免照搬翻譯,只摘錄精要部分輔以簡單說明
  • 寫這個系列博客的初衷是強迫自己梳理,同時方便一些較忙/沒空耐心看英文文檔的朋友快速上手,建議讀者有空多讀官方文檔,畢竟別人寫的都是二手資料
  • 如需查看 ES 系列更多博文,請關注我的個人網站@brianway 或者 @CSDN

數據準備

隨機 json 數據生成網站 www.json-generator.com

可以從官網下載數據 accounts.json,然後使用 _bulk api 建立索引即可

curl -O https://raw.githubusercontent.com/elastic/elasticsearch/master/docs/src/test/resources/accounts.json
curl -XPOST 'localhost:9200/bank/account/_bulk?pretty&refresh' --data-binary "@accounts.json"
curl 'localhost:9200/_cat/indices?v'

查詢API

兩種執行 search 的方式:

  • 通過 REST request URI 發送查詢參數
  • 通過 REST request body發送查詢參數
# returns all documents in the bank index
GET /bank/_search?q=*&sort=account_number:asc

# using the alternative request body method
GET /bank/_search
{
  "query": { "match_all": {} },
  "sort": [
    { "account_number": "asc" }
  ]
}

返回數據(部分省略)

{
  "took": 6,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1000,
    "max_score": null,
    "hits": [
      {
        "_index": "bank",
        "_type": "account",
        "_id": "0",
        "_score": null,
        "_source": {
          "account_number": 0,
          "balance": 16623,
          "firstname": "Bradshaw",
          "lastname": "Mckenzie",
          "age": 29,
          "gender": "F",
          "address": "244 Columbus Place",
          "employer": "Euron",
          "email": "[email protected]",
          "city": "Hobucken",
          "state": "CO"
        },
        "sort": [
          0
        ]
      },
      ...
    ]
  }
}

每個字段的含義根據字段名即可看出來,不贅述。其中:

  • took的單位是毫秒
  • hits.hits默認返回結果中的前十條記錄

需要注意的是,一旦獲得返回結果,Elasticsearch 就徹底完成了請求,且不保存任何服務端資源或者狀態信息,這和 SQL 裏一些先得到結果子集再通過狀態信息(如遊標)得到剩下結果集的情況不同

Query DSL

介紹幾個簡單的查詢,size 未指定則默認是 10,from 參數默認從零開始

# returns documents 11 through 20:
GET /bank/_search
{
  "query": { "match_all": {} },
  "from": 10,
  "size": 10
}

# sorts the results by account balance in descending order and returns the top 10 (default size) documents
GET /bank/_search
{
  "query": { "match_all": {} },
  "sort": { "balance": { "order": "desc" } }
}

# return two fields, account_number and balance
GET /bank/_search
{
  "query": { "match_all": {} },
  "_source": ["account_number", "balance"]
}

# returns the account numbered 20
GET /bank/_search
{
  "query": { "match": { "account_number": 20 } }
}

更多例子,如must,should,bool query 等等,請參考 Executing Searches

過濾器

查詢條件和文檔的相關性用 _score 這個數值字段來表示,數值越高越相關。而有時我們不需要去計算相關性,只需要確定文檔滿不滿足查詢條件即可,這時可以用 filter(過濾器).

#  uses a bool query to return all accounts with balances between 20000 and 30000
GET /bank/_search
{
  "query": {
    "bool": {
      "must": { "match_all": {} },
      "filter": {
        "range": {
          "balance": {
            "gte": 20000,
            "lte": 30000
          }
        }
      }
    }
  }
}

在上面的例子中,返回所有 balance 在 20000~30000 的文檔,所有滿足條件的文檔的匹配程度都是“等價”的,沒有誰更相關,所以計算分數是無意義且沒必要的。

聚合

aggregation(聚合)能夠對數據分組和提取統計信息,大致類似 SQL 中的 group by 和聚合函數。

Elasticsearch 能同時分別返回查詢結果和聚合結果,從而避免多次查詢。

# 類似SQL中的 SELECT state, COUNT(*) FROM bank GROUP BY state ORDER BY COUNT(*) DESC

GET /bank/_search
{
  "size": 0,
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "state.keyword"
      }
    }
  }
}

更多聚合例子參考 Executing Aggregations


作者@brianway更多文章:個人網站 | CSDN | oschina

發佈了96 篇原創文章 · 獲贊 99 · 訪問量 41萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章