Skip to content

Commit

Permalink
elastic: get-field-limit command
Browse files Browse the repository at this point in the history
To view the current field limits.
  • Loading branch information
jasonish committed Nov 27, 2024
1 parent 4997bfe commit 02a8203
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
53 changes: 51 additions & 2 deletions src/cli/elastic/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// SPDX-FileCopyrightText: (C) 2022 Jason Ish <[email protected]>
// SPDX-License-Identifier: MIT

use crate::cli::elastic::info;
use anyhow::Result;
use clap::{Command, CommandFactory, FromArgMatches, Parser, Subcommand};
use tracing::info;

use super::set_field_limit;

Expand Down Expand Up @@ -42,8 +43,12 @@ pub(crate) struct ElasticOptions {
pub(crate) enum Commands {
/// Display infomratiot about the Elasticsearch server
Info(ElasticOptions),

/// Set the field limit
SetFieldLimit(set_field_limit::Args),

/// Get the field limit.
GetFieldLimit,
}

pub fn main_options() -> Command {
Expand All @@ -53,8 +58,52 @@ pub fn main_options() -> Command {
pub async fn main(args: &clap::ArgMatches) -> anyhow::Result<()> {
let args = Args::from_arg_matches(args)?;
match args.commands {
Commands::Info(args) => info::main(args).await?,
Commands::Info(args) => crate::cli::elastic::info::main(args).await?,
Commands::SetFieldLimit(args) => set_field_limit::main(args).await?,
Commands::GetFieldLimit => get_field_limit(&args).await?,
}
Ok(())
}

async fn get_field_limit(args: &Args) -> Result<()> {
let mut client = crate::elastic::client::ClientBuilder::new(&args.options.elasticsearch)
.disable_certificate_validation(true);
if let Some(username) = &args.options.username {
client = client.with_username(username);
}
if let Some(password) = &args.options.password {
client = client.with_password(password);
}
let client = client.build();

for index in client.get_indices_pattern("*").await? {
// Only look at indices that match the name-YYYY.MM.DD
// pattern.
if regex::Regex::new(r"^\w+-\d{4}\.\d{2}\.\d{2}$")?
.find(&index.index)
.is_none()
{
continue;
}

let settings = client.get_index_settings(&index.index).await?;
if let Some(map) = settings.as_object() {
for (index, settings) in map {
let limit = &settings["settings"]["index"]["mapping"]["total_fields"]["limit"];
match limit {
serde_json::Value::Number(limit) => {
info!("{}: {}", index, limit);
}
serde_json::Value::String(limit) => {
info!("{}: {}", index, limit);
}
_ => {
info!("{}: default (likely 1000)", index);
}
}
}
}
}

Ok(())
}
10 changes: 10 additions & 0 deletions src/elastic/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,16 @@ impl Client {
let response = serde_json::from_str(&text)?;
Ok(response)
}

pub(crate) async fn get_index_settings(
&self,
index: &str,
) -> anyhow::Result<serde_json::Value> {
let response = self.get(&format!("{}/_settings", index))?.send().await?;
let text = response.text().await?;
let response = serde_json::from_str(&text)?;
Ok(response)
}
}

#[derive(Deserialize, Debug)]
Expand Down

0 comments on commit 02a8203

Please sign in to comment.