Quick start

Using LIFTI to index .NET classes in-memory

The LIFTI API is really straightforward to use, a simple example would be:

// People is a class defined with properties Name and Remarks
IEnumerable<Person> people = LoadPeople(); 

// Create the index
var index = new FullTextIndex<Person>(); 

// Index the people against the text in their remarks data
index.Index(people, p => p.Remarks);

// Search for all the people with "great" and "boss" in their remarks (in any order)
foreach (Person person in index.Search("great boss"))
{
    Console.WriteLine(person.Name); 
}

By default the full text index will not handle word derivatives, e.g. searching for CALLING will not return items indexed against the word CALL or CALLED. LIFTI provides a stemming word splitter which enables this kind of searching - to enable it just change the full text index constructor to:

var index = new FullTextIndex<Person>()
{
    WordSplitter = new StemmingWordSplitter()
};

You can also use a richer querying syntax to search the index if you want, just change the index so it is using an instance of the LiftiQueryParser class:

var index = new FullTextIndex<Person>()
{
    WordSplitter = new StemmingWordSplitter(),
    QueryParser = new LiftiQueryParser()
};

Updatable indexes

The FullTextIndex class doesn’t allow items to be modified once they are index – it is essentially write-once-per-item. If you need to be able to index the same item again, or remove an item from the index, then you can use the UpdatableFullTextIndex class instead.

Using LIFTI to create a persisted full text index

The FullTextIndex and UpdatableFullTextIndex classes require fully re-populating every time your application starts. If you want your index to be persisted across application restarts, you can use the PersistedFullTextIndex class instead. In this case you will typically want to store the id of the related item in the index - following on from the previous examples, this might be an PersonId of type Int32:

// Create the index - this will automatically load any data that was previously indexed
var index = new PersistedFullTextIndex<int>(@"c:\MyIndexData\PeopleIndex.dat")
{
    WordSplitter = new StemmingWordSplitter()
}; 

// Index new or updated people
index.Index(people, p => p.PersonId, p => p.Remarks);

// Search for all the people with "great" and "boss" in their remarks (in any order)
foreach (Person person in index.Search("great boss"))
{
    Console.WriteLine(person.Name); 
}

Indexes aren’t restricted to storing integers as keys – you can quite easily store strings or other value types. This final example indexes the paths to documents against the text that they contain:

private PersistedFullTextIndex<string> index;

void Main()
{
    this.index = new PersistedFullTextIndex<string>(@"c:\MyIndexData\DocumentIndex.dat")
    {
        WordSplitter = new StemmingWordSplitter(),
        QueryParser = new LiftiQueryParser()
    };

    this.index.Index(Directory.GetFiles(@"c:\MyDocumentStore"), f => File.ReadAllText(f));

    Console.WriteLine("Searching for documents containing the words 'design' and 'document'");
    foreach (var fileName in this.index.Search("design & document"))
    {
        Console.WriteLine("Document file name:", fileName);
    }
}

Last edited Jun 9, 2011 at 9:55 AM by MikeGoatly, version 2

Comments

No comments yet.