Skip to content

Instantly share code, notes, and snippets.

@mikecomstock
Created August 13, 2012 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikecomstock/3336841 to your computer and use it in GitHub Desktop.
Save mikecomstock/3336841 to your computer and use it in GitHub Desktop.
namespace Triptitude.Biz.Services
{
public class LuceneService
{
public void IndexDestinations()
{
var directoryInfo = new DirectoryInfo(LuceneDestinationsIndexPath);
var directory = FSDirectory.Open(directoryInfo);
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_29);
IndexWriter indexWriter = new IndexWriter(directory, analyzer, true, new IndexWriter.MaxFieldLength(100));
IEnumerable<IDestination> countries = new CountriesRepo().FindAll().ToList();
IEnumerable<IDestination> regions = new RegionsRepo().FindAll().ToList();
IEnumerable<IDestination> cities = new CitiesRepo().GetDataReaderForIndexing();
IEnumerable<IDestination> destinations = countries.Union(regions).Union(cities);
int i = 0;
foreach (var destination in destinations)
{
if (++i % 5000 == 0)
{
Console.Clear();
Console.WriteLine(i + destination.FullName);
}
Document doc = new Document();
doc.Add(new Field("fullName", destination.FullName, Field.Store.YES, Field.Index.ANALYZED));
doc.Add(new Field("id", destination.GeoNameID.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
indexWriter.AddDocument(doc);
}
indexWriter.Commit();
indexWriter.Optimize();
indexWriter.Close();
}
public IEnumerable<DestinationSearchResult> SearchDestinations(string term)
{
if (string.IsNullOrWhiteSpace(term)) yield break;
DirectoryInfo directoryInfo = new DirectoryInfo(LuceneDestinationsIndexPath);
FSDirectory directory = FSDirectory.Open(directoryInfo);
IndexSearcher searcher = new IndexSearcher(directory, true);
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_29);
TokenStream tokenStream = analyzer.TokenStream("fullName", new StringReader(term));
BooleanQuery query = new BooleanQuery();
Token token = tokenStream.Next();
while (token != null)
{
Term thisTerm = new Term("fullName", token.Term());
TermQuery termQuery = new TermQuery(thisTerm);
query.Add(termQuery, BooleanClause.Occur.SHOULD);
Query prefixQuery = new PrefixQuery(thisTerm);
query.Add(prefixQuery, BooleanClause.Occur.MUST);
token = tokenStream.Next();
}
TopScoreDocCollector collector = TopScoreDocCollector.create(10, true);
searcher.Search(query, collector);
ScoreDoc[] hits = collector.TopDocs().scoreDocs;
foreach (ScoreDoc scoreDoc in hits)
{
var document = searcher.Doc(scoreDoc.doc);
yield return new DestinationSearchResult
{
FullName = document.Get("fullName"),
GeoNameId = int.Parse(document.Get("id"))
};
}
}
public class DestinationSearchResult
{
public string FullName { get; set; }
public int GeoNameId { get; set; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment