Introduce a new facet filters query field

This commit is contained in:
Kerollmops 2021-01-07 10:15:31 +01:00
parent afa86d8a45
commit 33945a3115
No known key found for this signature in database
GPG key ID: 92ADA4E935E71FA4
5 changed files with 407 additions and 321 deletions

View file

@ -11,6 +11,7 @@ use std::{mem, io};
use askama_warp::Template;
use byte_unit::Byte;
use either::Either;
use flate2::read::GzDecoder;
use futures::stream;
use futures::{FutureExt, StreamExt};
@ -620,12 +621,29 @@ async fn main() -> anyhow::Result<()> {
.body(include_str!("../public/logo-black.svg"))
);
#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum UntaggedEither<L, R> {
Left(L),
Right(R),
}
impl<L, R> From<UntaggedEither<L, R>> for Either<L, R> {
fn from(value: UntaggedEither<L, R>) -> Either<L, R> {
match value {
UntaggedEither::Left(left) => Either::Left(left),
UntaggedEither::Right(right) => Either::Right(right),
}
}
}
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
#[serde(rename_all = "camelCase")]
struct QueryBody {
query: Option<String>,
facet_condition: Option<String>,
filters: Option<String>,
facet_filters: Option<Vec<UntaggedEither<Vec<String>, String>>>,
facet_distribution: Option<bool>,
}
@ -651,11 +669,32 @@ async fn main() -> anyhow::Result<()> {
if let Some(query) = query.query {
search.query(query);
}
if let Some(condition) = query.facet_condition {
if !condition.trim().is_empty() {
let condition = FacetCondition::from_str(&rtxn, &index, &condition).unwrap();
search.facet_condition(condition);
}
let filters = match query.filters {
Some(condition) if !condition.trim().is_empty() => {
Some(FacetCondition::from_str(&rtxn, &index, &condition).unwrap())
},
_otherwise => None,
};
let facet_filters = match query.facet_filters {
Some(array) => {
let eithers = array.into_iter().map(Into::into);
FacetCondition::from_array(&rtxn, &index, eithers).unwrap()
},
_otherwise => None,
};
let condition = match (filters, facet_filters) {
(Some(filters), Some(facet_filters)) => {
Some(FacetCondition::And(Box::new(filters), Box::new(facet_filters)))
},
(Some(condition), None) | (None, Some(condition)) => Some(condition),
_otherwise => None,
};
if let Some(condition) = condition {
search.facet_condition(condition);
}
let SearchResult { found_words, candidates, documents_ids } = search.execute().unwrap();