Searching Dictionaries
Full-text search lets you find entries by matching against their definitions, not just their headwords. Unlike lookup which requires an exact term match, search uses a Tantivy-powered full-text index.
Creating an index
Section titled “Creating an index”Before you can search, you need to create a full-text index. This only needs to be done once per dictionary (the index is persisted to disk).
use odict::{OpenDictionary, index::IndexOptions};
fn main() -> odict::Result<()> {let file = OpenDictionary::from_path("my-dictionary.odict")?;let dict = file.contents()?;
dict.index(IndexOptions::default())?;
Ok(())
}from theopendictionary import OpenDictionary
dictionary = await OpenDictionary.load("./my-dictionary.odict")dictionary.index()import { OpenDictionary } from "@odict/node";
const dictionary = await OpenDictionary.load("./my-dictionary.odict");dictionary.index();Index options
Section titled “Index options”You can configure the indexing behavior.
use odict::index::IndexOptions;
let options = IndexOptions::default() .dir("./my-index") // Custom index directory .overwrite(true) // Overwrite existing index .memory(50_000_000); // 50MB memory arena per thread
dict.index(options)?;from theopendictionary import IndexOptions
dictionary.index(IndexOptions(directory="./my-index", # Custom index directoryoverwrite=True, # Overwrite existing indexmemory=50_000_000 # 50MB memory arena per thread))dictionary.index({ directory: "./my-index", // Custom index directory overwrite: true, // Overwrite existing index memory: 50_000_000, // 50MB memory arena per thread});Running a search
Section titled “Running a search”Once indexed, you can search across all definitions in the dictionary.
use odict::search::SearchOptions;
let results = dict.search("domesticated mammal", SearchOptions::default())?;
for entry in &results {println!("{}", entry.term);}results = dictionary.search("domesticated mammal")
for entry in results: print(entry.term)const results = dictionary.search("domesticated mammal");
for (const entry of results) {console.log(entry.term);}Search options
Section titled “Search options”use odict::search::SearchOptions;
let options = SearchOptions::default() .dir("./my-index") // Custom index directory .autoindex(true) // Auto-create index if missing .limit(10) // Max results to return .threshold(50); // Relevance threshold
let results = dict.search("greeting", options)?;from theopendictionary import SearchOptions
results = dictionary.search("greeting", SearchOptions(directory="./my-index", # Custom index directoryautoindex=True, # Auto-create index if missinglimit=10, # Max results to returnthreshold=50 # Relevance threshold))const results = dictionary.search("greeting", { directory: "./my-index", // Custom index directory autoindex: true, // Auto-create index if missing limit: 10, // Max results to return threshold: 50, // Relevance threshold});Search vs. lookup
Section titled “Search vs. lookup”| Lookup | Search | |
|---|---|---|
| Matches against | Entry terms (headwords) | Definition text |
| Requires index | No | Yes |
| Speed | O(1) per term | Depends on index size |
| Use case | You know the exact word | You’re searching by meaning |
| Supports splitting | Yes | No |
| Supports follow | Yes | No |
In most applications you’ll use both: lookup for direct dictionary access, and search for discovery.