Created
January 18, 2013 10:18
-
-
Save jsm85/4563634 to your computer and use it in GitHub Desktop.
Blog - Searching with Lucene.Net - Building the Index and defining the Index Document
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Index | |
{ | |
public static void BuildIndex() | |
{ | |
var directory = FSDirectory.Open(new DirectoryInfo("C:\\temp\\test\\")); | |
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30); | |
var indexWriter = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED); | |
foreach (var movie in MovieDataExtractor.Execute(@" SELECT | |
[Position], | |
[Title], | |
[Director], | |
[Year], | |
[Country], | |
[RunTime], | |
[Genre], | |
[Colour] | |
FROM | |
[Movie].[dbo].[GreatestMovie]")) | |
indexWriter.AddDocument(movie.CreateIndexDocument()); | |
indexWriter.Optimize(); | |
indexWriter.Dispose(); | |
} | |
} | |
public class GreatestMovie | |
{ | |
public int Position { get; set; } | |
public string Director { get; set; } | |
public string Title { get; set; } | |
public string Country { get; set; } | |
public int Year { get; set; } | |
public int RunTime { get; set; } | |
public string Genre { get; set; } | |
public string Colour { get; set; } | |
public Document CreateIndexDocument() | |
{ | |
var document = new Document(); | |
document.Add(new Field("Director", Director, Field.Store.NO, Field.Index.ANALYZED)); | |
document.Add(new Field("Colour", Colour, Field.Store.NO, Field.Index.ANALYZED)); | |
document.Add(new Field("Title", Title, Field.Store.YES, Field.Index.ANALYZED)); | |
document.Add(new Field("Country", Country, Field.Store.NO, Field.Index.ANALYZED)); | |
document.Add(new Field("Genre", Country, Field.Store.YES, Field.Index.ANALYZED)); | |
document.Add(new Field("Position", Position.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); | |
document.Add(new Field("Year", Year.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); | |
return document; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment