001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.lucene.engine;
018:
019: import java.io.IOException;
020:
021: import org.apache.lucene.document.Document;
022: import org.apache.lucene.search.Explanation;
023: import org.apache.lucene.search.Hits;
024: import org.compass.core.Resource;
025: import org.compass.core.engine.SearchEngineException;
026: import org.compass.core.engine.SearchEngineHighlighter;
027: import org.compass.core.lucene.LuceneResource;
028:
029: /**
030: * @author kimchy
031: */
032:
033: public class DefaultLuceneSearchEngineHits implements
034: LuceneSearchEngineHits {
035:
036: private LuceneSearchEngine searchEngine;
037:
038: private LuceneSearchEngineQuery query;
039:
040: private LuceneSearchEngineInternalSearch internalSearch;
041:
042: private SearchEngineHighlighter highlighter;
043:
044: private Hits hits;
045:
046: public DefaultLuceneSearchEngineHits(Hits hits,
047: LuceneSearchEngine searchEngine,
048: LuceneSearchEngineQuery query,
049: LuceneSearchEngineInternalSearch internalSearch) {
050: this .hits = hits;
051: this .searchEngine = searchEngine;
052: this .query = query;
053: this .internalSearch = internalSearch;
054: }
055:
056: public Resource getResource(int i) throws SearchEngineException {
057: verifyWithinTransaction();
058: try {
059: Document doc = hits.doc(i);
060: return new LuceneResource(doc, hits.id(i), searchEngine
061: .getSearchEngineFactory());
062: } catch (IOException ioe) {
063: throw new SearchEngineException("Failed to find hit [" + i
064: + "]", ioe);
065: }
066: }
067:
068: public int getLength() {
069: return hits.length();
070: }
071:
072: public float score(int i) throws SearchEngineException {
073: verifyWithinTransaction();
074: try {
075: return hits.score(i);
076: } catch (IOException ioe) {
077: throw new SearchEngineException(
078: "Failed to fetch score for hit [" + i + "]", ioe);
079: }
080: }
081:
082: public Hits getHits() {
083: return this .hits;
084: }
085:
086: public SearchEngineHighlighter getHighlighter()
087: throws SearchEngineException {
088: verifyWithinTransaction();
089: if (highlighter == null) {
090: highlighter = new LuceneSearchEngineHighlighter(query
091: .getOriginalQuery(), internalSearch.getReader(),
092: searchEngine);
093: }
094: return highlighter.clear();
095: }
096:
097: public Explanation explain(int i) throws SearchEngineException {
098: verifyWithinTransaction();
099: try {
100: return internalSearch.getSearcher().explain(
101: query.getQuery(), hits.id(i));
102: } catch (IOException e) {
103: throw new SearchEngineException("Failed to explain hit ["
104: + i + "]", e);
105: }
106: }
107:
108: public void close() throws SearchEngineException {
109: if (internalSearch != null) {
110: internalSearch.close();
111: internalSearch = null;
112: }
113: }
114:
115: private void verifyWithinTransaction() throws SearchEngineException {
116: if (!searchEngine.isWithinTransaction()) {
117: throw new SearchEngineException(
118: "Accessing hits outside of a running transaction, either expand the "
119: + "transaction scope or detach the hits");
120: }
121: }
122: }
|