From ae3c8af56cbd938666b4a3b4bb23db871c5c38c5 Mon Sep 17 00:00:00 2001 From: mpostma Date: Thu, 4 Mar 2021 10:40:37 +0100 Subject: [PATCH] enable faceted search --- .../actor_index_controller/index_actor.rs | 76 +++++++++++++++---- 1 file changed, 61 insertions(+), 15 deletions(-) diff --git a/src/index_controller/actor_index_controller/index_actor.rs b/src/index_controller/actor_index_controller/index_actor.rs index 04920260e..b45b683bf 100644 --- a/src/index_controller/actor_index_controller/index_actor.rs +++ b/src/index_controller/actor_index_controller/index_actor.rs @@ -1,23 +1,24 @@ +use std::collections::{HashMap, hash_map::Entry}; use std::fs::{File, create_dir_all}; use std::path::{PathBuf, Path}; use std::sync::Arc; use std::time::Instant; +use anyhow::bail; +use either::Either; +use async_stream::stream; use chrono::Utc; -use heed::EnvOpenOptions; -use milli::Index; -use std::collections::HashMap; -use std::collections::hash_map::Entry; +use futures::stream::StreamExt; +use heed::{EnvOpenOptions, RoTxn}; +use log::info; +use milli::{FacetCondition, Index}; +use serde_json::Value; use thiserror::Error; use tokio::sync::{mpsc, oneshot, RwLock}; use uuid::Uuid; -use log::info; -use crate::data::SearchQuery; -use futures::stream::StreamExt; use super::update_handler::UpdateHandler; -use async_stream::stream; -use crate::data::SearchResult; +use crate::data::{SearchQuery, SearchResult}; use crate::index_controller::{IndexMetadata, UpdateMeta, updates::{Processed, Failed, Processing}, UpdateResult as UResult}; use crate::option::IndexerOpts; @@ -99,7 +100,7 @@ impl IndexActor { } async fn handle_update(&self, meta: Processing, data: File, ret: oneshot::Sender) { - info!("processing update {}", meta.id()); + info!("Processing update {}", meta.id()); let uuid = meta.index_uuid().clone(); let index = self.store.get_or_create(uuid).await.unwrap(); let update_handler = self.update_handler.clone(); @@ -122,11 +123,12 @@ fn perform_search(index: &Index, query: SearchQuery) -> anyhow::Result anyhow::Result, +) -> anyhow::Result> { + let mut ands = Vec::new(); + for value in arr { + match value { + Value::String(s) => ands.push(Either::Right(s.clone())), + Value::Array(arr) => { + let mut ors = Vec::new(); + for value in arr { + match value { + Value::String(s) => ors.push(s.clone()), + v => bail!("Invalid facet expression, expected String, found: {:?}", v), + } + } + ands.push(Either::Left(ors)); + } + v => bail!( + "Invalid facet expression, expected String or [String], found: {:?}", + v + ), + } + } + + FacetCondition::from_array(txn, index, ands) +} + +fn parse_facets( + facets: &Value, + index: &Index, + txn: &RoTxn, +) -> anyhow::Result> { + match facets { + // Disabled for now + //Value::String(expr) => Ok(Some(FacetCondition::from_str(txn, index, expr)?)), + Value::Array(arr) => parse_facets_array(txn, index, arr), + v => bail!( + "Invalid facet expression, expected Array, found: {:?}", + v + ), + } +} #[derive(Clone)] pub struct IndexActorHandle { sender: mpsc::Sender,