Crate frits

Crate frits 

Source
Expand description

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]
);

Re-exports§

pub use index::Index;
pub use schema::Schema;
pub use search::Query;
pub use search::ResultSet;

Modules§

analysis
Analyze phrases to split into tokens.
index
Indexes store documents.
ranking
Ways to rank results.
schema
Schemas define how documents are indexed.
search
Search queries and collectors.
storage
How documents are stored.