ObjectArrayList<ResultItem> resultItems = new ObjectArrayList<ResultItem>();
if ( ! results.isEmpty() ) {
SelectedInterval[] selectedInterval = null;
final DocumentCollection collection = documentCollection != null ? documentCollection.copy() : null;
for( int i = 0; i < results.size(); i++ ) {
DocumentScoreInfo<Reference2ObjectMap<Index,SelectedInterval[]>> dsi = results.get( i );
LOGGER.debug( "Intervals for item " + i );
final ResultItem resultItem = new ResultItem( dsi.document, dsi.score );
resultItems.add( resultItem );
if ( collection != null ) {
final Document document = collection.document( dsi.document );
// If both collection and title list are present, we override the collection title (cfr. Query)
resultItem.title = StringEscapeUtils.escapeHtml( titleList != null ? titleList.get( resultItem.doc ).toString() : document.title().toString() );
if ( useUri ) {
if ( document.uri() != null ) resultItem.uri = StringEscapeUtils.escapeHtml( document.uri().toString() );
}
else {
if ( document.uri() != null ) {
String stringUri = document.uri().toString();
// TODO: this is a quick patch to get the file server running with relative files
final String documentUri = URLEncoder.encode( derelativise
? new File( stringUri.startsWith( "file:" ) ? stringUri.substring( 5 ) : stringUri ).getAbsoluteFile().toURI().toASCIIString()
: document.uri().toString(), "UTF-8" );
resultItem.uri = StringEscapeUtils.escapeHtml( "./Item?doc=" + resultItem.doc + "&m=" + urlEncodedMimeType + "&uri=" + documentUri );
}
else resultItem.uri = StringEscapeUtils.escapeHtml( "./Item?doc=" + resultItem.doc + "&m=" + urlEncodedMimeType );
}
MarkingMutableString snippet = new MarkingMutableString( TextMarker.HTML_STRONG, MarkingMutableString.HTML_ESCAPE );
for( int j = 0; j < sortedIndex.length; j++ ) {
if ( ! sortedIndex[ j ].hasPositions || dsi.info == null ) continue;
selectedInterval = dsi.info.get( sortedIndex[ j ] );
if ( selectedInterval != null ) {
final int field = documentCollection.factory().fieldIndex( sortedIndex[ j ].field );
// If the field is not present (e.g., because of parallel indexing) or it is not text we skip
if ( field == -1 || documentCollection.factory().fieldType( field ) != DocumentFactory.FieldType.TEXT ) continue;
LOGGER.debug( "Found intervals for " + sortedIndex[ j ].field + " (" + field + ")" );
final Reader content = (Reader)document.content( field );
snippet.startField( selectedInterval ).appendAndMark( document.wordReader( field ).setReader( content ) ).endField();
}
if ( LOGGER.isDebugEnabled() ) LOGGER.debug( sortedIndex[ j ].field + ": " + ( selectedInterval == null ? null : Arrays.asList( selectedInterval ) ) );
document.close();
}
resultItem.text = snippet;
}
else {
if ( titleList != null ) {
// TODO: this is a bit radical
resultItem.title = resultItem.uri = titleList.get( resultItem.doc );
}
else {
resultItem.title = "Document #" + resultItem.doc;
resultItem.uri = new MutableString( "./Item?doc=" ).append( resultItem.doc ).append( "&m=" ).append( urlEncodedMimeType );
}
MutableString text = new MutableString();
for( Iterator<Index> j = indexMap.values().iterator(); j.hasNext(); ) {
final Index index = j.next();
selectedInterval = dsi.info.get( index );
if ( selectedInterval != null )
text.append( "<p>" ).append( index.field ).append( ": " ).append( Arrays.asList( selectedInterval ) );
LOGGER.debug( index.field + ": " + ( selectedInterval == null ? null : Arrays.asList( selectedInterval ) ) );
}
resultItem.text = text;
}
}
if ( collection != null ) collection.close();
}
// Note that if we pass an array to the template we lose the possibility of measuring its length.
context.put( "result", resultItems );