rustio_core/search/traits.rs
1//! The `Searchable` trait. Models implement this to opt into the
2//! search index. The trait is deliberately tiny: we want to serialise
3//! into a Meilisearch document, name the index, and declare which
4//! fields are filterable/sortable.
5
6use serde_json::Value;
7
8pub trait Searchable {
9 /// Index name — typically the same as the admin name ("posts").
10 const INDEX_NAME: &'static str;
11
12 /// Primary key field in the serialised document. Meilisearch
13 /// requires one unique key per doc; for us it's almost always "id".
14 const PRIMARY_KEY: &'static str = "id";
15
16 /// Which attributes the engine should actually tokenise for
17 /// full-text queries. Fewer is faster; only include what users
18 /// would actually type to find this model.
19 const SEARCHABLE_ATTRIBUTES: &'static [&'static str];
20
21 /// Fields available for `filter=…` queries (e.g. `published=true`).
22 const FILTERABLE_ATTRIBUTES: &'static [&'static str] = &[];
23
24 /// Fields available for `sort=…`.
25 const SORTABLE_ATTRIBUTES: &'static [&'static str] = &[];
26
27 /// Subset of filterable attributes to request facet counts for.
28 /// A UI with a "by author" sidebar wants author here; a raw
29 /// `created_at` timestamp that's filterable-but-not-faceted does not.
30 const FACETABLE_ATTRIBUTES: &'static [&'static str] = &[];
31
32 /// Serialise `self` into the JSON document shape Meili indexes.
33 fn to_search_doc(&self) -> Value;
34}