Skip to content

Commit 5f8bb5d

Browse files
JerryKwanPSeitz
andauthored
add more check logic when processing search request (#3870)
* add more check logc when processing search request return error when search with start_timestamp or end_timestamp but timestamp field is not defined in index * add test case for the newly added checking logic add test case for index without timestamp defined but query with start_timestamp or end_timestamp --------- Co-authored-by: PSeitz <[email protected]>
1 parent 261c630 commit 5f8bb5d

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

quickwit/quickwit-search/src/root.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,15 @@ fn validate_request(
324324
search_request: &SearchRequest,
325325
) -> crate::Result<()> {
326326
let schema = doc_mapper.schema();
327+
if doc_mapper.timestamp_field_name().is_none()
328+
&& (search_request.start_timestamp.is_some() || search_request.end_timestamp.is_some())
329+
{
330+
return Err(SearchError::InvalidQuery(format!(
331+
"the timestamp field is not set in index: {:?} definition but start-timestamp or \
332+
end-timestamp are set in the query",
333+
search_request.index_id_patterns
334+
)));
335+
}
327336

328337
validate_requested_snippet_fields(&schema, &search_request.snippet_fields)?;
329338

quickwit/quickwit-search/src/tests.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,65 @@ async fn test_single_node_filtering() -> anyhow::Result<()> {
449449
Ok(())
450450
}
451451

452+
#[tokio::test]
453+
async fn test_single_node_without_timestamp_with_query_start_timestamp_enabled(
454+
) -> anyhow::Result<()> {
455+
let index_id = "single-node-no-timestamp";
456+
let doc_mapping_yaml = r#"
457+
tag_fields:
458+
- owner
459+
field_mappings:
460+
- name: body
461+
type: text
462+
- name: owner
463+
type: text
464+
tokenizer: raw
465+
"#;
466+
let indexing_settings_json = r#"{}"#;
467+
let test_sandbox = TestSandbox::create(
468+
index_id,
469+
doc_mapping_yaml,
470+
indexing_settings_json,
471+
&["body"],
472+
)
473+
.await?;
474+
475+
let mut docs = Vec::new();
476+
let start_timestamp = OffsetDateTime::now_utc().unix_timestamp();
477+
for i in 0..30 {
478+
let body = format!("info @ t:{}", i + 1);
479+
docs.push(json!({"body": body}));
480+
}
481+
test_sandbox.add_documents(docs).await?;
482+
483+
let search_request = SearchRequest {
484+
index_id_patterns: vec![index_id.to_string()],
485+
query_ast: qast_json_helper("info", &["body"]),
486+
start_timestamp: Some(start_timestamp + 10),
487+
end_timestamp: Some(start_timestamp + 20),
488+
max_hits: 15,
489+
..Default::default()
490+
};
491+
let single_node_response = single_node_search(
492+
search_request,
493+
test_sandbox.metastore(),
494+
test_sandbox.storage_resolver(),
495+
)
496+
.await;
497+
498+
assert!(single_node_response.is_err());
499+
assert_eq!(
500+
single_node_response.err().map(|err| err.to_string()),
501+
Some(
502+
"the timestamp field is not set in index: [\"single-node-no-timestamp\"] definition \
503+
but start-timestamp or end-timestamp are set in the query"
504+
.to_string()
505+
)
506+
);
507+
test_sandbox.assert_quit().await;
508+
Ok(())
509+
}
510+
452511
async fn single_node_search_sort_by_field(
453512
sort_by_field: &str,
454513
fieldnorms_enabled: bool,

0 commit comments

Comments
 (0)