Skip to content

Commit

Permalink
[GRAPHDB-57] implemented Highlight Support to Lucene Index
Browse files Browse the repository at this point in the history
  • Loading branch information
sunlounger committed Nov 22, 2011
1 parent 21e2005 commit 38eb25a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
11 changes: 10 additions & 1 deletion Plugins/Index/Implementations/SonesLuceneIndex/LuceneEntry.cs
Expand Up @@ -52,6 +52,15 @@ public string IndexId
/// Gets or sets the property id.
/// </summary>
public long? PropertyId
{
get;
set;
}

/// <summary>
/// Gets or sets the highlights.
/// </summary>
public string[] Highlights
{
get;
set;
Expand All @@ -60,7 +69,7 @@ public string IndexId
#endregion

/// <summary>
/// Gets or sets the score. (Set only by Solr.Net)
/// Gets or sets the score.
/// </summary>
public double? Score
{
Expand Down
35 changes: 29 additions & 6 deletions Plugins/Index/Implementations/SonesLuceneIndex/LuceneReturn.cs
Expand Up @@ -2,7 +2,11 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lucene.Net;
using Lucene.Net.Search;
using Lucene.Net.Analysis;
using Lucene.Net.Index;
using Lucene.Net.Search.Vectorhighlight;

namespace sones.Plugins.Index.LuceneIdx
{
Expand All @@ -12,12 +16,18 @@ public class LuceneReturnEnumerator : IEnumerator<LuceneEntry>
TopDocs _docs;
int _pos = -1;
int _doccount = 0;
FastVectorHighlighter _highlighter;
Analyzer _analyzer;
Query _query;

public LuceneReturnEnumerator(TopDocs myDocuments, IndexSearcher myIndexSearcher)
public LuceneReturnEnumerator(TopDocs myDocuments, IndexSearcher myIndexSearcher, FastVectorHighlighter myHighlighter, Analyzer myAnalyzer, Query myQuery)
{
_IndexSearcher = myIndexSearcher;
_docs = myDocuments;
_doccount = _docs.scoreDocs.Count();
_doccount = _docs.ScoreDocs.Count();
_highlighter = myHighlighter;
_analyzer = myAnalyzer;
_query = myQuery;
}

public LuceneEntry Current
Expand All @@ -26,8 +36,13 @@ public LuceneEntry Current
{
if (_pos < 0) MoveNext();

var docnum = _docs.scoreDocs.ElementAt(_pos).doc;
return new LuceneEntry(_IndexSearcher.Doc(docnum));
var docnum = _docs.ScoreDocs[_pos].doc;
var entry = new LuceneEntry(_IndexSearcher.Doc(docnum));

FieldQuery fieldquery = _highlighter.GetFieldQuery(_query);
entry.Highlights = _highlighter.GetBestFragments(fieldquery, _IndexSearcher.GetIndexReader(), docnum, LuceneIndex.FieldNames[LuceneIndex.Fields.TEXT], 100, 3);

return entry;
}
}

Expand Down Expand Up @@ -65,13 +80,21 @@ public class LuceneReturn : IEnumerable<LuceneEntry>
private TopScoreDocCollector _Collector;
private bool bOpen = false;
TopDocs _docs;
FastVectorHighlighter _highlighter;
Analyzer _analyzer;
Query _query;

public LuceneReturn(TopScoreDocCollector myCollector, IndexSearcher myIndexSearcher)
public LuceneReturn(TopScoreDocCollector myCollector, IndexSearcher myIndexSearcher, Query myQuery, Analyzer myAnalyzer)
{
_Collector = myCollector;
_IndexSearcher = myIndexSearcher;
_docs = _Collector.TopDocs();
bOpen = true;

_highlighter = new FastVectorHighlighter();
_analyzer = myAnalyzer;
_query = myQuery;

}

public IEnumerator<LuceneEntry> GetEnumerator()
Expand All @@ -81,7 +104,7 @@ public IEnumerator<LuceneEntry> GetEnumerator()
throw new InvalidOperationException("This LuceneReturn Enumerator has already been closed!");
}

return new LuceneReturnEnumerator(_docs, _IndexSearcher);
return new LuceneReturnEnumerator(_docs, _IndexSearcher, _highlighter, _analyzer, _query);
}

public void Close()
Expand Down

0 comments on commit 38eb25a

Please sign in to comment.