3 * Copyright (C) 2013 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.api
.service
.search
;
12 import java
.util
.HashMap
;
15 import org
.apache
.lucene
.analysis
.Analyzer
;
16 import org
.apache
.lucene
.index
.IndexReader
;
17 import org
.apache
.lucene
.queryparser
.classic
.QueryParser
;
18 import org
.hibernate
.SessionFactory
;
19 import org
.hibernate
.search
.Search
;
20 import org
.hibernate
.search
.SearchFactory
;
21 import org
.hibernate
.search
.indexes
.IndexReaderAccessor
;
22 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
23 import org
.springframework
.stereotype
.Component
;
25 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
26 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
27 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
28 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
29 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
30 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
31 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
34 * @author a.kohlbecker
39 public class LuceneIndexToolProviderImpl
implements ILuceneIndexToolProvider
{
41 private final static String DEFAULT_QURERY_FIELD_NAME
= "titleCache";
44 private SessionFactory sessionFactory
;
46 private final Map
<Class
<?
extends CdmBase
>, QueryParser
> queryParsers
= new HashMap
<Class
<?
extends CdmBase
>, QueryParser
>();
49 * @param sessionfactory
52 private SearchFactory
getCurrentSearchFactory() {
53 return Search
.getFullTextSession(sessionFactory
.getCurrentSession()).getSearchFactory();
58 * TODO the abstract base class DescriptionElementBase can not be used, so
59 * we are using an arbitrary subclass to find the DirectoryProvider, future
60 * versions of hibernate search my allow using abstract base classes see
61 * {@link http://stackoverflow.com/questions/492184/how-do-you-find-all-subclasses-of-a-given-class-in-java}
63 * @param type must not be null
66 protected Class
<?
extends CdmBase
> pushAbstractBaseTypeDown(Class
<?
extends CdmBase
> type
) {
68 throw new NullPointerException("parameter type must not be null");
70 if (type
.equals(DescriptionElementBase
.class)) {
71 return TextData
.class;
73 if (type
.equals(TaxonBase
.class)) {
76 if (type
.equals(TaxonNameBase
.class)) {
77 return NonViralName
.class;
83 * @see eu.etaxonomy.cdm.api.service.search.ILuceneIndexToolProvider#getIndexReaderFor(java.lang.Class)
86 public IndexReader
getIndexReaderFor(Class
<?
extends CdmBase
> clazz
) {
87 IndexReader reader
= getCurrentSearchFactory().getIndexReaderAccessor().open(pushAbstractBaseTypeDown(clazz
));
92 * @see eu.etaxonomy.cdm.api.service.search.ILuceneIndexToolProvider#getQueryParserFor(java.lang.Class)
95 public QueryParser
getQueryParserFor(Class
<?
extends CdmBase
> clazz
) {
96 if(!queryParsers
.containsKey(clazz
)){
97 Analyzer analyzer
= getAnalyzerFor(clazz
);
98 QueryParser parser
= new QueryParser(DEFAULT_QURERY_FIELD_NAME
, analyzer
);
99 queryParsers
.put(clazz
, parser
);
101 return queryParsers
.get(clazz
);
105 * <b>WARING</b> This method might return an Analyzer
106 * which is not suitable for all fields of the lucene document. This method
107 * internally uses the simplified method from {@link {
108 * @link org.hibernate.search.SearchFactory#getAnalyzer(Class)}
110 * TODO implement method which allows to retrieve the correct Analyzer
111 * per document field, this method will have another signature.
113 * @return the Analyzer suitable for the lucene index of the given
117 public Analyzer
getAnalyzerFor(Class
<?
extends CdmBase
> clazz
) {
118 Analyzer analyzer
= getCurrentSearchFactory().getAnalyzer(pushAbstractBaseTypeDown(clazz
));
123 * @see eu.etaxonomy.cdm.api.service.search.ILuceneIndexToolProvider#getQueryFactoryFor(java.lang.Class)
126 public QueryFactory
newQueryFactoryFor(Class
<?
extends CdmBase
> clazz
){
127 return new QueryFactory(this, pushAbstractBaseTypeDown(clazz
));
131 * @see eu.etaxonomy.cdm.api.service.search.ILuceneIndexToolProvider#getIndexReaderAccessor()
134 public IndexReaderAccessor
getIndexReaderAccessor(){
135 return getCurrentSearchFactory().getIndexReaderAccessor();