5555 * this renderer. This class is not thread-safe.
5656 * </p>
5757 */
58- public class HtmlSnippets implements SnippetsConsumer
58+ public class ResultsSnippets implements SnippetsConsumer
5959{
6060 private final String contentFieldName ;
6161 private final Detagger detagger = new Detagger (Set .of ("i" , "em" ));
@@ -93,7 +93,7 @@ public class HtmlSnippets implements SnippetsConsumer
9393 * @throws IllegalArgumentException if {@code snipLimit < 0}
9494 * @throws NullPointerException if any non-primitive argument is {@code null}
9595 */
96- public HtmlSnippets (
96+ public ResultsSnippets (
9797 final Writer writer ,
9898 final StoredFields storedFields ,
9999 final String contentFieldName ,
@@ -106,12 +106,14 @@ public HtmlSnippets(
106106 this .contentFieldName = Objects .requireNonNull (contentFieldName , "contentFieldName" );
107107 this .rail = Objects .requireNonNull (rail , "rail" );
108108 this .termWeights = Objects .requireNonNull (termWeights , "termWeights" );
109- if (snipLimit < 0 ) {
110- throw new IllegalArgumentException ("snipLimit=" + snipLimit + ", expected >= 0" );
111- }
112109 this .snipLimit = snipLimit ;
113110 this .termDedup = new int [termWeights .length ];
114- this .topSnips = new TopArray (snipLimit );
111+ if (snipLimit > 0 ) {
112+ this .topSnips = new TopArray (snipLimit );
113+ }
114+ else {
115+ this .topSnips = null ; // should be OK for docSnippets()
116+ }
115117 }
116118
117119 /**
@@ -131,7 +133,7 @@ public int ctx()
131133 * @param ctx context width in words
132134 * @return this instance
133135 */
134- public HtmlSnippets ctx (final int ctx )
136+ public ResultsSnippets ctx (final int ctx )
135137 {
136138 this .ctx = ctx ;
137139 return this ;
@@ -169,7 +171,7 @@ public String doclineFieldName()
169171 * @param doclineFieldName stored-field name, or {@code null}
170172 * @return this instance
171173 */
172- public HtmlSnippets doclineFieldName (final String doclineFieldName )
174+ public ResultsSnippets doclineFieldName (final String doclineFieldName )
173175 {
174176 this .doclineFieldName = doclineFieldName ;
175177 return this ;
@@ -183,28 +185,29 @@ public HtmlSnippets doclineFieldName(final String doclineFieldName)
183185 * @param docId Lucene document id
184186 * @throws IOException if the writer or stored-fields access fails
185187 */
186- public void docOpen (final int docId ) throws IOException
188+ public void docOpen (final int docId , String css ) throws IOException
187189 {
190+ if (css == null || css .isBlank ()) {css ="" ; }
191+ else { css = " " + css ;};
188192 doc = storedFields .document (docId );
189193 id = doc .get (ALIX_ID );
190194 writer .append ("<article" )
191195 .append (" id=\" " ).append (id ).append ("\" " )
192196 .append (" data-docid=\" " ).append (String .valueOf (docId )).append ("\" " )
193- .append (" class=\" result\" " )
197+ .append (" class=\" result" ). append ( css ). append ( " \" " )
194198 .append (">\n " );
195199
196200 String url = hrefBase + id + hrefExt + hrefSearch ;
197201 if (doclineFieldName != null ) {
198202 final String docline = doc .get (doclineFieldName );
199203 if (docline != null ) {
200204 writer .append ("<h4" )
201- .append (" data-href =\" " ). append ( url ). append ( " \" " )
205+ .append (" class =\" result-title \" " )
202206 .append (">\n " )
203- .append ("<span>" ).append (docline ).append ("</span>\n " )
204207 .append ("<a" )
205208 .append (" href=\" " ).append (url ).append ("\" " )
206- .append (" class= \" result-open \" " )
207- .append (">→ </a>\n " )
209+ .append (">" ). append ( docline )
210+ .append ("</a>\n " )
208211 .append ("</h4>\n " );
209212 }
210213 }
@@ -227,10 +230,27 @@ public void docOpen(final int docId) throws IOException
227230 @ Override
228231 public void docSnippets (final int docId , final Snippets snippets ) throws IOException
229232 {
230- docOpen (docId );
231- content = doc .get (contentFieldName );
232233 final int snipCount = snippets .snips4doc ();
233- if (snipLimit > 0 && content != null && snipCount > 0 ) {
234+ docOpen (docId , "hassnippets" );
235+ content = doc .get (contentFieldName );
236+ if (content == null ) {
237+ writer .append ("<!-- No text stored for field: '" + contentFieldName + "' -->" );
238+ }
239+ else if (snipCount <= 0 ) {
240+ writer .append ("<!-- No snippets found -->" );
241+ }
242+ else if (snipLimit < 0 ) {
243+ // list all snippets in document order
244+ writer .append ("<ol class=\" snippets\" >\n " );
245+ for (int snipOrd = 0 ; snipOrd < snipCount ; snipOrd ++) {
246+ print (snippets , snipOrd );
247+ }
248+ writer .append ("</ol>\n " );
249+ }
250+ else if (snipLimit == 0 ) {
251+
252+ }
253+ else {
234254 topSnips .clear ();
235255 for (int snipOrd = 0 ; snipOrd < snipCount ; snipOrd ++) {
236256 final int startPos = Math .max (0 , snippets .snipStartPosition (snipOrd ) - ctx );
@@ -262,7 +282,7 @@ public String hrefBase()
262282 * @param hrefBase URL prefix
263283 * @return this instance
264284 */
265- public HtmlSnippets hrefBase (final String hrefBase )
285+ public ResultsSnippets hrefBase (final String hrefBase )
266286 {
267287 this .hrefBase = hrefBase ;
268288 return this ;
@@ -284,7 +304,7 @@ public String hrefExt()
284304 * @param hrefExt URL suffix
285305 * @return this instance
286306 */
287- public HtmlSnippets hrefExt (final String hrefExt )
307+ public ResultsSnippets hrefExt (final String hrefExt )
288308 {
289309 this .hrefExt = hrefExt ;
290310 return this ;
@@ -306,7 +326,7 @@ public String hrefSearch()
306326 * @param hrefSearch query-string fragment
307327 * @return this instance
308328 */
309- public HtmlSnippets hrefSearch (final String hrefSearch )
329+ public ResultsSnippets hrefSearch (final String hrefSearch )
310330 {
311331 this .hrefSearch = hrefSearch ;
312332 return this ;
@@ -345,7 +365,8 @@ private void print(final Snippets snippets, final int snipOrd) throws IOExceptio
345365 final int leftMatchStartOffset = snippets .matchStartOffset (leftMatchOrd );
346366 final int rightMatchEndOffset = snippets .matchEndOffset (rightMatchOrd );
347367
348- final String url = hrefBase + id + hrefExt + hrefSearch + "#snippet" + snipOrd ;
368+ final int snipAnchor = snipOrd + 1 ;
369+ final String url = hrefBase + id + hrefExt + hrefSearch + "#snippet-" + snipAnchor ;
349370 writer
350371 .append ("<li" )
351372 .append (" class=\" snippet\" " )
@@ -380,7 +401,7 @@ private void print(final Snippets snippets, final int snipOrd) throws IOExceptio
380401 .append ("\n <a" )
381402 .append (" class=\" snippet-open\" " )
382403 .append (" href=\" " ).append (url ).append ("\" " )
383- .append ("\" >→</a>" );
404+ .append (">→</a>" );
384405 writer .append ("</li>\n " );
385406 }
386407
0 commit comments