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.search.suggest.QuerySuggester;
020    import com.liferay.portal.kernel.search.suggest.Suggester;
021    import com.liferay.portal.kernel.search.suggest.SuggesterResults;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.Validator;
024    
025    import java.util.Collections;
026    import java.util.List;
027    import java.util.Map;
028    
029    /**
030     * @author Michael C. Han
031     */
032    public abstract class BaseIndexSearcher
033            implements IndexSearcher, QuerySuggester {
034    
035            /**
036             * @deprecated As of 7.0.0, replaced by {@link #search(SearchContext,
037             *             Query)}
038             */
039            @Deprecated
040            @Override
041            public Hits search(
042                            String searchEngineId, long companyId, Query query, Sort[] sorts,
043                            int start, int end)
044                    throws SearchException {
045    
046                    SearchContext searchContext = new SearchContext();
047    
048                    searchContext.setCompanyId(companyId);
049                    searchContext.setEnd(end);
050                    searchContext.setSearchEngineId(searchEngineId);
051                    searchContext.setSorts(sorts);
052                    searchContext.setStart(start);
053    
054                    return search(searchContext, query);
055            }
056    
057            public void setQuerySuggester(QuerySuggester querySuggester) {
058                    _querySuggester = querySuggester;
059            }
060    
061            @Override
062            public String spellCheckKeywords(SearchContext searchContext)
063                    throws SearchException {
064    
065                    if (_querySuggester == null) {
066                            if (_log.isDebugEnabled()) {
067                                    _log.debug("No query suggester configured");
068                            }
069    
070                            return StringPool.BLANK;
071                    }
072    
073                    return _querySuggester.spellCheckKeywords(searchContext);
074            }
075    
076            @Override
077            public Map<String, List<String>> spellCheckKeywords(
078                            SearchContext searchContext, int max)
079                    throws SearchException {
080    
081                    if (_querySuggester == null) {
082                            if (_log.isDebugEnabled()) {
083                                    _log.debug("No query suggester configured");
084                            }
085    
086                            return Collections.emptyMap();
087                    }
088    
089                    return _querySuggester.spellCheckKeywords(searchContext, max);
090            }
091    
092            @Override
093            public SuggesterResults suggest(
094                            SearchContext searchContext, Suggester suggester)
095                    throws SearchException {
096    
097                    if (_querySuggester == null) {
098                            if (_log.isDebugEnabled()) {
099                                    _log.debug("No query suggester configured");
100                            }
101    
102                            return new SuggesterResults();
103                    }
104    
105                    return _querySuggester.suggest(searchContext, suggester);
106            }
107    
108            @Override
109            public String[] suggestKeywordQueries(SearchContext searchContext, int max)
110                    throws SearchException {
111    
112                    if (_querySuggester == null) {
113                            if (_log.isDebugEnabled()) {
114                                    _log.debug("No query suggester configured");
115                            }
116    
117                            return StringPool.EMPTY_ARRAY;
118                    }
119    
120                    return _querySuggester.suggestKeywordQueries(searchContext, max);
121            }
122    
123            protected void populateUID(Document document, QueryConfig queryConfig) {
124                    Field uidField = document.getField(Field.UID);
125    
126                    if (uidField != null) {
127                            return;
128                    }
129    
130                    if (Validator.isNull(queryConfig.getAlternateUidFieldName())) {
131                            return;
132                    }
133    
134                    String uidValue = document.get(queryConfig.getAlternateUidFieldName());
135    
136                    if (Validator.isNotNull(uidValue)) {
137                            uidField = new Field(Field.UID, uidValue);
138    
139                            document.add(uidField);
140                    }
141            }
142    
143            private static final Log _log = LogFactoryUtil.getLog(
144                    BaseIndexSearcher.class);
145    
146            private QuerySuggester _querySuggester;
147    
148    }