public
Created

  • Download Gist
LuceneService.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
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; }
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.