Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best way to get Lucene DocId from SearchResult? #395

Open
TheBekker opened this issue Nov 7, 2024 · 0 comments
Open

Best way to get Lucene DocId from SearchResult? #395

TheBekker opened this issue Nov 7, 2024 · 0 comments

Comments

@TheBekker
Copy link

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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant