Constructing an in-memory full text index

The code to create a new full text index class looks like this:

var index = new FullTextIndex<MyType>(); 
var updatableIndex = new UpdatableFullTextIndex<MyType>();

From this you can see that all forms of the full text index class are generic types defining the type of item that the index will contain.

There are two approaches that you can take to indexing items: indexing keys and indexing .NET objects.

Indexing keys

The first approach is to index a unique key for your items. This means that the index itself will only store a reference or id to the indexed item. This might be an integer, a string, a Guid or other primitive type.
The constructor for this approach looks like this (Note that the FullTextIndex generic type is int):

var index = new FullTextIndex<int>()

The main benefit to this approach is that once an item has been indexed the associated text doesn’t need to be retained in memory.

Indexing .NET objects

If your items are always loaded into memory and they contain all the text that you want to index, you can index them directly:

var index = new FullTextIndex<Customer>();

Here the generic type of FullTextIndex is Customer.

Obviously the drawback to this approach is that all the instances are retained in memory, along with all their associated properties, including the text.

Constructing a persisted full text index

Full text indexes can be created by passing the path to the persisted file into the constructor:

var indexFilePath = Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
    Path.Combine("MyApplication", "MyTextIndex.dat");

var index = new PersistedFullTextIndex<int>(indexFilePath);

As with the FullTextIndex and UpdatableFullTextIndex, the persisted full index is a generic type. It is strongly recommended that you use a key-based approach to the index, by restricting the type that you store in the index to a primitive type, e.g. int, string, etc.

If you do need to store some other type in the index, such as a Guid or custom class, then this blog post should get you started.

Last edited May 27, 2013 at 10:11 AM by MikeGoatly, version 2

Comments

No comments yet.