Elaticsearch REST API常用技巧

2021-09-07 03:52:06 字數 2268 閱讀 3598

在elasticsearch的rest api中,有很多使用技巧,這裡針對官方文件給出的介紹,總結了幾個常用的例子。

es支援在一次請求中指定多個索引,可以使用萬用字元或者日期表示式的方式:

例如,foo*索引會匹配foo1,foo2,foo3等索引。_all則會匹配所有的索引。

同時也可以針對不可用的索引進行限制。

日期表示式支援如下的格式:

static_name是索引的靜態部分

date_math_expr是日期表示式

後面跟著date_format是日期的格式;time_zone為時區

舉個例子,如果想要查詢logstash前兩天的日期,可以寫成:

curl -xget '

localhost:9200//_search

'}

再比如,當前的時間是2023年3月22日,那麼:

會匹配 logstash-2024.03.22

會匹配 logstash-2024.03.01

會匹配 logstash-2024.03

會匹配 logstash-2024.02

12:00}} 會匹配 logstash-2024.03.23

過去三天的索引可以表示為:

curl -xget '

localhost:9200/,,/_search

'}

所有的api都可以接受乙個引數,filter_path,這個引數指定了過濾後的字段,返回的結果只會顯示過濾指定的內容:

curl -xget '

localhost:9200/_search?pretty&filter_path=took,hits.hits._id,hits.hits._score',]}}

支援使用萬用字元,進行匹配

curl -xget '

localhost:9200/_nodes/stats?filter_path=nodes.*.ho*

' }

}

如果使用了兩個**則會匹配所有的內容

curl '

localhost:9200/_segments?pretty&filter_path=indices.**.version

' }

} ],

"2": [

}} ]}},

"books":

}} ]}}

}}

如果要過濾_source,那麼需要重新指定_source中的字段:

curl -xget '

localhost:9200/_search?pretty&filter_path=hits.hits._source&_source=title

' },

}, } ]

}}

使用flat_settings引數,它只會影響到返回的內容顯示,例如設定為true後返回的內容是下面這種:

,  "

transient":

}

而設定為false,則為:

,  "

transient": }}}

正常返回的資料,可能是混雜在一行的,人的肉眼很難分別其中的資訊,這時,可以再請求的末尾新增?pretty=true,或者?format-yaml設定成可讀的形式。

pretty是以json的形式返回結果。直接寫?pretty與?pretty=true作用一樣

yaml則是使用橫向縮排的方式展現結果。

返回的結果設定為可讀,對於我們肉眼去觀察結果也很重要,比如:

"

exists_time

": "1h"

"size

": "

1kb"要比"

exists_time_in_millis

": 3600000

"size_in_bytes

": 1024

容易理解的多

yaml格式

elasticsearch apis

ReportingSerivces 常用技巧

解決重複線問題 dim name as string public function findline byval value as string as string if name value then return false else name value return true end if...

Delphi ListView的用法 常用技巧

delphi listview的用法 常用技巧 2008 02 03 11 37 增加 i listview1.items.count with listview1 do begin listitem items.add listitem.caption inttostr i listitem.su...

常用的Elasticseaerch檢索技巧彙總

本篇部落格是對前期工作中遇到es坑的一些小結,順手記錄下,方便日後查閱。為了講解不同型別es檢索,我們將要對包含以下型別的文件集合進行檢索 1 title 標題 2 authors 作者 3 summary 摘要 4 release data 發布日期 首先,讓我們借助 bulk api批量建立新的...