Created
November 26, 2014 23:34
-
-
Save lars-erik/3c7f7f879faa4b828e27 to your computer and use it in GitHub Desktop.
Highlighting Lucene Wildcard Queries
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
using System; | |
using Lucene.Net.Analysis.Snowball; | |
using Lucene.Net.Documents; | |
using Lucene.Net.Highlight; | |
using Lucene.Net.Index; | |
using Lucene.Net.Search; | |
using Lucene.Net.Store; | |
using NUnit.Framework; | |
namespace Lucene.Experiments.Tests | |
{ | |
[TestFixture] | |
public class Highlighting | |
{ | |
[Test] | |
public void With_WildcardQuery() | |
{ | |
const string text = "Here's text and something"; | |
const string fieldName = "z"; | |
var wildcardQuery = new WildcardQuery(new Term(fieldName, "some*")); | |
Query query = wildcardQuery; | |
var analyzer = new SnowballAnalyzer("English"); | |
var dir = new RAMDirectory(); | |
var writer = new IndexWriter(dir, analyzer, IndexWriter.MaxFieldLength.UNLIMITED); | |
var doc = new Document(); | |
doc.Add(new Field(fieldName, text, Field.Store.NO, Field.Index.ANALYZED)); | |
writer.AddDocument(doc); | |
writer.Flush(true, true, true); | |
var reader = writer.GetReader(); | |
// Prove query for fun | |
Console.WriteLine(query); | |
var result = new IndexSearcher(reader).Search(query, null, 99); | |
Assert.AreEqual(1, result.TotalHits); | |
// The one in 2.9.4.1 that does the matching term lookus | |
wildcardQuery.SetRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE); | |
query = wildcardQuery.Rewrite(reader); | |
// Prove rewritten query with 'someth' from index | |
Console.WriteLine(query); | |
result = new IndexSearcher(reader).Search(query, null, 99); | |
Assert.AreEqual(1, result.TotalHits); | |
// And here's the magic | |
var highlighter = new Highlighter(new SimpleHTMLFormatter("<span>", "</span>"), new QueryScorer(query, reader, fieldName)); | |
var bestFragment = highlighter.GetBestFragment(analyzer, fieldName, text); | |
Assert.AreEqual("Here's text and <span>something</span>", bestFragment); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment