001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.search;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.StringPool;
020    
021    import java.util.Collections;
022    import java.util.List;
023    import java.util.Map;
024    
025    /**
026     * @author Michael C. Han
027     */
028    public abstract class BaseIndexSearcher
029            implements IndexSearcher, QuerySuggester {
030    
031            /**
032             * @deprecated As of 7.0.0, replaced by {@link #search(SearchContext,
033             *             Query)}
034             */
035            @Deprecated
036            @Override
037            public Hits search(
038                            String searchEngineId, long companyId, Query query, Sort[] sorts,
039                            int start, int end)
040                    throws SearchException {
041    
042                    SearchContext searchContext = new SearchContext();
043    
044                    searchContext.setCompanyId(companyId);
045                    searchContext.setEnd(end);
046                    searchContext.setSearchEngineId(searchEngineId);
047                    searchContext.setSorts(sorts);
048                    searchContext.setStart(start);
049    
050                    return search(searchContext, query);
051            }
052    
053            public void setQuerySuggester(QuerySuggester querySuggester) {
054                    _querySuggester = querySuggester;
055            }
056    
057            @Override
058            public String spellCheckKeywords(SearchContext searchContext)
059                    throws SearchException {
060    
061                    if (_querySuggester == null) {
062                            if (_log.isDebugEnabled()) {
063                                    _log.debug("No query suggester configured");
064                            }
065    
066                            return StringPool.BLANK;
067                    }
068    
069                    return _querySuggester.spellCheckKeywords(searchContext);
070            }
071    
072            @Override
073            public Map<String, List<String>> spellCheckKeywords(
074                            SearchContext searchContext, int max)
075                    throws SearchException {
076    
077                    if (_querySuggester == null) {
078                            if (_log.isDebugEnabled()) {
079                                    _log.debug("No query suggester configured");
080                            }
081    
082                            return Collections.emptyMap();
083                    }
084    
085                    return _querySuggester.spellCheckKeywords(searchContext, max);
086            }
087    
088            @Override
089            public String[] suggestKeywordQueries(SearchContext searchContext, int max)
090                    throws SearchException {
091    
092                    if (_querySuggester == null) {
093                            if (_log.isDebugEnabled()) {
094                                    _log.debug("No query suggester configured");
095                            }
096    
097                            return StringPool.EMPTY_ARRAY;
098                    }
099    
100                    return _querySuggester.suggestKeywordQueries(searchContext, max);
101            }
102    
103            private static final Log _log = LogFactoryUtil.getLog(
104                    BaseIndexSearcher.class);
105    
106            private QuerySuggester _querySuggester;
107    
108    }