Author: epbernard
Date: 2008-05-05 11:20:22 -0400 (Mon, 05 May 2008)
New Revision: 14642
Modified:
search/trunk/src/java/org/hibernate/search/SearchFactory.java
search/trunk/src/java/org/hibernate/search/impl/InitContext.java
search/trunk/src/java/org/hibernate/search/impl/SearchFactoryImpl.java
search/trunk/src/test/org/hibernate/search/test/embedded/doubleinsert/Contact.java
Log:
HSEARCH-195 return analyzer instances by name
Modified: search/trunk/src/java/org/hibernate/search/SearchFactory.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/SearchFactory.java 2008-05-03 20:11:12 UTC
(rev 14641)
+++ search/trunk/src/java/org/hibernate/search/SearchFactory.java 2008-05-05 15:20:22 UTC
(rev 14642)
@@ -1,8 +1,11 @@
-//$Id: $
+//$Id$
package org.hibernate.search;
+import java.util.Map;
+
import org.hibernate.search.reader.ReaderProvider;
import org.hibernate.search.store.DirectoryProvider;
+import org.apache.lucene.analysis.Analyzer;
/**
* Provide application wide operations as well as access to the underlying Lucene
resources.
@@ -33,4 +36,12 @@
* Optimize the index holding <code>entityType</code>
*/
void optimize(Class entityType);
+
+ /**
+ * Experimental API
+ * retrieve an analyzer instance by its definition name
+ *
+ * @throws SearchException if the definition name is unknown
+ */
+ Analyzer getAnalyzer(String name);
}
Modified: search/trunk/src/java/org/hibernate/search/impl/InitContext.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/impl/InitContext.java 2008-05-03 20:11:12
UTC (rev 14641)
+++ search/trunk/src/java/org/hibernate/search/impl/InitContext.java 2008-05-05 15:20:22
UTC (rev 14642)
@@ -134,26 +134,34 @@
return defaultSimilarity;
}
- public void initLazyAnalyzers() {
- Map<String, Analyzer> initializedAnalizers = new HashMap<String, Analyzer>(
analyzerDefs.size() );
+ public Map<String, Analyzer> initLazyAnalyzers() {
+ Map<String, Analyzer> initializedAnalyzers = new HashMap<String, Analyzer>(
analyzerDefs.size() );
for (DelegateNamedAnalyzer namedAnalyzer : lazyAnalyzers) {
String name = namedAnalyzer.getName();
- if ( initializedAnalizers.containsKey( name ) ) {
- namedAnalyzer.setDelegate( initializedAnalizers.get( name ) );
+ if ( initializedAnalyzers.containsKey( name ) ) {
+ namedAnalyzer.setDelegate( initializedAnalyzers.get( name ) );
}
else {
if ( analyzerDefs.containsKey( name ) ) {
final Analyzer analyzer = buildAnalyzer( analyzerDefs.get( name ) );
namedAnalyzer.setDelegate( analyzer );
- initializedAnalizers.put( name, analyzer );
+ initializedAnalyzers.put( name, analyzer );
}
else {
- //exception
throw new SearchException("Analyzer found with an unknown definition: " +
name);
}
}
}
+
+ //initialize the remaining definitions
+ for ( Map.Entry<String, AnalyzerDef> entry : analyzerDefs.entrySet() ) {
+ if ( ! initializedAnalyzers.containsKey( entry.getKey() ) ) {
+ final Analyzer analyzer = buildAnalyzer( entry.getValue() );
+ initializedAnalyzers.put( entry.getKey(), analyzer );
+ }
+ }
+ return Collections.unmodifiableMap( initializedAnalyzers );
}
private Analyzer buildAnalyzer(AnalyzerDef analyzerDef) {
Modified: search/trunk/src/java/org/hibernate/search/impl/SearchFactoryImpl.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/impl/SearchFactoryImpl.java 2008-05-03
20:11:12 UTC (rev 14641)
+++ search/trunk/src/java/org/hibernate/search/impl/SearchFactoryImpl.java 2008-05-05
15:20:22 UTC (rev 14642)
@@ -43,6 +43,7 @@
import org.hibernate.search.store.DirectoryProvider;
import org.hibernate.search.store.DirectoryProviderFactory;
import org.hibernate.search.store.optimization.OptimizerStrategy;
+import org.apache.lucene.analysis.Analyzer;
/**
* @author Emmanuel Bernard
@@ -66,13 +67,14 @@
private BackendQueueProcessorFactory backendQueueProcessorFactory;
private Map<String, FilterDef> filterDefinitions = new HashMap<String,
FilterDef>();
private FilterCachingStrategy filterCachingStrategy;
- private String indexingStrategy;
+ private Map<String, Analyzer> analyzers;
/**
* Each directory provider (index) can have its own performance settings.
*/
private Map<DirectoryProvider, LuceneIndexingParameters> dirProviderIndexingParams
=
new HashMap<DirectoryProvider, LuceneIndexingParameters>();
+ private String indexingStrategy;
public BackendQueueProcessorFactory getBackendQueueProcessorFactory() {
@@ -256,6 +258,12 @@
getBackendQueueProcessorFactory().getProcessor( queue ).run();
}
+ public Analyzer getAnalyzer(String name) {
+ final Analyzer analyzer = analyzers.get( name );
+ if ( analyzer == null) throw new SearchException( "Unknown Analyzer definition:
" + name);
+ return analyzer;
+ }
+
private void initDocumentBuilders(Configuration cfg, ReflectionManager
reflectionManager, InitContext context) {
Iterator iter = cfg.getClassMappings();
DirectoryProviderFactory factory = new DirectoryProviderFactory();
@@ -281,7 +289,7 @@
}
}
}
- context.initLazyAnalyzers();
+ analyzers = context.initLazyAnalyzers();
factory.startDirectoryProviders();
}
Modified:
search/trunk/src/test/org/hibernate/search/test/embedded/doubleinsert/Contact.java
===================================================================
---
search/trunk/src/test/org/hibernate/search/test/embedded/doubleinsert/Contact.java 2008-05-03
20:11:12 UTC (rev 14641)
+++
search/trunk/src/test/org/hibernate/search/test/embedded/doubleinsert/Contact.java 2008-05-05
15:20:22 UTC (rev 14642)
@@ -22,7 +22,8 @@
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
-import org.apache.log4j.Logger;
+import org.apache.commons.logging.LogFactory;
+import org.apache.commons.logging.Log;
import org.hibernate.annotations.Type;
import org.hibernate.search.annotations.ContainedIn;
import org.hibernate.search.annotations.DocumentId;
@@ -39,7 +40,7 @@
@Indexed
public class Contact implements Serializable {
- private static Logger logger = Logger.getLogger(Contact.class);
+ private static Log logger = LogFactory.getLog(Contact.class);
private static final long serialVersionUID = 1L;