Skip to content

Commit

Permalink
Make hit count of search results optional
Browse files Browse the repository at this point in the history
  • Loading branch information
owi92 committed Dec 21, 2023
1 parent bba9ae2 commit d695b80
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion backend/src/api/model/known_roles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,5 @@ pub(crate) async fn search_known_users(
items.extend(results.hits.into_iter().map(|h| h.result));
}

Ok(KnownUsersSearchOutcome::Results(SearchResults { items }))
Ok(KnownUsersSearchOutcome::Results(SearchResults { items, total_hits: None }))
}
19 changes: 11 additions & 8 deletions backend/src/api/model/search/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ pub(crate) enum SearchOutcome {

pub(crate) struct SearchResults<T> {
pub(crate) items: Vec<T>,
total_hits: usize,
pub(crate) total_hits: Option<usize>,
}

#[juniper::graphql_object(Context = Context)]
impl SearchResults<NodeValue> {
fn items(&self) -> &[NodeValue] {
&self.items
}
fn total_hits(&self) -> i32 {
self.total_hits as i32
fn total_hits(&self) -> Option<i32> {
self.total_hits.map(|usize| usize as i32)
}
}

Expand Down Expand Up @@ -133,7 +133,7 @@ pub(crate) async fn perform(
.into_iter()
.collect();
let total_hits = items.len();
return Ok(SearchOutcome::Results(SearchResults { items, total_hits }));
return Ok(SearchOutcome::Results(SearchResults { items, total_hits: Some(total_hits) }));
}


Expand Down Expand Up @@ -184,10 +184,13 @@ pub(crate) async fn perform(
let mut merged = realms.chain(events).collect::<Vec<_>>();
merged.sort_unstable_by(|(_, score0), (_, score1)| score1.unwrap().total_cmp(&score0.unwrap()));

let total_hits = [event_results.estimated_total_hits, realm_results.estimated_total_hits].into_iter().flatten().sum();
let total_hits: usize = [event_results.estimated_total_hits, realm_results.estimated_total_hits]
.iter()
.filter_map(|&x| x)
.sum();

let items = merged.into_iter().map(|(node, _)| node).collect();
Ok(SearchOutcome::Results(SearchResults { items, total_hits }))
Ok(SearchOutcome::Results(SearchResults { items, total_hits: Some(total_hits) }))
}

fn looks_like_opencast_uuid(query: &str) -> bool {
Expand Down Expand Up @@ -249,7 +252,7 @@ pub(crate) async fn all_events(
let items = results.hits.into_iter().map(|h| h.result).collect();
let total_hits = results.estimated_total_hits.unwrap_or(0);

Ok(EventSearchOutcome::Results(SearchResults { items, total_hits }))
Ok(EventSearchOutcome::Results(SearchResults { items, total_hits: Some(total_hits) }))
}

// See `EventSearchOutcome` for additional information.
Expand Down Expand Up @@ -302,7 +305,7 @@ pub(crate) async fn all_series(
let items = results.hits.into_iter().map(|h| h.result).collect();
let total_hits = results.estimated_total_hits.unwrap_or(0);

Ok(SeriesSearchOutcome::Results(SearchResults { items, total_hits }))
Ok(SeriesSearchOutcome::Results(SearchResults { items, total_hits: Some(total_hits) }))
}

fn acl_filter(action: &str, context: &Context) -> Option<Filter> {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ type Track {

type SearchResults {
items: [Node!]!
totalHits: Int!
totalHits: Int
}

input UpdateTextBlock {
Expand Down

0 comments on commit d695b80

Please sign in to comment.