Ninja Database Pro
Creating Full Text Indexes
Basic Tasks > Creating Full Text Indexes

A full text index is a non-unique index on a string property.  Creating a full text index gives the ability to search for keywords, search for phrases, and search by regular expressions.  To create a full text index, call the AddFullTextIndex.  To search see these methods: SearchPhrase, SearchAllKeywords, SearchAnyKeywords, SearchByRegex

 

NinjaDbPro db = new NinjaDbPro("FullTextIndexExampleDir", "FullTextIndexExampleDir.db");

 

//Licensed Mode

//db.UserName = "John Smith 101224";

//db.LicenseKey = "aousdf832jasf==";

//Set before OpenDatabase. Default storage is IsolatedStorageDatabase. Other options are:

//db.Storage = new MemoryDatabase(); //In memory database

//db.Storage = new FileDatabase(); //Valid only for non Silverlight projects

 

//Open the database

db.OpenDatabase();

 

//Add a full text index if it doesn't already exist

if (!db.IndexExists<Quote>("QuoteTextIndex"))

db.AddFullTextIndex<Quote>("QuoteTextIndex", "QuoteText");

 

//Add a couple record to the database

Quote quote = new Quote();

quote.Author = "John";

quote.QuoteText = "The quick brown fox jumps over the lazy dog.";

db.Save(quote);

 

//Add a couple record to the database

quote = new Quote();

quote.Author = "Jane";

quote.QuoteText = "The rain in spain falls mainly on the plain.";

db.Save(quote);

 

//Find a quote by a phrase

var phraseQuery = db.SearchPhrase<Quote>("QuoteTextIndex", "brown fox", true);

 

//Lazy load the first record and write out a property

Console.WriteLine(phraseQuery[0].LazyValue.Author);

 

//Find a quote by any keywords

var anyQuery = db.SearchAnyKeywords<Quote>("QuoteTextIndex", "fox spain", true);

 

//Lazy load the second record and write out a property

Console.WriteLine(anyQuery[1].LazyValue.Author);

 

//Find a quote by all keywords

var allQuery = db.SearchAllKeywords<Quote>("QuoteTextIndex", "jumps dog", true);

 

//Lazy load the second record and write out a property

Console.WriteLine(allQuery[0].LazyValue.Author);

 

//Find a quote by Regex

Regex searchRegex = new Regex(@"brown\sfox");

var regexQuery = db.SearchByRegex<Quote>("QuoteTextIndex", searchRegex);

 

//Lazy load the second record and write out a property

Console.WriteLine(regexQuery[0].LazyValue.Author);

 

//Close the database

db.CloseDatabase();