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

update tantivy #3914

Merged
merged 3 commits into from
Oct 6, 2023
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
26 changes: 13 additions & 13 deletions quickwit/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion quickwit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ quickwit-serve = { version = "0.6.3", path = "./quickwit-serve" }
quickwit-storage = { version = "0.6.3", path = "./quickwit-storage" }
quickwit-telemetry = { version = "0.6.3", path = "./quickwit-telemetry" }

tantivy = { git = "https://github.com/quickwit-oss/tantivy/", rev = "0241a05b", default-features = false, features = [
tantivy = { git = "https://github.com/quickwit-oss/tantivy/", rev = "b700c42", default-features = false, features = [
"mmap",
"lz4-compression",
"zstd-compression",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use indexmap::IndexSet;
use quickwit_datetime::{DateTimeInputFormat, DateTimeOutputFormat};
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::Value as JsonValue;
use tantivy::schema::Value as TantivyValue;
use tantivy::schema::OwnedValue as TantivyValue;
use tantivy::DateTimePrecision;

use super::default_as_true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ use serde::{Deserialize, Serialize};
use serde_json::{self, Value as JsonValue};
use tantivy::query::Query;
use tantivy::schema::{
Field, FieldType, FieldValue, Schema, Value as TantivyValue, INDEXED, STORED,
Field, FieldType, FieldValue, OwnedValue as TantivyValue, Schema, INDEXED, STORED,
};
use tantivy::tokenizer::TokenizerManager;
use tantivy::Document;
use tantivy::TantivyDocument as Document;

use super::field_mapping_entry::RAW_TOKENIZER_NAME;
use super::DefaultDocMapperBuilder;
Expand Down Expand Up @@ -396,27 +396,63 @@ fn extract_single_obj(
);
}
match values.pop() {
Some(TantivyValue::JsonObject(dynamic_json_obj)) => Ok(Some(dynamic_json_obj)),
Some(TantivyValue::Object(dynamic_json_obj)) => Ok(Some(
dynamic_json_obj
.into_iter()
.map(|(key, val)| (key, tantivy_value_to_json(val)))
.collect(),
)),
Some(_) => {
bail!("the `{key}` value has to be a json object");
}
None => Ok(None),
}
}

// TODO: Formatting according to mapper if applicable
fn tantivy_value_to_json(val: TantivyValue) -> JsonValue {
match val {
TantivyValue::Null => JsonValue::Null,
TantivyValue::Str(val) => JsonValue::String(val),
TantivyValue::PreTokStr(val) => JsonValue::String(val.text),
TantivyValue::U64(val) => JsonValue::Number(val.into()),
TantivyValue::I64(val) => JsonValue::Number(val.into()),
TantivyValue::F64(val) => serde_json::json!(val),
TantivyValue::Bool(val) => JsonValue::Bool(val),
TantivyValue::Date(val) => JsonValue::String(format!("{:?}", val)),
TantivyValue::Facet(val) => JsonValue::String(val.to_string()),
TantivyValue::Bytes(val) => JsonValue::String(format!("{:?}", val)),
TantivyValue::Array(val) => val.into_iter().map(tantivy_value_to_json).collect(),
TantivyValue::Object(val) => val
.into_iter()
.map(|(key, val)| (key, tantivy_value_to_json(val)))
.collect(),
TantivyValue::IpAddr(val) => JsonValue::String(format!("{:?}", val)),
}
}

#[inline]
fn populate_field_presence_for_json_value(
json_value: &JsonValue,
json_value: &TantivyValue,
path_hasher: &PathHasher,
is_expand_dots_enabled: bool,
output: &mut FnvHashSet<u64>,
) {
match json_value {
JsonValue::Null => {}
JsonValue::Bool(_) | JsonValue::Number(_) | JsonValue::String(_) => {
TantivyValue::Null => {}
TantivyValue::Bool(_)
| TantivyValue::F64(_)
| TantivyValue::I64(_)
| TantivyValue::U64(_)
| TantivyValue::PreTokStr(_)
| TantivyValue::Date(_)
| TantivyValue::Facet(_)
| TantivyValue::Bytes(_)
| TantivyValue::IpAddr(_)
| TantivyValue::Str(_) => {
output.insert(path_hasher.finish());
}
JsonValue::Array(items) => {
TantivyValue::Array(items) => {
for item in items {
populate_field_presence_for_json_value(
item,
Expand All @@ -426,7 +462,7 @@ fn populate_field_presence_for_json_value(
);
}
}
JsonValue::Object(json_obj) => {
TantivyValue::Object(json_obj) => {
populate_field_presence_for_json_obj(
json_obj,
path_hasher.clone(),
Expand All @@ -438,7 +474,7 @@ fn populate_field_presence_for_json_value(
}

fn populate_field_presence_for_json_obj(
json_obj: &JsonObject,
json_obj: &BTreeMap<String, TantivyValue>,
path_hasher: PathHasher,
is_expand_dots_enabled: bool,
output: &mut FnvHashSet<u64>,
Expand Down Expand Up @@ -474,7 +510,14 @@ impl DocMapper for DefaultDocMapper {
let mut document = Document::default();

if let Some(source_field) = self.source_field {
document.add_json_object(source_field, json_obj.clone());
document.add_object(
source_field,
json_obj
.clone()
.into_iter()
.map(|(key, val)| (key, TantivyValue::from(val)))
.collect(),
);
}

let mode = self.mode.mode_type();
Expand All @@ -488,7 +531,13 @@ impl DocMapper for DefaultDocMapper {

if let Some(dynamic_field) = self.dynamic_field {
if !dynamic_json_obj.is_empty() {
document.add_json_object(dynamic_field, dynamic_json_obj);
document.add_object(
dynamic_field,
dynamic_json_obj
.into_iter()
.map(|(key, val)| (key, TantivyValue::from(val)))
.collect(),
);
}
}

Expand All @@ -507,7 +556,7 @@ impl DocMapper for DefaultDocMapper {
}
let mut path_hasher: PathHasher = PathHasher::default();
path_hasher.append(&field.field_id().to_le_bytes()[..]);
if let tantivy::schema::Value::JsonObject(json_obj) = value {
if let TantivyValue::Object(json_obj) = value {
let is_expand_dots_enabled: bool =
if let FieldType::JsonObject(json_options) = field_entry.field_type() {
json_options.is_expand_dots_enabled()
Expand Down Expand Up @@ -600,7 +649,7 @@ mod tests {
use quickwit_common::PathHasher;
use quickwit_query::query_ast::query_ast_from_user_text;
use serde_json::{self, json, Value as JsonValue};
use tantivy::schema::{FieldType, IndexRecordOption, Type, Value as TantivyValue};
use tantivy::schema::{FieldType, IndexRecordOption, OwnedValue as TantivyValue, Type, Value};

use super::DefaultDocMapper;
use crate::default_doc_mapper::field_mapping_entry::DEFAULT_TOKENIZER_NAME;
Expand Down Expand Up @@ -675,11 +724,14 @@ mod tests {
for field_value in document.field_values() {
let field_name = schema.get_field_name(field_value.field());
if field_name == SOURCE_FIELD_NAME {
assert_eq!(field_value.value().as_json(), json_doc.as_object());
assert_eq!(
tantivy::schema::OwnedValue::from(field_value.value().as_value()),
tantivy::schema::OwnedValue::from(json_doc.as_object().unwrap().clone())
);
} else if field_name == DYNAMIC_FIELD_NAME {
assert_eq!(
field_value.value().as_json(),
json!({"response_date2": "2021-12-19T16:39:57+00:00"}).as_object()
serde_json::to_string(&field_value.value()).unwrap(),
r#"{"response_date2":"2021-12-19T16:39:57Z"}"#
);
} else if field_name == FIELD_PRESENCE_FIELD_NAME {
let field_presence_u64 = field_value.value().as_u64().unwrap();
Expand Down Expand Up @@ -1157,7 +1209,10 @@ mod tests {
document.field_values().iter().for_each(|field_value| {
let field_name = schema.get_field_name(field_value.field());
if field_name == SOURCE_FIELD_NAME {
assert_eq!(field_value.value().as_json(), json_doc_value.as_object());
assert_eq!(
tantivy::schema::OwnedValue::from(field_value.value().as_value()),
tantivy::schema::OwnedValue::from(json_doc_value.as_object().unwrap().clone())
);
} else if field_name == FIELD_PRESENCE_FIELD_NAME {
let field_value_hash = field_value.value().as_u64().unwrap();
field_presences.insert(field_value_hash);
Expand Down Expand Up @@ -1469,7 +1524,7 @@ mod tests {
.unwrap();
let vals: Vec<&TantivyValue> = doc.get_all(dynamic_field).collect();
assert_eq!(vals.len(), 1);
if let TantivyValue::JsonObject(json_val) = &vals[0] {
if let TantivyValue::Object(json_val) = &vals[0] {
assert_eq!(
serde_json::to_value(json_val).unwrap(),
json!({
Expand Down Expand Up @@ -1515,7 +1570,7 @@ mod tests {
.unwrap();
let vals: Vec<&TantivyValue> = doc.get_all(dynamic_field).collect();
assert_eq!(vals.len(), 1);
if let TantivyValue::JsonObject(json_val) = &vals[0] {
if let TantivyValue::Object(json_val) = &vals[0] {
assert_eq!(
serde_json::to_value(json_val).unwrap(),
serde_json::json!({
Expand Down Expand Up @@ -1561,7 +1616,7 @@ mod tests {
.unwrap();
let vals: Vec<&TantivyValue> = doc.get_all(json_field).collect();
assert_eq!(vals.len(), 1);
if let TantivyValue::JsonObject(json_val) = &vals[0] {
if let TantivyValue::Object(json_val) = &vals[0] {
assert_eq!(
serde_json::to_value(json_val).unwrap(),
serde_json::json!({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ use base64::prelude::{Engine, BASE64_STANDARD};
use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;
use tantivy::schema::{
IndexRecordOption, JsonObjectOptions, TextFieldIndexing, TextOptions, Type,
Value as TantivyValue,
IndexRecordOption, JsonObjectOptions, OwnedValue as TantivyValue, TextFieldIndexing,
TextOptions, Type,
};

use super::date_time_type::QuickwitDateTimeOptions;
Expand Down
Loading