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 extra_filters parameter to ES API #4585

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions quickwit/quickwit-query/src/query_ast/bool_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ use crate::InvalidQuery;
/// - named queries
///
/// Edge cases of BooleanQuery are not obvious,
/// and different beahvior could be justified.
/// and different behavior could be justified.
///
/// Here we aligne ourselves with Elasticsearch.
/// Here we align ourselves with Elasticsearch.
/// A boolean query is to be interpreted like a filtering predicate
/// over the set of documents.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ pub struct SearchQueryParams {
#[serde(serialize_with = "to_simple_list")]
#[serde(deserialize_with = "from_simple_list")]
#[serde(default)]
/// Additional filters to be applied to the query.
/// Useful for permissions and other use cases.
/// This is not part of the official Elasticsearch API.
pub extra_filters: Option<Vec<String>>,
#[serde(serialize_with = "to_simple_list")]
#[serde(deserialize_with = "from_simple_list")]
#[serde(default)]
pub filter_path: Option<Vec<String>>,
#[serde(default)]
pub force_synthetic_source: Option<bool>,
Expand Down
26 changes: 24 additions & 2 deletions quickwit/quickwit-serve/src/elasticsearch_api/rest_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use quickwit_proto::search::{
};
use quickwit_proto::types::IndexUid;
use quickwit_proto::ServiceErrorCode;
use quickwit_query::query_ast::{QueryAst, UserInputQuery};
use quickwit_query::query_ast::{BoolQuery, QueryAst, UserInputQuery};
use quickwit_query::BooleanOperand;
use quickwit_search::{list_all_splits, resolve_index_patterns, SearchError, SearchService};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -210,7 +210,7 @@ fn build_request_for_es_api(
let default_operator = search_params.default_operator.unwrap_or(BooleanOperand::Or);
// The query string, if present, takes priority over what can be in the request
// body.
let query_ast = if let Some(q) = &search_params.q {
let mut query_ast = if let Some(q) = &search_params.q {
let user_text_query = UserInputQuery {
user_text: q.to_string(),
default_fields: None,
Expand All @@ -224,6 +224,28 @@ fn build_request_for_es_api(
} else {
QueryAst::MatchAll
};

if let Some(extra_filters) = &search_params.extra_filters {
let queries: Vec<QueryAst> = extra_filters
.iter()
.map(|query| {
let user_text_query = UserInputQuery {
user_text: query.to_string(),
default_fields: None,
default_operator,
};
QueryAst::UserInput(user_text_query)
})
.collect();

query_ast = QueryAst::Bool(BoolQuery {
must: vec![query_ast],
must_not: vec![],
should: vec![],
filter: queries,
});
}

let aggregation_request: Option<String> = if search_body.aggs.is_empty() {
None
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Extra filters are additional filters that are applied to the query. Useful for permissions and other use cases.
json:
query:
match_all: {}
params:
extra_filters: "type:PushEvent"
expected:
hits:
total:
value: 60
--- # 2 extra filters
json:
query:
match_all: {}
params:
extra_filters: "type:PushEvent,actor.login:jadonk"
expected:
hits:
total:
value: 2
--- # Test mixing
json:
query:
query_string:
query: "type:PushEvent"
params:
extra_filters: "actor.login:jadonk"
expected:
hits:
total:
value: 2
--- # Test mixing
json:
query:
query_string:
query: "type:PushEvent"
params:
extra_filters: "type:PushEvent,actor.login:jadonk"
expected:
hits:
total:
value: 2



Loading