blob: 48199509e784c18819cb7e45d6fa8f19ba1f3164 [file] [log] [blame]
<section id="quick-start" class="home-section">
<div class="container">
<div class="row">
<div class="col-xs-12 col-md-6">
<p class="text-center">Create an index and define a text analyzer</p>
<pre class="clean">
<code class="csharp">// Ensures index backwards compatibility
var AppLuceneVersion = LuceneVersion.LUCENE_48;
var indexLocation = @"C:\Index";
var dir = FSDirectory.Open(indexLocation);
//create an analyzer to process the text
var analyzer = new StandardAnalyzer(AppLuceneVersion);
//create an index writer
var indexConfig = new IndexWriterConfig(AppLuceneVersion, analyzer);
var writer = new IndexWriter(dir, indexConfig);
</code>
</pre>
</div>
<div class="col-xs-12 col-md-6">
<p class="text-center">Add to the index</p>
<pre class="clean">
<code class="csharp">var source = new
{
Name = "Kermit the Frog",
FavoritePhrase = "The quick brown fox jumps over the lazy dog"
};
Document doc = new Document
{
// StringField indexes but doesn't tokenize
new StringField("name",
source.Name,
Field.Store.YES),
new TextField("favoritePhrase",
source.FavoritePhrase,
Field.Store.YES)
};
writer.AddDocument(doc);
writer.Flush(triggerMerge: false, applyAllDeletes: false);
</code>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-md-6">
<p class="text-center">Construct a query</p>
<pre class="clean">
<code class="csharp">// search with a phrase
var phrase = new MultiPhraseQuery
{
new Term("favoritePhrase", "brown"),
new Term("favoritePhrase", "fox")
};
</code>
</pre>
</div>
<div class="col-xs-12 col-md-6">
<p class="text-center">Fetch the results</p>
<pre class="clean">
<code class="csharp">// re-use the writer to get real-time updates
var searcher = new IndexSearcher(writer.GetReader(applyAllDeletes: true));
var hits = searcher.Search(phrase, 20 /* top 20 */).ScoreDocs;
foreach (var hit in hits)
{
&nbsp;&nbsp;&nbsp;&nbsp;var foundDoc = searcher.Doc(hit.Doc);
&nbsp;&nbsp;&nbsp;&nbsp;hit.Score.Dump("Score");
&nbsp;&nbsp;&nbsp;&nbsp;foundDoc.Get("name").Dump("Name");
&nbsp;&nbsp;&nbsp;&nbsp;foundDoc.Get("favoritePhrase").Dump("Favorite Phrase");
}
</code>
</pre>
</div>
</div>
</div>
</section>