2 releases

new 0.1.1 Feb 12, 2025
0.1.0 Feb 12, 2025

#165 in Database implementations

Download history 195/week @ 2025-02-08

195 downloads per month

LGPL-3.0-or-later

61KB
1.5K SLoC

A full-text search engine.

use frits::{Index, Schema, Query, ResultSet};
use frits::storage::memory::InMemoryStorage;
use frits::analysis::UnicodeWordsTokenizer;
use frits::ranking::TfIdfScorer;
use frits::search::collect::TopNCollector;
use frits::search::queryparser::{QueryParser, QueryFieldOptions};

// Define our schema.
struct Doc {
    content: String,
}

let schema = Schema::<Doc>::builder()
    .add_field(|doc| &doc.content, UnicodeWordsTokenizer)  // field 0
    .build();

// Create the index.
let storage = InMemoryStorage::new(&schema);
let mut index = Index::new(schema, storage);

// Insert some documents.
let doc1 = index.insert(&Doc {
    content: "the quick brown fox jumps over the lazy dog".to_string(),
});
let doc2 = index.insert(&Doc {
    content: "the quick brown fox jumped over two lazy dogs".to_string(),
});
let doc3 = index.insert(&Doc {
    content: ("the quick brown fox jumps over the lazy dog
               the quick brown fox jumps over the lazy dog").to_string(),
});
let doc4 = index.insert(&Doc {
    content: "jackdaws love my big sphinx of quartz".to_string(),
});

// Do a search.
let qp = QueryParser::builder(index.schema())
    .add_field("content", QueryFieldOptions::new(0))
    .build();
let query = qp.parse("the quick");
assert_eq!(
    index
        .search(&query)
        .collect(&TopNCollector::new(1, TfIdfScorer, &query, &index))
        .into_iter()
        .map(|(id, _)| id)
        .collect::<Vec<_>>(),
    vec![doc3]
);

Dependencies

~1–18MB
~239K SLoC