/*******************************************************************************
* Copyright 2009, 2010 Innovation Gate GmbH. All Rights Reserved.
*
* This file is part of the OpenWGA server platform.
*
* OpenWGA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition, a special exception is granted by the copyright holders
* of OpenWGA called "OpenWGA plugin exception". You should have received
* a copy of this exception along with OpenWGA in file COPYING.
* If not, see <http://www.openwga.com/gpl-plugin-exception>.
*
* OpenWGA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenWGA in file COPYING.
* If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package de.innovationgate.wgpublisher.lucene;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import de.innovationgate.utils.WGUtils;
import de.innovationgate.webgate.api.WGContent;
import de.innovationgate.webgate.api.WGContentKey;
import de.innovationgate.webgate.api.WGDatabase;
import de.innovationgate.webgate.api.WGException;
import de.innovationgate.webgate.api.WGResultSet;
import de.innovationgate.wgpublisher.WGACore;
public abstract class LuceneResultSet implements WGResultSet {
protected TopDocs _hits;
protected WGACore _core;
protected HttpServletRequest _request;
protected long _executionTime;
protected Query _query;
boolean _enhance;
boolean _explain = false;
public LuceneResultSet(TopDocs hits, WGACore core, HttpServletRequest request, Map parameters, Query query, long executionTime) {
_hits = hits;
_core = core;
_request = request;
_executionTime = executionTime;
_enhance = !parameters.containsKey(WGDatabase.QUERYOPTION_ENHANCE) || parameters.get(WGDatabase.QUERYOPTION_ENHANCE).equals(new Boolean(true));
if (parameters.containsKey(WGDatabase.QUERYOPTION_NATIVEOPTIONS)) {
String nativeOptionsStr = (String) parameters.get(WGDatabase.QUERYOPTION_NATIVEOPTIONS);
if (nativeOptionsStr != null) {
Iterator nativeOptions = WGUtils.deserializeCollection(nativeOptionsStr, ",", true).iterator();
while (nativeOptions.hasNext()) {
String option = (String) nativeOptions.next();
if (option.equalsIgnoreCase("explain")) {
_explain = true;
}
}
}
}
}
/**
* @param doc
* @return
* @throws IOException
* @throws CorruptIndexException
* @throws InterruptedException
* @throws WGAccessException
*/
protected WGContent luceneDocToContent(ScoreDoc scoreDoc, float scMax) throws CorruptIndexException, IOException, InterruptedException {
try {
Document doc = _core.getLuceneManager().getDocument(scoreDoc.doc);
String contentKey = doc.get(LuceneManager.INDEXFIELD_CONTENTKEY);
String dbKey = doc.get(LuceneManager.INDEXFIELD_DBKEY);
WGDatabase db = _core.openContentDB(dbKey, _request);
if (db.isSessionOpen() == false) {
return null;
}
float score = 0;
if (scMax != 0 && !Float.isNaN(scMax) && !Float.isNaN(scoreDoc.score)) {
score = scoreDoc.score/scMax;
}
WGContent content = db.getContentByKey(WGContentKey.parse(contentKey, db));
if (content != null && (!_enhance || content.isVisibleNow())) {
content.setSearchScore(score);
if (_explain) {
try {
content.setSearchExplanation(_core.getLuceneManager().explain(_query, scoreDoc.doc));
} catch (Exception e) {
}
}
return content;
}
else {
return null;
}
}
catch (WGException e) {
return null;
}
}
public long getExecutionTime() {
return _executionTime;
}
public void close() {
// TODO Auto-generated method stub
}
}