ElasticSearch(四) API 約定


title: ElasticSearch(四) API 約定
tags: ElasticSearch
author: Clown95


API 約定

現在我們對Elasticsearch有些瞭解,現在我們來了解下它的API,Elasticsearch提供了一個REST API,是通過HTTP訪問JSON

注意:學到這裏相信大家對ES的交互有了足夠的瞭解,所以下面爲了文章的簡潔性,我將不再列出Curl命令。

數據準備

爲了方便演示我們再重新創建一些索引

POST /_bulk
{"create":{"_index":"othermovies","_type":"doc","_id":"1"}}
{"title": "復仇者聯盟4","director": "安東尼·羅素","year": 2019,"genres": ["科幻","動作"],"actors": ["小羅伯特·唐尼","克里斯·埃文斯","娜塔麗·波曼"]}
{"create":{"_index":"othermovies","_type":"doc","_id":"2"}}
{"title": "愛在記憶消失前","director": "保羅·維爾奇","year": 2017,"genres": ["劇情"],"actors": ""}
{"create":{"_index":"othermovies","_type":"doc","_id":"3"}}
{"title": "肥貓流浪記","director": "曹建南","year": 1988,"genres": ["劇情"], "actors": "鄭則仕"}

逗號分隔符

大多數引用index參數的API都支持使用index1,index2,index3簡單的表示方法來對索引進行操作

例如,我們查詢了索引moviesothermovies中的包含愛在的文檔信息:

POST /movies,othermovies/_search
{
  "query": {
    "match": {
      "title": "愛在"
    }
  }
}

通過響應消息知道我們成功匹配出了愛在日落黃昏時愛在記憶消失前

通配符號 * 和 -

它還支持通配符,例如:test*或*test或te*t或*test*

我們這裏只同匹配moives即可,如:

POST /*movies/_search
{
  "query": {
    "match": {
      "title": "流浪"
    }
  }
}

匹配出了流浪地球肥貓流浪記

它還支持“排除”(-)的能力,例如:test*,-test3。

注意: - 必須和 * 一塊使用

POST /*movies,-othermovies/_search
{
  "query": {
    "match": {
      "title": "仇"
    }
  }
}

只匹配到了V字仇殺隊

ignore_unavailable

如果URL中一個或多個索引不存在的時候,是否忽略這些索引,值爲true和false,默認爲true。

例如,classicmovies索引存在,但comedymovies不存在。

首先我們來執行下沒有ignore_unavailable參數的

POST /movies,comedymovies/_search
{
    "query": {
        "match": {
            "query": "西遊"
        }
    }
}

響應信息:

{
  "error": {
    "root_cause": [
      {
        "type": "index_not_found_exception",
        "reason": "no such index",
        "resource.type": "index_or_alias",
        "resource.id": "comedymovies",
        "index_uuid": "_na_",
        "index": "comedymovies"
      }
    ],
    "type": "index_not_found_exception",
    "reason": "no such index",
    "resource.type": "index_or_alias",
    "resource.id": "comedymovies",
    "index_uuid": "_na_",
    "index": "comedymovies"
  },
  "status": 404
}

我們從響應信息可以看出查詢報錯了。

接下來我們添加上ignore_unavailable 參數,並且指定爲true值防止錯誤。

POST /movies,comedymovies/_search?ignore_unavailable=true
{
    "query": {
        "query_string": {
            "query": "西遊"
        }
    }
}

命令執行成功

allow_no_indices

當使用通配符查詢時,當有索引不存在的時候是否返回查詢失敗。值爲true和false,默認爲true.

allow_no_indicesignore_unavailable 都是用來防止沒有索引的錯誤的,它們的區別是:
ignore_unavailable控制的是任何索引包括帶通配符和不帶通配符的,
allow_no_indices 控制的是帶通配符的索引。

例如,不是以act開頭的索引
我們使用false 值來執行一遍看看

POST /oth1er*/_search?allow_no_indices=false
{
    "query": {
        "query_string": {
            "query": "西遊"
        }
    }
}

設爲true查看下執行情況

POST /oth1er*/_search?allow_no_indices=true
{
    "query": {
        "match": {
            "query": "西遊"
        }
    }
}

經過我多次測試發現,使用了\* 通配符,即使不添加allow_no_indices=true 參數也能成功執行,所以我懷疑es默認添加了allow_no_indices=true,但是目前我在文檔上沒有查找到這個說明。

expand_wildcards

設置是否擴展通配符到closed的index中,open表示只在匹配併爲open的index中查詢,closed表示在匹配的所有的index中查詢, 默認爲closed

值爲open,close,none,all。

說明
open 表示只支持open類型的索引
close 表示只支持關閉狀態的索引
none 表示不可用
all 表示同時支持open和close索引

現在我們來關閉othermovies這個索引

POST /othermovies/_close

現在我們來查詢這個索引看看

GET /othermovies/moive/1

enter description here
我們可以查看到,這個索引已經被關閉 "reason":"closed"

下面我們使用expand_wildcards=closed 來查詢看看

GET /*movies/_search?expand_wildcards=close

enter description here

結果錯誤,提示沒有有效的擴展通配符值

下面我們使用expand_wildcards=open 來查詢看看

curl -H "Content-Type: application/json" -GET 'http://localhost:9200/*movies/_search?expand_wildcards=open'

enter description here
我們可以看到匹配到了其他的索引。

常見參數

format

format: 表示返回數據的格式, 可選值爲yaml和json兩種。

首先我們返回json格式:

GET /movies/doc/1?format=json

響應信息:

{
  "_index": "movies",
  "_type": "doc",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "title": "大話西遊",
    "director": "劉鎮偉",
    "year": 1994,
    "genres": [
      "喜劇",
      "愛情",
      "魔幻"
    ],
    "actors": [
      "周星馳",
      "朱茵",
      "吳孟達",
      "羅家英",
      "藍潔瑛",
      "莫文蔚"
    ]
  }
}

接着我們看下yaml格式的:

GET /movies/doc/1?format=yaml

響應信息:

---
_index: "movies"
_type: "doc"
_id: "1"
_version: 1
found: true
_source:
  title: "大話西遊"
  director: "劉鎮偉"
  year: 1994
  genres:
  - "喜劇"
  - "愛情"
  - "魔幻"
  actors:
  - "周星馳"
  - "朱茵"
  - "吳孟達"
  - "羅家英"
  - "藍潔瑛"
  - "莫文蔚"

pretty

pretty 漂亮的結果,表示在已json格式返回數據時是否以可視化的格式返回, false或未在設置表示不格式化, 否則格式化。

這個必須使用 Crul命令來查看, 因爲kibana會幫我們自動格式化,體現不出來效果。

首先我們看下不加pretty=false的:

curl -XGET 'http://localhost:9200/movies/doc/1?pretty=false'

響應信息:
enter description here
可以看到如果pretty=false 是以壓縮形式顯示的,對閱讀照成一定的難度。

再來看下pretty=true的:

curl -XGET 'http://localhost:9200/movies/doc/1?pretty=true'

響應信息:
enter description here

可以看到我們得到了一個格式化的json信息。

filter_path

查詢結果過濾,主要使用filter_path參數進行設置

比如說我們只需要 took, hits.total, hits.hits._id, hits._source

例如:

curl -XGET 'http://localhost:9200/movies/doc/_search?filter_path=took,hits.total,hits.hits._id,hits.hits._source'

比如說我們需要查詢節點信息,但是節點信息很多。

我們可以使用通配符,例如:

curl -XGET 'http://localhost:9200/_nodes/stats?filter_path=nodes.*.*ost*,nodes.*.os.*u&pretty=true'

響應消息:

{
  "nodes": {
    "g1nsooFJSAiopLEAfxBnhA": {
      "host": "10.211.55.8",
      "os": {
        "cpu": {
          "percent": 4,
          "load_average": {
            "1m": 0.26,
            "5m": 0.15,
            "15m": 0.15
          }
        }
      }
    }
  }
}

filter_path 暫時說到這裏,如果你需要了解更多的用法可以查看官方文檔。

日期篩選

日期篩選可以限定時間序列索引的搜索範圍,而不是搜索全部內容,通過時間限定,可以從集羣中減少搜索的內容,提高搜索效率和減少資源佔用。例如只搜索最近兩天的錯誤日誌。

幾乎所有的API都支持日期篩選。

日期篩選的語法爲:
<static_ name{date_ math_ expr{date_ format |time_ zone}}>

語法解釋:

  • static_ name:索引的名稱;
  • date_ math_ expr: 動態日期計算表達式;
  • date_ format:日期格式;
  • time_ zone: 時區,默認爲UTC。

例如:

curl -XGET 'http://localhost:9200/<*moives-{now/d}>/_search'

表達式說明:

表達式 解析
<logstash-{now/d}> logstash-2024.03.22
<logstash-{now/M}> logstash-2024.03.01
<logstash-{now/M{YYYY.MM}}> ogstash-2024.03
<logstash-{now/M-1M{YYYY.MM}}> logstash-2024.02
<logstash-{now/d{YYYY.MM.dd|+12:00}}> logstash-2024.03.23

時間搜索也可以通過逗號,來選擇多個時間,例如,選擇最近三天的數據。

基於URL的訪問控制

當多用戶通過URL訪問Elasticsearch索引的時候,爲了防止用戶誤刪除等操作,可以通過基於URL的訪問控制來限制用戶對某個具體索引的訪問。在elasticsearch.yml配置文件中添加參數:
rest.action.multi.allow_explicit_index: false
這個參數默認爲true。當該參數爲false時,在請求參數中指定具體索引的請求將會被拒絕。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章