View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.fulltext.spell;
24  
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import org.apache.solr.client.solrj.SolrQuery;
29  import org.apache.solr.client.solrj.response.QueryResponse;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  import org.springframework.beans.factory.annotation.Required;
33  
34  import com.gisgraphy.domain.valueobject.Constants;
35  import com.gisgraphy.fulltext.IsolrClient;
36  
37  /**
38   * 
39   * Solr implementation of {@link ISpellCheckerIndexer}
40   * 
41   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
42   * 
43   */
44  public class SpellCheckerIndexer implements ISpellCheckerIndexer {
45  
46      private IsolrClient solrClient;
47  
48      /**
49       * The logger
50       */
51      protected static final Logger logger = LoggerFactory
52  	    .getLogger(SpellCheckerIndexer.class);
53  
54      /*
55       * (non-Javadoc)
56       * 
57       * @see com.gisgraphy.domain.geoloc.service.fulltextsearch.spell.ISpellCheckerIndexer#buildAllIndex()
58       */
59      public Map<String, Boolean> buildAllIndex() {
60  	Map<String, Boolean> resultMap = new HashMap<String, Boolean>();
61  	for (SpellCheckerDictionaryNames dictionary : SpellCheckerDictionaryNames
62  		.values()) {
63  	    Boolean result = buildIndex(dictionary);
64  	    resultMap.put(dictionary.name(), result);
65  	}
66  	return resultMap;
67  
68      }
69  
70      /*
71       * (non-Javadoc)
72       * 
73       * @see com.gisgraphy.domain.geoloc.service.fulltextsearch.spell.ISpellCheckerIndexer#buildIndex(com.gisgraphy.domain.geoloc.service.fulltextsearch.spell.SpellCheckerDictionaryNames)
74       */
75      public boolean buildIndex(
76  	    SpellCheckerDictionaryNames spellCheckerDictionaryName) {
77  	if (!SpellCheckerConfig.isEnabled()) {
78  	    return false;
79  	}
80  	
81  	SolrQuery solrQuery = new SolrQuery();
82  	solrQuery.setQueryType(Constants.SolrQueryType.spellcheck.toString());
83  	solrQuery.add(Constants.SPELLCHECKER_DICTIONARY_NAME_PARAMETER,
84  		spellCheckerDictionaryName.toString());
85  	solrQuery.add(Constants.SPELLCHECKER_BUILD_PARAMETER, "true");
86  	solrQuery.add(Constants.SPELLCHECKER_ENABLED_PARAMETER, "true");
87  	solrQuery.setQuery("spell");
88  	try {
89  	    QueryResponse response = solrClient.getServer().query(solrQuery);
90  	    if (response.getStatus() != 0) {
91  		 logger.error("Indexing dictionary "
92  			    + spellCheckerDictionaryName.name()+" fails");
93  		return false;
94  	    }
95  	    logger.info("Successfully indexing dictionary "
96  		    + spellCheckerDictionaryName.name());
97  	    return true;
98  
99  	} catch (Exception e) {
100 	    logger.error("An error has occured when indexing spellchecker "
101 		    + spellCheckerDictionaryName + " : " + e);
102 	    return false;
103 	}
104 
105     }
106 
107     /**
108      * @param solrClient
109      *                the solrClient to set
110      */
111     @Required
112     public void setSolrClient(IsolrClient solrClient) {
113 	this.solrClient = solrClient;
114     }
115 
116 }