pub trait Indexable {
// Required method
fn strings(&self) -> Vec<String>;
}
Expand description
To make a record indexable for Indicium Search, the Indexable
trait must
be implemented for it. The trait returns a Vec<String>
of all content that
is to be indexed.
To begin, we’ll implement the Indexable
trait for our struct
. The idea
is to return a String
for every field that we would like to be indexed.
Basic usage:
struct MyStruct {
title: String,
year: u16,
body: String,
}
impl Indexable for MyStruct {
fn strings(&self) -> Vec<String> {
vec![
self.title.clone(),
self.year.to_string(),
self.body.clone(),
]
}
}
-
Note: Don’t forget that you may make numbers, numeric identifiers, enums, and other types in your
struct
(or other complex types) indexable by converting them to aString
and including them in the returnedVec<String>
. -
Note: Any type that implements
ToString
(and consequently any type that implementsDisplay
) to, in turn, gets theIndexable
implementation for free. This includes Rust primitives likeString
,u8
,u16
,u32
,u64
,i8
,i16
,i32
,i64
and so on…
§Pro-Tip: Hash Tags
If you would like to categorize or classify your records, you could employ
hash tags. The purpose of the
hash tag is to prevent metadata or category name collisions with
user-space keywords. Of course, consider using another symbol if the hash
symbol (#
) is anticipated to be in the user-space.
You could prepend the user’s search query with hash tag(s). For example:
you could categorize cities as American by returning the #US
keyword for
them from your Indexable
trait. Then, if a user intends to search for
cities in United States, your application could quietly prepend the user’s
query with #US
to restrict the search to American cities.