caio.co/de/cantine


Use tique::QueryParser 💬 by Caio 6 years ago (log)
Now only `QueryParser` is public, not the mod.

Blob cantine/src/main.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
use std::{convert::TryFrom, env, io, path::Path, str::FromStr, sync::Arc};

use env_logger;
use serde::Serialize;
use tique::QueryParser;
use uuid::Uuid;

use actix_web::{
    http::StatusCode, middleware::Logger, web, App, HttpResponse, HttpServer, Result as ActixResult,
};

use tantivy::{
    query::{AllQuery, BooleanQuery, Occur, Query},
    Index, IndexReader, Result,
};

use cantine::{
    database::DatabaseReader,
    index::{After, RecipeIndex},
    model::{
        FeaturesAggregationQuery, FeaturesAggregationResult, Recipe, RecipeCard, RecipeId,
        RecipeInfo, SearchCursor, SearchQuery, SearchResult, Sort,
    },
};

type RecipeDatabase = Arc<DatabaseReader<Recipe>>;

pub async fn recipe(
    database: web::Data<RecipeDatabase>,
    uuid: web::Path<Uuid>,
) -> ActixResult<HttpResponse> {
    if let Some(recipe) = database
        .find_by_uuid(&uuid)
        .transpose()
        .expect("db operational")
    {
        Ok(HttpResponse::Ok().json(RecipeInfo::from(recipe)))
    } else {
        Ok(HttpResponse::new(StatusCode::NOT_FOUND))
    }
}

#[derive(Serialize, Clone)]
pub struct IndexInfo {
    pub total_recipes: u64,
    pub features: FeaturesAggregationResult,
    pub sort: Vec<Sort>,
}

pub async fn index_info(info: web::Data<IndexInfo>) -> ActixResult<HttpResponse> {
    Ok(HttpResponse::Ok().json(info.get_ref()))
}

fn cursor_to_after(database: &RecipeDatabase, cursor: &SearchCursor) -> Option<After> {
    database
        .id_for_uuid(&Uuid::from_bytes(*cursor.uuid()))
        .map(|id| match &cursor {
            SearchCursor::Relevance(score, _) => After::Relevance(*score, *id),
            SearchCursor::U64Field(score, _) => After::U64Field(*score, *id),
            SearchCursor::F64Field(score, _) => After::F64Field(*score, *id),
        })
}

pub async fn search(
    query: web::Json<SearchQuery>,
    state: web::Data<Arc<SearchState>>,
    database: web::Data<RecipeDatabase>,
) -> ActixResult<HttpResponse> {
    let after = if let Some(cursor) = &query.after {
        let checked_after = cursor_to_after(&database, &cursor);
        if checked_after.is_none() {
            return Ok(HttpResponse::new(StatusCode::BAD_REQUEST));
        }
        checked_after
    } else {
        None
    };

    let (total_found, recipe_ids, after, agg) =
        web::block(move || -> Result<ExecuteResult> { state.search(query.0, after) }).await?;

    let num_results = recipe_ids.len();
    let mut items = Vec::with_capacity(num_results);
    for recipe_id in recipe_ids {
        let recipe: Recipe = database
            .find_by_id(recipe_id)
            .expect("item in the index always present in the db")?;
        items.push(RecipeCard::from(recipe));
    }

    let next = after.map(|after| {
        let last_uuid = &items[num_results - 1].uuid;

        match after {
            After::Relevance(score, _) => SearchCursor::Relevance(score, *last_uuid.as_bytes()),
            After::U64Field(score, _) => SearchCursor::U64Field(score, *last_uuid.as_bytes()),
            After::F64Field(score, _) => SearchCursor::F64Field(score, *last_uuid.as_bytes()),
        }
    });

    Ok(HttpResponse::Ok().json(SearchResult {
        total_found,
        items,
        next,
        agg,
    }))
}

type ExecuteResult = (
    usize,
    Vec<RecipeId>,
    Option<After>,
    Option<FeaturesAggregationResult>,
);

pub struct SearchState {
    reader: IndexReader,
    recipe_index: RecipeIndex,
    query_parser: QueryParser,
    agg_threshold: usize,
}

impl SearchState {
    pub fn search(&self, query: SearchQuery, after: Option<After>) -> Result<ExecuteResult> {
        let limit = query.num_items.unwrap_or(10) as usize;

        let searcher = self.reader.searcher();
        let interpreted_query = self.interpret_query(&query)?;

        let (total_found, recipe_ids, after) = self.recipe_index.search(
            &searcher,
            &interpreted_query,
            limit,
            query.sort.unwrap_or(Sort::Relevance),
            after,
        )?;

        let agg = if total_found <= self.agg_threshold {
            query
                .agg
                .map(|agg_query| {
                    self.recipe_index
                        .aggregate_features(&searcher, &interpreted_query, agg_query)
                })
                .transpose()?
        } else {
            None
        };

        Ok((total_found, recipe_ids, after, agg))
    }

    fn interpret_query(&self, query: &SearchQuery) -> Result<Box<dyn Query>> {
        let mut subqueries: Vec<(Occur, Box<dyn Query>)> = Vec::new();

        if let Some(fulltext) = &query.fulltext {
            if let Some(parsed) = self.query_parser.parse_dixmax(fulltext.as_str(), 0.1) {
                subqueries.push((Occur::Must, parsed));
            }
        }

        if let Some(filter) = &query.filter {
            for query in self.recipe_index.features.interpret(filter).into_iter() {
                subqueries.push((Occur::Must, query));
            }
        }

        match subqueries.len() {
            0 => Ok(Box::new(AllQuery)),
            1 => Ok(subqueries.pop().expect("length has been checked").1),
            _ => Ok(Box::new(BooleanQuery::from(subqueries))),
        }
    }

    pub fn index_info(&self) -> Result<IndexInfo> {
        let searcher = self.reader.searcher();
        let features = self.recipe_index.aggregate_features(
            &searcher,
            &AllQuery,
            FeaturesAggregationQuery::full_range(),
        )?;

        let sort = Sort::VALUES.to_vec();

        Ok(IndexInfo {
            total_recipes: searcher.num_docs(),
            features,
            sort,
        })
    }
}

const BASE_DIR: &str = "BASE_DIR";
const AGG_THRESHOLD: &str = "AGG_THRESHOLD";

fn get_env(key: &str) -> Result<String> {
    env::var(key).map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, key).into())
}

#[actix_rt::main]
async fn main() -> Result<()> {
    env_logger::init();

    let base_dir = get_env(BASE_DIR)?;
    let threshold = get_env(AGG_THRESHOLD)
        .ok()
        .map(|v| usize::from_str(&v).expect("valid usize"));

    log::info!(
        "Starting with base_dir={} agg_threshold={:?}",
        base_dir,
        threshold
    );

    let base_path = Path::new(&base_dir);
    let index_path = base_path.join("tantivy");
    let db_path = base_path.join("database");

    let index = Index::open_in_dir(&index_path)?;
    let recipe_index = RecipeIndex::try_from(&index.schema())?;
    let mut query_parser = QueryParser::new(
        &index,
        vec![
            recipe_index.name,
            recipe_index.ingredients,
            recipe_index.instructions,
        ],
    )?;

    // XXX This is as scientific as "4" is random
    // Reduce importance of instructions match
    query_parser.set_boost(recipe_index.instructions, Some(0.7));
    // And make name matches slightly more important than ingredient
    query_parser.set_boost(recipe_index.name, Some(1.15));

    let reader = index.reader()?;
    let search_state = Arc::new(SearchState {
        reader,
        recipe_index,
        query_parser,
        agg_threshold: threshold.unwrap_or(std::usize::MAX),
    });

    let database: RecipeDatabase = Arc::new(DatabaseReader::open(&db_path)?);

    let info = search_state.index_info()?;

    HttpServer::new(move || {
        App::new()
            .wrap(Logger::default())
            .app_data(web::Data::new(search_state.clone()))
            .app_data(web::Data::new(database.clone()))
            .app_data(web::Data::new(info.clone()))
            .data(web::JsonConfig::default().limit(4096))
            .service(web::resource("/recipe/{uuid}").route(web::get().to(recipe)))
            .service(web::resource("/search").route(web::post().to(search)))
            .service(web::resource("/info").route(web::get().to(index_info)))
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await?;

    Ok(())
}