Skip to content

Commit

Permalink
Add index ID to elasticsearch results.
Browse files Browse the repository at this point in the history
  • Loading branch information
fmassot committed Oct 23, 2023
1 parent 7414956 commit 67ddb6e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
4 changes: 3 additions & 1 deletion quickwit/quickwit-proto/protos/quickwit/search.proto
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,14 @@ message LeafHit {
}

message Hit {
// The actual content of the hit/
// The actual content of the hit
string json = 1;
// The partial hit (ie: the sorting field + the document address)
PartialHit partial_hit = 2;
// A snippet of the matching content
optional string snippet = 3;
// The index id of the hit
string index_id = 4;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ pub struct LeafHit {
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Hit {
/// The actual content of the hit/
/// The actual content of the hit
#[prost(string, tag = "1")]
pub json: ::prost::alloc::string::String,
/// The partial hit (ie: the sorting field + the document address)
Expand All @@ -237,6 +237,9 @@ pub struct Hit {
/// A snippet of the matching content
#[prost(string, optional, tag = "3")]
pub snippet: ::core::option::Option<::prost::alloc::string::String>,
/// The index id of the hit
#[prost(string, tag = "4")]
pub index_id: ::prost::alloc::string::String,
}
/// A partial hit, is a hit for which we have not fetch the content yet.
/// Instead, it holds a document_uri which is enough information to
Expand Down
26 changes: 25 additions & 1 deletion quickwit/quickwit-search/src/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use quickwit_proto::search::{
LeafSearchRequest, LeafSearchResponse, ListTermsRequest, ListTermsResponse, PartialHit,
SearchRequest, SearchResponse, SnippetRequest, SortField, SplitIdAndFooterOffsets,
};
use quickwit_proto::IndexUid;
use quickwit_proto::{IndexUid, SplitId};
use quickwit_query::query_ast::{
BoolQuery, QueryAst, QueryAstVisitor, RangeQuery, TermQuery, TermSetQuery,
};
Expand Down Expand Up @@ -564,6 +564,17 @@ pub(crate) async fn fetch_docs_phase(
.into_iter()
.flat_map(|response| response.hits.into_iter());

// Build map of Split ID > index ID to add the index ID to the hits.
// Used for ES compatibility.
let split_id_to_index_id_map: HashMap<&SplitId, &str> = split_metadatas
.iter()
.map(|split_metadata| {
(
&split_metadata.split_id,
split_metadata.index_uid.index_id(),
)
})
.collect();
let mut hits_with_position: Vec<(usize, Hit)> = leaf_hits
.flat_map(|leaf_hit: LeafHit| {
let partial_hit_ref = leaf_hit.partial_hit.as_ref()?;
Expand All @@ -573,12 +584,17 @@ pub(crate) async fn fetch_docs_phase(
partial_hit_ref.doc_id,
);
let position = *hit_order.get(&key)?;
let index_id = split_id_to_index_id_map
.get(&partial_hit_ref.split_id)
.map(|split_id| split_id.to_string())
.unwrap_or_default();
Some((
position,
Hit {
json: leaf_hit.leaf_json,
partial_hit: leaf_hit.partial_hit,
snippet: leaf_hit.leaf_snippet_json,
index_id,
},
))
})
Expand Down Expand Up @@ -3158,6 +3174,14 @@ mod tests {
.unwrap();
assert_eq!(search_response.num_hits, 3);
assert_eq!(search_response.hits.len(), 3);
assert_eq!(
search_response
.hits
.iter()
.map(|hit| &hit.index_id)
.collect_vec(),
vec!["test-index-2", "test-index-1", "test-index-1"]
);
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ fn convert_hit(hit: quickwit_proto::search::Hit, append_shard_doc: bool) -> Elas
ElasticHit {
fields,
explanation: None,
index: "".to_string(),
index: hit.index_id,
id: "".to_string(),
score: None,
nested: None,
Expand Down
1 change: 1 addition & 0 deletions quickwit/quickwit-serve/src/search_api/rest_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,7 @@ mod tests {
json: r#"{"title": "foo", "body": "foo bar baz"}"#.to_string(),
partial_hit: None,
snippet: Some(r#"{"title": [], "body": ["foo <em>bar</em> baz"]}"#.to_string()),
index_id: "quickwit-demo-index".to_string(),
}],
num_hits: 1,
elapsed_time_micros: 16,
Expand Down

0 comments on commit 67ddb6e

Please sign in to comment.