Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add REST endpoint to parse a query into a query AST. #4652

Merged
merged 2 commits into from
Mar 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions quickwit/quickwit-serve/src/index_api/rest_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use quickwit_proto::metastore::{
MetastoreService, MetastoreServiceClient, ResetSourceCheckpointRequest, ToggleSourceRequest,
};
use quickwit_proto::types::IndexUid;
use quickwit_query::query_ast::{query_ast_from_user_text, QueryAst};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use tracing::info;
Expand Down Expand Up @@ -88,6 +89,8 @@ pub fn index_management_handlers(
.or(delete_source_handler(index_service.metastore()))
// Tokenizer handlers.
.or(analyze_request_handler())
// Parse query into query AST handler.
.or(parse_query_request_handler())
}

fn json_body<T: DeserializeOwned + Send>(
Expand Down Expand Up @@ -866,6 +869,50 @@ async fn analyze_request(request: AnalyzeRequest) -> Result<serde_json::Value, I
Ok(json_value)
}

#[derive(Debug, Deserialize, utoipa::IntoParams, utoipa::ToSchema)]
struct ParseQueryRequest {
/// Query text. The query language is that of tantivy.
pub query: String,
// Fields to search on.
#[param(rename = "search_field")]
#[serde(default)]
#[serde(rename(deserialize = "search_field"))]
#[serde(deserialize_with = "from_simple_list")]
pub search_fields: Option<Vec<String>>,
}

fn parse_query_request_filter(
) -> impl Filter<Extract = (ParseQueryRequest,), Error = Rejection> + Clone {
warp::path!("parse-query")
.and(warp::post())
.and(warp::body::json())
}

fn parse_query_request_handler(
) -> impl Filter<Extract = (impl warp::Reply,), Error = Rejection> + Clone {
parse_query_request_filter()
.then(parse_query_request)
.and(extract_format_from_qs())
.map(into_rest_api_response)
}

/// Analyzes text with given tokenizer config and returns the list of tokens.
#[utoipa::path(
post,
tag = "parse_query",
path = "/parse_query",
request_body = ParseQueryRequest,
responses(
(status = 200, description = "Successfully parsed query into AST.")
),
)]
async fn parse_query_request(request: ParseQueryRequest) -> Result<QueryAst, IndexServiceError> {
let query_ast = query_ast_from_user_text(&request.query, request.search_fields)
.parse_user_query(&[])
.map_err(|err| IndexServiceError::Internal(err.to_string()))?;
Ok(query_ast)
}

#[cfg(test)]
mod tests {
use std::ops::{Bound, RangeInclusive};
Expand Down Expand Up @@ -1917,4 +1964,24 @@ mod tests {
expected: expected_response_json
);
}

#[tokio::test]
async fn test_parse_query_request() {
let metastore = MetastoreServiceClient::mock();
let index_service = IndexService::new(
MetastoreServiceClient::from(metastore),
StorageResolver::unconfigured(),
);
let index_management_handler =
super::index_management_handlers(index_service, Arc::new(NodeConfig::for_test()))
.recover(recover_fn);
let resp = warp::test::request()
.path("/parse-query")
.method("POST")
.json(&true)
.body(r#"{"query": "field:this AND field:that"}"#)
.reply(&index_management_handler)
.await;
assert_eq!(resp.status(), 200);
}
}
Loading