Source Code Cross Referenced for LuceneHelper.java in  » Search-Engine » compass-2.0 » org » compass » core » lucene » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Search Engine » compass 2.0 » org.compass.core.lucene.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.