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.util;
018:
019: import java.io.IOException;
020:
021: import org.apache.lucene.document.Document;
022: import org.apache.lucene.index.TermFreqVector;
023: import org.apache.lucene.search.Filter;
024: import org.apache.lucene.search.Query;
025: import org.apache.lucene.store.Directory;
026: import org.compass.core.Compass;
027: import org.compass.core.CompassHits;
028: import org.compass.core.CompassQuery;
029: import org.compass.core.CompassQueryFilter;
030: import org.compass.core.CompassSession;
031: import org.compass.core.Resource;
032: import org.compass.core.engine.SearchEngineException;
033: import org.compass.core.engine.SearchEngineQuery;
034: import org.compass.core.engine.SearchEngineQueryFilter;
035: import org.compass.core.impl.DefaultCompassHits;
036: import org.compass.core.impl.DefaultCompassQuery;
037: import org.compass.core.impl.DefaultCompassQueryFilter;
038: import org.compass.core.lucene.LuceneResource;
039: import org.compass.core.lucene.engine.LuceneSearchEngine;
040: import org.compass.core.lucene.engine.LuceneSearchEngineFactory;
041: import org.compass.core.lucene.engine.LuceneSearchEngineHits;
042: import org.compass.core.lucene.engine.LuceneSearchEngineInternalSearch;
043: import org.compass.core.lucene.engine.LuceneSearchEngineQuery;
044: import org.compass.core.lucene.engine.LuceneSearchEngineQueryFilter;
045: import org.compass.core.lucene.engine.analyzer.LuceneAnalyzerManager;
046: import org.compass.core.lucene.engine.manager.LuceneSearchEngineIndexManager;
047: import org.compass.core.spi.InternalCompass;
048: import org.compass.core.spi.InternalCompassSession;
049: import org.compass.core.spi.InternalResource;
050:
051: /**
052: * Allows to create Compass related objects based on external (internally no supported by Compass)
053: * Lucene objects.
054: *
055: * @author kimchy
056: */
057: public abstract class LuceneHelper {
058:
059: /**
060: * Creates a new {@link CompassQuery} based on a Lucene {@link Query}.
061: * <p/>
062: * Allows to create {@link CompassQuery} based on external Lucene {@link Query} that is not supported
063: * by one of Compass query builders.
064: *
065: * @param session Comapss session
066: * @param query The lucene query to wrap
067: * @return A compass query wrapping the lucene query
068: */
069: public static CompassQuery createCompassQuery(
070: CompassSession session, Query query) {
071: InternalCompassSession internalCompassSession = (InternalCompassSession) session;
072: SearchEngineQuery searchEngineQuery = new LuceneSearchEngineQuery(
073: (LuceneSearchEngine) internalCompassSession
074: .getSearchEngine(), query);
075: return new DefaultCompassQuery(searchEngineQuery,
076: internalCompassSession);
077: }
078:
079: /**
080: * Returns the underlying {@link LuceneSearchEngineQuery} of the given {@link CompassQuery}.
081: * <p/>
082: * Can be used for example to add custom Sorting using
083: * {@link LuceneSearchEngineQuery#addSort(org.apache.lucene.search.SortField)}, or get the actual lucene query
084: * using {@link org.compass.core.lucene.engine.LuceneSearchEngineQuery#getQuery()}.
085: *
086: * @param query The compass query to extract the lucene search engine query from
087: * @return The lucene search engine query extracted from the compass query
088: */
089: public static LuceneSearchEngineQuery getLuceneSearchEngineQuery(
090: CompassQuery query) {
091: return (LuceneSearchEngineQuery) ((DefaultCompassQuery) query)
092: .getSearchEngineQuery();
093: }
094:
095: /**
096: * Creates a new {@link CompassQueryFilter} based on a Lucene {@link Filter}.
097: * <p/>
098: * Allows to create {@link CompassQueryFilter} based on external Lucene {@link Filter} that is not supported
099: * by one fo Comapss query filter builders.
100: *
101: * @param session Comapss session
102: * @param filter The lucene filter to wrap
103: * @return A compass query filter wrapping lucene query.
104: */
105: public static CompassQueryFilter createCompassQueryFilter(
106: CompassSession session, Filter filter) {
107: SearchEngineQueryFilter searchEngineQueryFilter = new LuceneSearchEngineQueryFilter(
108: filter);
109: return new DefaultCompassQueryFilter(searchEngineQueryFilter);
110: }
111:
112: /**
113: * Returns the underlying {@link LuceneSearchEngineQueryFilter} of the given {@link CompassQueryFilter}.
114: * <p/>
115: * Can be used to get the actual Lucene {@link Filter} using
116: * {@link org.compass.core.lucene.engine.LuceneSearchEngineQueryFilter#getFilter()}.
117: *
118: * @param filter The compass query filter to extract the lucene search engine query filter from
119: * @return The lucene search engine query filter extracted from the compass query filter
120: */
121: public static LuceneSearchEngineQueryFilter getLuceneSearchEngineQueryFilter(
122: CompassQueryFilter filter) {
123: return (LuceneSearchEngineQueryFilter) ((DefaultCompassQueryFilter) filter)
124: .getFilter();
125: }
126:
127: /**
128: * Returns the underlying {@link LuceneSearchEngineHits} of the given {@link CompassHits}.
129: * <p/>
130: * Used mainly to access the actual Lucene {@link org.apache.lucene.search.Hits}, or get
131: * Lucene {@link org.apache.lucene.search.Explanation}.
132: */
133: public static LuceneSearchEngineHits getLuceneSearchEngineHits(
134: CompassHits hits) {
135: return (LuceneSearchEngineHits) ((DefaultCompassHits) hits)
136: .getSearchEngineHits();
137: }
138:
139: /**
140: * Returns Compass own internal <code>LuceneAnalyzerManager</code>. Can be used
141: * to access Lucene {@link org.apache.lucene.analysis.Analyzer} at runtime.
142: */
143: public static LuceneAnalyzerManager getLuceneAnalyzerManager(
144: Compass compass) {
145: return ((LuceneSearchEngineFactory) ((InternalCompass) compass)
146: .getSearchEngineFactory()).getAnalyzerManager();
147: }
148:
149: /**
150: * Returns the given search engine "internals" used for search. For Lucene, returns
151: * {@link LuceneSearchEngineInternalSearch} which allows to access Lucene
152: * {@link org.apache.lucene.index.IndexReader} and {@link org.apache.lucene.search.Searcher}.
153: * <p/>
154: * The search intenrals will be ones that are executed against the whole index. In order to search on
155: * specific aliases or sub indexes, please use {@link #getLuceneInternalSearch(org.compass.core.CompassSession,String[],String[])} .
156: *
157: * @param session A compass session within a transaction
158: * @return Lucene search "internals"
159: */
160: public static LuceneSearchEngineInternalSearch getLuceneInternalSearch(
161: CompassSession session) {
162: return (LuceneSearchEngineInternalSearch) ((InternalCompassSession) session)
163: .getSearchEngine().internalSearch(null, null);
164: }
165:
166: /**
167: * Returns the given search engine "internals" used for search. For Lucene, returns
168: * {@link LuceneSearchEngineInternalSearch} which allows to access Lucene
169: * {@link org.apache.lucene.index.IndexReader} and {@link org.apache.lucene.search.Searcher}.
170: * <p/>
171: * The search can be narrowed down to specific sub indexes or aliases. A <code>null</code> value
172: * means all the sub indexes/aliases.
173: *
174: * @param session A compass sessino within a transaction
175: * @param subIndexes A set of sub indexes to narrow down the index scope
176: * @param aliases A set of aliases to narrow down the index scope
177: * @return Lucene search "internals"
178: */
179: public static LuceneSearchEngineInternalSearch getLuceneInternalSearch(
180: CompassSession session, String[] subIndexes,
181: String[] aliases) {
182: return (LuceneSearchEngineInternalSearch) ((InternalCompassSession) session)
183: .getSearchEngine().internalSearch(subIndexes, aliases);
184: }
185:
186: /**
187: * Returns the actual Lucene {@link Document} that the {@link Resource} wraps.
188: *
189: * @param resource The resource to get the document from
190: * @return The Lucene document that resource wraps
191: */
192: public static Document getDocument(Resource resource) {
193: return ((LuceneResource) resource).getDocument();
194: }
195:
196: /**
197: * Returns Lucene {@link TermFreqVector} using the given Compass session and {@link Resource}.
198: *
199: * @param session Compass session
200: * @param resource The resource to get the term freq vector for
201: * @return The term infos freq vector for the given resource
202: * @throws SearchEngineException
203: */
204: public static TermFreqVector[] getTermFreqVectors(
205: CompassSession session, Resource resource)
206: throws SearchEngineException {
207: String subIndex = ((InternalResource) resource).getSubIndex();
208: LuceneSearchEngineInternalSearch internalSearch = getLuceneInternalSearch(
209: session, new String[] { subIndex }, null);
210: try {
211: return internalSearch.getReader().getTermFreqVectors(
212: ((LuceneResource) resource).getDocNum());
213: } catch (IOException e) {
214: throw new SearchEngineException(
215: "Failed to fetch term info for resource ["
216: + resource + "]", e);
217: }
218: }
219:
220: /**
221: * Returns Lucene {@link TermFreqVector} for the given property and resource, using the session.
222: *
223: * @param session Compass session
224: * @param resource The resource to get the term freq vector for
225: * @param propertyName Theh property name (Lucene field name) to get the term freq vector for
226: * @return Teh term info freq vector for the given resource and property
227: * @throws SearchEngineException
228: */
229: public static TermFreqVector getTermFreqVector(
230: CompassSession session, Resource resource,
231: String propertyName) throws SearchEngineException {
232: String subIndex = ((InternalResource) resource).getSubIndex();
233: LuceneSearchEngineInternalSearch internalSearch = getLuceneInternalSearch(
234: session, new String[] { subIndex }, null);
235: try {
236: return internalSearch.getReader().getTermFreqVector(
237: ((LuceneResource) resource).getDocNum(),
238: propertyName);
239: } catch (IOException e) {
240: throw new SearchEngineException(
241: "Failed to fetch term info for resource ["
242: + resource + "]", e);
243: }
244: }
245:
246: /**
247: * Returns the lucene {@link org.apache.lucene.store.Directory} associated with the given sub index.
248: */
249: public static Directory getDirectory(Compass compass,
250: String subIndex) {
251: return ((LuceneSearchEngineIndexManager) ((InternalCompass) compass)
252: .getSearchEngineIndexManager()).getStore()
253: .openDirectory(subIndex);
254: }
255: }
|