Skip to content

Commit

Permalink
Add quickwit index list command (#1300)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dai Dao authored Apr 19, 2022
1 parent ab2145e commit 2edd45d
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 2 deletions.
38 changes: 38 additions & 0 deletions docs/reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,44 @@ The CLI is structured into high-level commands with subcommands.
## index
Create your index, ingest data, search, describe... every command you need to manage indexes.

### index list

List indexes.
`quickwit index list [args]`

*Synopsis*

```bash
quickwit index list
--config <config>
[--metastore-uri <metastore-uri>]
```

*Options*

`--config`
`--metastore-uri`

*Examples*

*List indexes*
```bash
quickwit index list --config ./config/quickwit.yaml
# Or with alias.
quickwit index ls --config ./config/quickwit.yaml

Indexes
+-----------+--------------------------------------------------------+
| Index ID | Index URI |
+-----------+--------------------------------------------------------+
| hdfs-logs | file:///home/quickwit-indices/qwdata/indexes/hdfs-logs |
+-----------+--------------------------------------------------------+
| wikipedia | file:///home/quickwit-indices/qwdata/indexes/wikipedia |
+-----------+--------------------------------------------------------+


```

### index create

Creates an index of ID `index` at `index-uri` configured by a [YAML config file](index-config.md) located at `index-config`.
Expand Down
19 changes: 19 additions & 0 deletions quickwit-cli/src/cli_doc_ext.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,25 @@ quickwit index search --index wikipedia --query "search" --search-fields title -
quickwit index search --index wikipedia --query "search" --search-fields title --config ./config/quickwit.yaml | jq '.hits[].title'
'''

[[index.list.examples]]
name = "List indexes"
command = '''
quickwit index list --config ./config/quickwit.yaml
# Or with alias.
quickwit index ls --config ./config/quickwit.yaml
Indexes
+-----------+--------------------------------------------------------+
| Index ID | Index URI |
+-----------+--------------------------------------------------------+
| hdfs-logs | file:///home/quickwit-indices/qwdata/indexes/hdfs-logs |
+-----------+--------------------------------------------------------+
| wikipedia | file:///home/quickwit-indices/qwdata/indexes/wikipedia |
+-----------+--------------------------------------------------------+
'''


[[index.describe.examples]]
name = "Displays descriptive statistics of your index"
command = '''quickwit index describe --index wikipedia --config ./config/quickwit.yaml
Expand Down
82 changes: 80 additions & 2 deletions quickwit-cli/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,35 @@ use quickwit_indexing::models::{
DetachPipeline, IndexingStatistics, SpawnMergePipeline, SpawnPipeline,
};
use quickwit_indexing::source::INGEST_SOURCE_ID;
use quickwit_metastore::{quickwit_metastore_uri_resolver, Split, SplitState};
use quickwit_metastore::{quickwit_metastore_uri_resolver, IndexMetadata, Split, SplitState};
use quickwit_proto::{SearchRequest, SearchResponse};
use quickwit_search::{single_node_search, SearchResponseRest};
use quickwit_storage::{load_file, quickwit_storage_uri_resolver};
use quickwit_telemetry::payload::TelemetryEvent;
use tabled::{Table, Tabled};
use thousands::Separable;
use tracing::{debug, Level};

use crate::stats::{mean, percentile, std_deviation};
use crate::{
load_quickwit_config, parse_duration_with_unit, run_index_checklist, THROUGHPUT_WINDOW_SIZE,
load_quickwit_config, make_table, parse_duration_with_unit, run_index_checklist,
THROUGHPUT_WINDOW_SIZE,
};

pub fn build_index_command<'a>() -> Command<'a> {
Command::new("index")
.about("Create your index, ingest data, search, describe... every command you need to manage indexes.")
.subcommand(
Command::new("list")
.about("List indexes.")
.alias("ls")
.args(&[
arg!(--config <CONFIG> "Quickwit config file")
.env("QW_CONFIG"),
arg!(--"metastore-uri" <METASTORE_URI> "Metastore URI. Override the `metastore_uri` parameter defined in the config file. Default to file-backed, but could be Amazon S3 or PostgreSQL.")
.required(false)
])
)
.subcommand(
Command::new("create")
.about("Creates an index from an index config file.")
Expand Down Expand Up @@ -240,8 +253,15 @@ pub struct MergeOrDemuxArgs {
pub data_dir: Option<PathBuf>,
}

#[derive(Debug, PartialEq, Eq)]
pub struct ListIndexesArgs {
pub config_uri: Uri,
pub metastore_uri: Option<Uri>,
}

#[derive(Debug, PartialEq)]
pub enum IndexCliCommand {
List(ListIndexesArgs),
Create(CreateIndexArgs),
Describe(DescribeIndexArgs),
Delete(DeleteIndexArgs),
Expand All @@ -265,6 +285,7 @@ impl IndexCliCommand {
.subcommand()
.ok_or_else(|| anyhow::anyhow!("Failed to parse sub-matches."))?;
match subcommand {
"list" => Self::parse_list_args(submatches),
"create" => Self::parse_create_args(submatches),
"delete" => Self::parse_delete_args(submatches),
"search" => Self::parse_search_args(submatches),
Expand All @@ -277,6 +298,23 @@ impl IndexCliCommand {
}
}

fn parse_list_args(matches: &ArgMatches) -> anyhow::Result<Self> {
let config_uri = matches
.value_of("config")
.map(Uri::try_new)
.expect("`config` is a required arg.")?;

let metastore_uri = matches
.value_of("metastore-uri")
.map(Uri::try_new)
.transpose()?;

Ok(Self::List(ListIndexesArgs {
config_uri,
metastore_uri,
}))
}

fn parse_describe_args(matches: &ArgMatches) -> anyhow::Result<Self> {
let index_id = matches
.value_of("index")
Expand Down Expand Up @@ -468,6 +506,7 @@ impl IndexCliCommand {

pub async fn execute(self) -> anyhow::Result<()> {
match self {
Self::List(args) => list_index_cli(args).await,
Self::Create(args) => create_index_cli(args).await,
Self::Describe(args) => describe_index_cli(args).await,
Self::Ingest(args) => ingest_docs_cli(args).await,
Expand All @@ -480,6 +519,45 @@ impl IndexCliCommand {
}
}

pub async fn list_index_cli(args: ListIndexesArgs) -> anyhow::Result<()> {
debug!(args = ?args, "list");
let metastore_uri_resolver = quickwit_metastore_uri_resolver();
let quickwit_config = load_quickwit_config(&args.config_uri, None).await?;
let metastore_uri = if let Some(uri) = args.metastore_uri {
uri.to_string()
} else {
quickwit_config.metastore_uri()
};
let metastore = metastore_uri_resolver.resolve(&metastore_uri).await?;
let res = metastore.list_indexes_metadatas().await?;
let index_table = make_list_indexes_table(res);

println!();
println!("{}", index_table);
println!();
Ok(())
}

fn make_list_indexes_table<I>(indexes: I) -> Table
where I: IntoIterator<Item = IndexMetadata> {
let rows = indexes
.into_iter()
.map(|index| IndexRow {
index_id: index.index_id,
index_uri: index.index_uri,
})
.sorted_by(|left, right| left.index_id.cmp(&right.index_id));
make_table("Indexes", rows, false)
}

#[derive(Tabled)]
struct IndexRow {
#[tabled(rename = "Index ID")]
index_id: String,
#[tabled(rename = "Index URI")]
index_uri: String,
}

pub async fn describe_index_cli(args: DescribeIndexArgs) -> anyhow::Result<()> {
debug!(args = ?args, "describe");
let metastore_uri_resolver = quickwit_metastore_uri_resolver();
Expand Down

0 comments on commit 2edd45d

Please sign in to comment.