Search code examples
lucenefragmentlucene-highlighter

How to allow only one find per document searched on Lucene


I only want my Lucene search to give the highest scoring highlighted fragment per document. So say I have 5 documents with the word "performance" on each one three times, I still only want 5 results to be printed and highlighted to the results page. How can I go about doing that? Thanks!


Solution

  • You get only one fragment per document returned from the search by calling getBestFragment, rather than getBestFragments.

    If your call to search is returning the same documents more than once, you very likely have more than one copy of the same document in your index. Make sure that if you intend to create a new index, you open your IndexWriter with it's OpenMode set to: IndexWriterConfig.OpenMode.CREATE.