且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

弹性搜索cat索引为JSON格式

更新时间:2023-11-26 23:06:34

cat API的文档


JSON是伟大的...对于电脑。即使它是漂亮的打印,试图找到数据中的关系是乏味的。人的眼睛,特别是当看着一个ssh终端,需要紧凑和对齐的文本。 cat API旨在满足这一需求。


换句话说,cat API旨在以此格式提供数据。所以你将要使用不同形式的API。问题是(至少这是我的问题),谷歌搜索弹性搜索列表索引提出了猫API作为第一个结果,所以这个问题是非常合理的。



要获取JSON格式的所有索引,就像运行这样一样简单:

  GET / * / _ stats 

这是很多数据。你可能想要JSON,因为你想对它进行某种处理。回到我如何发现这个问题,我实际上想要获得按商店大小排序的索引列表,我不想使用 curl sort unix命令这样做。该命令如下所示:

  GET / * / _ stats / store 
pre>

不幸的是,搜索体中的排序将不适用于 _stats 命令(至少我无法使其工作)。但是,添加存储属性将使您只收回有关每个索引的数据存储的信息。



有关指标属性的详细信息,请参见 _stats文档


Using Elasticsearch 1.7, I want to see the results of _cat/indices in JSON format. I understand the results are meant to be aligned/pretty/readable, but is there a way to convert it to JSON using Elasticsearch API?

Per the documentation of cat APIs:

JSON is great… for computers. Even if it’s pretty-printed, trying to find relationships in the data is tedious. Human eyes, especially when looking at an ssh terminal, need compact and aligned text. The cat API aims to meet this need.

In other words, the cat API is designed to give data in this format. So you will want to use a different form of the API. The problem is (at least this was my problem) that googling "elasticsearch list indices" brings up the cat API as the first result, so this question is pretty reasonable.

To get all of the indices in JSON form, it's as easy as running this:

GET /*/_stats

That's a lot of data though. You likely want JSON because you want to do some sort of processing on it. Going back to how I found this question, I actually wanted to get a list of indices sorted by the store size and I didn't want to have to use curl and sort unix commands to do so. That command looks like this:

GET /*/_stats/store

Unfortunately, the sort in the search body won't work for the _stats command (at least, I haven't been able to make it work). But adding the store attribute will make it so you only get back the information about the data stores for each index.

More information about the metrics attributes can be found in the _stats documentation.