Skip to content

Commit

Permalink
add extra_filters parameter to ES API (#4585)
Browse files Browse the repository at this point in the history
add extra_filters to ES API. Useful for permission handling.

Closes #4576
  • Loading branch information
PSeitz authored Feb 15, 2024
1 parent 002b6f5 commit 91c71fb
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 4 deletions.
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



0 comments on commit 91c71fb

Please sign in to comment.