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

Fulltext filter #4442

Merged
merged 7 commits into from
Mar 14, 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
3 changes: 2 additions & 1 deletion graph/src/components/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ pub enum EntityFilter {
NotEndsWithNoCase(Attribute, Value),
ChangeBlockGte(BlockNumber),
Child(Child),
Fulltext(Attribute, Value),
}

// A somewhat concise string representation of a filter
Expand All @@ -198,7 +199,7 @@ impl fmt::Display for EntityFilter {
Or(fs) => {
write!(f, "{}", fs.iter().map(|f| f.to_string()).join(" or "))
}
Equal(a, v) => write!(f, "{a} = {v}"),
Equal(a, v) | Fulltext(a, v) => write!(f, "{a} = {v}"),
Not(a, v) => write!(f, "{a} != {v}"),
GreaterThan(a, v) => write!(f, "{a} > {v}"),
LessThan(a, v) => write!(f, "{a} < {v}"),
Expand Down
5 changes: 5 additions & 0 deletions graphql/src/schema/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,11 @@ fn query_field_for_fulltext(fulltext: &Directive) -> Option<Field> {
},
// block: BlockHeight
block_argument(),
input_value(
"where",
"",
Type::NamedType(format!("{}_filter", entity_name)),
),
];

arguments.push(subgraph_error_argument());
Expand Down
23 changes: 15 additions & 8 deletions graphql/src/store/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,18 +174,25 @@ fn build_filter(
field: &a::Field,
schema: &ApiSchema,
) -> Result<Option<EntityFilter>, QueryExecutionError> {
match field.argument_value("where") {
let where_filter = match field.argument_value("where") {
Some(r::Value::Object(object)) => match build_filter_from_object(entity, object, schema) {
Ok(filter) => Ok(Some(EntityFilter::And(filter))),
Err(e) => Err(e),
},
Some(r::Value::Null) => Ok(None),
None => match field.argument_value("text") {
Some(r::Value::Object(filter)) => build_fulltext_filter_from_object(filter),
None => Ok(None),
_ => Err(QueryExecutionError::InvalidFilterError),
},
Some(r::Value::Null) | None => Ok(None),
_ => Err(QueryExecutionError::InvalidFilterError),
}?;

let text_filter = match field.argument_value("text") {
Some(r::Value::Object(filter)) => build_fulltext_filter_from_object(filter),
None => Ok(None),
_ => Err(QueryExecutionError::InvalidFilterError),
}?;

match (where_filter, text_filter) {
(None, None) => Ok(None),
(Some(f), None) | (None, Some(f)) => Ok(Some(f)),
(Some(w), Some(t)) => Ok(Some(EntityFilter::And(vec![t, w]))),
}
}

Expand All @@ -196,7 +203,7 @@ fn build_fulltext_filter_from_object(
Err(QueryExecutionError::FulltextQueryRequiresFilter),
|(key, value)| {
if let r::Value::String(s) = value {
Ok(Some(EntityFilter::Equal(
Ok(Some(EntityFilter::Fulltext(
key.to_string(),
Value::String(s.clone()),
)))
Expand Down
65 changes: 65 additions & 0 deletions graphql/tests/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,22 @@ async fn setup(

fn test_schema(id: DeploymentHash, id_type: IdType) -> Schema {
const SCHEMA: &str = "

type _Schema_
@fulltext(
name: \"bandReviewSearch\"
language: en
algorithm: proximityRank
include: [
{
entity: \"BandReview\"
fields: [
{ name: \"body\" }
]
}
]
)

type Musician @entity {
id: ID!
name: String!
Expand Down Expand Up @@ -687,6 +703,55 @@ fn can_query_many_to_many_relationship() {
})
}

#[test]
fn can_query_with_fulltext_search() {
const QUERY: &str = "
query {
bandReviewSearch(text: \"musicians\") {
id
body
author {
name
}
}
}";

run_query(QUERY, |result, _| {
let exp = object! {
bandReviewSearch: vec![
object! { id: "r1", body: "Bad musicians", author: object! { name: "Baden" } },
object! { id: "r5", body: "Very Bad musicians", author: object! { name: "Anonymous 3" } },
]
};
let data = extract_data!(result).unwrap();
assert_eq!(data, exp);
})
}

#[test]
fn can_query_with_fulltext_search_filter() {
const QUERY: &str = "
query {
bandReviewSearch(text: \"musicians\", where: { author_: { name: \"Anonymous 3\" } }) {
id
body
author {
name
}
}
}";

run_query(QUERY, |result, _| {
let exp = object! {
bandReviewSearch: vec![
object! { id: "r5", body: "Very Bad musicians", author: object! { name: "Anonymous 3" } },
]
};
let data = extract_data!(result).unwrap();
assert_eq!(data, exp);
})
}

#[test]
fn can_query_with_sorting_by_child_entity() {
const QUERY: &str = "
Expand Down
35 changes: 26 additions & 9 deletions store/postgres/src/relational_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,7 @@ impl<'a> QueryFilter<'a> {
| NotContains(attr, _)
| NotContainsNoCase(attr, _)
| Equal(attr, _)
| Fulltext(attr, _)
| Not(attr, _)
| GreaterThan(attr, _)
| LessThan(attr, _)
Expand Down Expand Up @@ -1404,7 +1405,9 @@ impl<'a> QueryFragment<Pg> for QueryFilter<'a> {
NotContains(attr, value) => self.contains(attr, value, true, true, out)?,
NotContainsNoCase(attr, value) => self.contains(attr, value, true, false, out)?,

Equal(attr, value) => self.equals(attr, value, c::Equal, out)?,
Equal(attr, value) | Fulltext(attr, value) => {
self.equals(attr, value, c::Equal, out)?
}
Not(attr, value) => self.equals(attr, value, c::NotEqual, out)?,

GreaterThan(attr, value) => self.compare(attr, value, c::Greater, out)?,
Expand Down Expand Up @@ -2784,6 +2787,20 @@ impl<'a> SortKey<'a> {
block: BlockNumber,
layout: &'a Layout,
) -> Result<Self, QueryExecutionError> {
fn sort_key_from_value<'a>(
column: &'a Column,
value: &'a Value,
direction: &'static str,
) -> Result<SortKey<'a>, QueryExecutionError> {
let sort_value = value.as_str();

Ok(SortKey::Key {
column,
value: sort_value,
direction,
})
}

fn with_key<'a>(
table: &'a Table,
attribute: String,
Expand All @@ -2794,15 +2811,15 @@ impl<'a> SortKey<'a> {
let column = table.column_for_field(&attribute)?;
if column.is_fulltext() {
match filter {
Some(EntityFilter::Equal(_, value)) => {
let sort_value = value.as_str();

Ok(SortKey::Key {
column,
value: sort_value,
direction,
})
Some(EntityFilter::Fulltext(_, value)) => {
sort_key_from_value(column, value, direction)
}
Some(EntityFilter::And(vec)) => match vec.first() {
Some(EntityFilter::Fulltext(_, value)) => {
sort_key_from_value(column, value, direction)
}
_ => unreachable!(),
},
_ => unreachable!(),
}
} else if column.is_primary_key() {
Expand Down