/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.exoplatform.services.jcr.impl.core.query.lucene;
import java.io.IOException;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.FieldSelector;
import org.apache.lucene.document.FieldSelectorResult;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.Hits;
/**
* Wraps the lucene <code>Hits</code> object and adds a close method that allows to release
* resources after a query has been executed and the results have been read completely.
*/
public class QueryHits
{
/**
* The lucene hits we wrap.
*/
private final Hits hits;
/**
* The IndexReader in use by the lucene hits.
*/
private final IndexReader reader;
/**
* Number of results.
*/
private final int length;
/**
* Creates a new <code>QueryHits</code> instance wrapping <code>hits</code>.
*
* @param hits
* the lucene hits.
* @param reader
* the IndexReader in use by <code>hits</code>.
*/
public QueryHits(Hits hits, IndexReader reader)
{
this.hits = hits;
this.reader = reader;
this.length = hits.length();
}
/**
* Releases resources held by this hits instance.
*
* @throws IOException
* if an error occurs while releasing resources.
*/
public final void close() throws IOException
{
reader.close();
PerQueryCache.getInstance().dispose();
}
/**
* Returns the number of results.
*
* @return the number of results.
*/
public final int length()
{
return length;
}
/**
* Returns the <code>n</code><sup>th</sup> document in this QueryHits.
*
* @param n
* index.
* @return the <code>n</code><sup>th</sup> document in this QueryHits.
* @throws IOException
* if an error occurs while reading from the index.
*/
public final Document doc(int n) throws IOException
{
return hits.doc(n);
}
public String getFieldContent(int n, final String field) throws IOException
{
int id = hits.id(n);
FieldSelector fieldSelector = new FieldSelector()
{
public FieldSelectorResult accept(String fieldName)
{
if (fieldName.equals(field))
return FieldSelectorResult.LOAD_AND_BREAK;
return FieldSelectorResult.NO_LOAD;
}
};
Document doc = reader.document(id, fieldSelector);
if (doc == null)
throw new IOException("Document with id " + id + " not found");
return doc.get(field);
}
/**
* Returns the score for the <code>n</code><sup>th</sup> document in this QueryHits.
*
* @param n
* index.
* @return the score for the <code>n</code><sup>th</sup> document.
*/
public final float score(int n) throws IOException
{
return hits.score(n);
}
/**
* Returns the document number for the <code>n</code><sup>th</sup> document in this QueryHits.
*
* @param n
* index.
* @return the document number for the <code>n</code><sup>th</sup> document.
* @throws IOException
* if an error occurs.
*/
public final int id(int n) throws IOException
{
return hits.id(n);
}
}