Skip to content

Best way to get Lucene DocId from SearchResult? #395

@TheBekker

Description

@TheBekker

What is the best way to get the DocId from a SearchResult?

I'm trying to do POC on result highlighting using the "Lucene.Net.Highlighter" package, and from what i can see i need to pass the DocId to get the TokenStream. (Source: https://lucenenet.apache.org/docs/4.8.0-beta00016/api/highlighter/Lucene.Net.Search.Highlight.html)

But that does not seem to be available in the ISearchResult returned from the ISearcher when executing a query.

Heres an example of what i've done, for some context.
Where this of course fails, since result.Id is not the same as DocId, that GetAnyTokenStream is expecting.

var analyzer = new StandardAnalyzer(LuceneInfo.CurrentVersion);
var parser = new QueryParser(LuceneInfo.CurrentVersion, CustomExamineConstants.ModulesContentField, analyzer);
var query = parser.Parse(searchQuery);
var htmlFormatter = new SimpleHTMLFormatter();
var highlighter = new Highlighter(htmlFormatter, new QueryScorer(query));

var searchResults = booleanOperation.Execute();

foreach (var result in searchResults)
{
    result.Values.TryGetValue(CustomExamineConstants.ModulesContentField, out var text);
    int.TryParse(result.Id, out var id);

    var tokenStream = TokenSources.GetAnyTokenStream(searcher.GetSearchContext().GetSearcher().IndexSearcher.IndexReader, id, CustomExamineConstants.ModulesContentField, analyzer);
    var fragment = highlighter.GetBestTextFragments(tokenStream, text, mergeContiguousFragments: false, maxNumFragments: 10);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions