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.expando.kernel.model.ExpandoBridge;
018    import com.liferay.expando.kernel.model.ExpandoColumnConstants;
019    import com.liferay.expando.kernel.util.ExpandoBridgeFactoryUtil;
020    import com.liferay.portal.kernel.model.Group;
021    import com.liferay.portal.kernel.search.facet.Facet;
022    import com.liferay.portal.kernel.search.filter.BooleanFilter;
023    import com.liferay.portal.kernel.search.filter.Filter;
024    import com.liferay.portal.kernel.search.filter.TermsFilter;
025    import com.liferay.portal.kernel.search.generic.BooleanQueryImpl;
026    import com.liferay.portal.kernel.search.generic.MatchAllQuery;
027    import com.liferay.portal.kernel.service.GroupLocalServiceUtil;
028    import com.liferay.portal.kernel.util.ArrayUtil;
029    import com.liferay.portal.kernel.util.GetterUtil;
030    import com.liferay.portal.kernel.util.ListUtil;
031    import com.liferay.portal.kernel.util.SetUtil;
032    import com.liferay.portal.kernel.util.UnicodeProperties;
033    import com.liferay.portal.kernel.util.Validator;
034    
035    import java.util.List;
036    import java.util.Map;
037    import java.util.Set;
038    
039    /**
040     * @author     Raymond Aug??
041     * @deprecated As of 7.0.0, replaced by {@link com.liferay.portal.kernel.search.facet.searcher.FacetedSearcher}
042     */
043    @Deprecated
044    public class FacetedSearcher extends BaseSearcher {
045    
046            public static Indexer<?> getInstance() {
047                    return new FacetedSearcher();
048            }
049    
050            protected void addSearchExpandoKeywords(
051                            BooleanQuery searchQuery, SearchContext searchContext,
052                            String keywords, String className)
053                    throws Exception {
054    
055                    ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
056                            searchContext.getCompanyId(), className);
057    
058                    Set<String> attributeNames = SetUtil.fromEnumeration(
059                            expandoBridge.getAttributeNames());
060    
061                    for (String attributeName : attributeNames) {
062                            UnicodeProperties properties = expandoBridge.getAttributeProperties(
063                                    attributeName);
064    
065                            int indexType = GetterUtil.getInteger(
066                                    properties.getProperty(ExpandoColumnConstants.INDEX_TYPE));
067    
068                            if (indexType != ExpandoColumnConstants.INDEX_TYPE_NONE) {
069                                    String fieldName = getExpandoFieldName(
070                                            searchContext, expandoBridge, attributeName);
071    
072                                    if (searchContext.isAndSearch()) {
073                                            searchQuery.addRequiredTerm(fieldName, keywords);
074                                    }
075                                    else {
076                                            searchQuery.addTerm(fieldName, keywords);
077                                    }
078                            }
079                    }
080            }
081    
082            @Override
083            protected BooleanQuery createFullQuery(
084                            BooleanFilter fullQueryBooleanFilter, SearchContext searchContext)
085                    throws Exception {
086    
087                    BooleanQuery searchQuery = new BooleanQueryImpl();
088    
089                    String keywords = searchContext.getKeywords();
090    
091                    if (Validator.isNotNull(keywords)) {
092                            addSearchLocalizedTerm(
093                                    searchQuery, searchContext, Field.ASSET_CATEGORY_TITLES, false);
094    
095                            searchQuery.addExactTerm(Field.ASSET_TAG_NAMES, keywords);
096    
097                            int groupId = GetterUtil.getInteger(
098                                    searchContext.getAttribute(Field.GROUP_ID));
099    
100                            if (groupId == 0) {
101                                    fullQueryBooleanFilter.addTerm(
102                                            Field.STAGING_GROUP, "true", BooleanClauseOccur.MUST_NOT);
103                            }
104    
105                            searchQuery.addTerms(Field.KEYWORDS, keywords);
106                    }
107    
108                    List<Group> inactiveGroups = GroupLocalServiceUtil.getActiveGroups(
109                            searchContext.getCompanyId(), false);
110    
111                    if (ListUtil.isNotEmpty(inactiveGroups)) {
112                            TermsFilter groupIdTermsFilter = new TermsFilter(Field.GROUP_ID);
113    
114                            groupIdTermsFilter.addValues(
115                                    ArrayUtil.toStringArray(
116                                            ListUtil.toArray(inactiveGroups, Group.GROUP_ID_ACCESSOR)));
117    
118                            fullQueryBooleanFilter.add(
119                                    groupIdTermsFilter, BooleanClauseOccur.MUST_NOT);
120                    }
121    
122                    for (String entryClassName : searchContext.getEntryClassNames()) {
123                            Indexer<?> indexer = IndexerRegistryUtil.getIndexer(entryClassName);
124    
125                            if (indexer == null) {
126                                    continue;
127                            }
128    
129                            String searchEngineId = searchContext.getSearchEngineId();
130    
131                            if (!searchEngineId.equals(indexer.getSearchEngineId())) {
132                                    continue;
133                            }
134    
135                            if (Validator.isNotNull(keywords)) {
136                                    addSearchExpandoKeywords(
137                                            searchQuery, searchContext, keywords, entryClassName);
138                            }
139    
140                            indexer.postProcessSearchQuery(
141                                    searchQuery, fullQueryBooleanFilter, searchContext);
142    
143                            for (IndexerPostProcessor indexerPostProcessor :
144                                            indexer.getIndexerPostProcessors()) {
145    
146                                    indexerPostProcessor.postProcessSearchQuery(
147                                            searchQuery, fullQueryBooleanFilter, searchContext);
148                            }
149    
150                            doPostProcessSearchQuery(indexer, searchQuery, searchContext);
151                    }
152    
153                    Map<String, Facet> facets = searchContext.getFacets();
154    
155                    BooleanFilter facetBooleanFilter = new BooleanFilter();
156    
157                    for (Facet facet : facets.values()) {
158                            BooleanClause<Filter> facetClause =
159                                    facet.getFacetFilterBooleanClause();
160    
161                            if (facetClause != null) {
162                                    facetBooleanFilter.add(
163                                            facetClause.getClause(),
164                                            facetClause.getBooleanClauseOccur());
165                            }
166                    }
167    
168                    addFacetClause(searchContext, facetBooleanFilter, facets.values());
169    
170                    if (facetBooleanFilter.hasClauses()) {
171                            fullQueryBooleanFilter.add(
172                                    facetBooleanFilter, BooleanClauseOccur.MUST);
173                    }
174    
175                    BooleanQuery fullQuery = new BooleanQueryImpl();
176    
177                    if (fullQueryBooleanFilter.hasClauses()) {
178                            fullQuery.setPreBooleanFilter(fullQueryBooleanFilter);
179                    }
180    
181                    if (searchQuery.hasClauses()) {
182                            fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
183                    }
184    
185                    BooleanClause<Query>[] booleanClauses =
186                            searchContext.getBooleanClauses();
187    
188                    if (booleanClauses != null) {
189                            for (BooleanClause<Query> booleanClause : booleanClauses) {
190                                    fullQuery.add(
191                                            booleanClause.getClause(),
192                                            booleanClause.getBooleanClauseOccur());
193                            }
194                    }
195    
196                    for (String entryClassName : searchContext.getEntryClassNames()) {
197                            Indexer<?> indexer = IndexerRegistryUtil.getIndexer(entryClassName);
198    
199                            if (indexer == null) {
200                                    continue;
201                            }
202    
203                            String searchEngineId = searchContext.getSearchEngineId();
204    
205                            if (!searchEngineId.equals(indexer.getSearchEngineId())) {
206                                    continue;
207                            }
208    
209                            for (IndexerPostProcessor indexerPostProcessor :
210                                            indexer.getIndexerPostProcessors()) {
211    
212                                    indexerPostProcessor.postProcessFullQuery(
213                                            fullQuery, searchContext);
214                            }
215                    }
216    
217                    return fullQuery;
218            }
219    
220            @Override
221            protected Hits doSearch(SearchContext searchContext)
222                    throws SearchException {
223    
224                    try {
225                            searchContext.setSearchEngineId(getSearchEngineId());
226    
227                            BooleanFilter queryBooleanFilter = new BooleanFilter();
228    
229                            queryBooleanFilter.addRequiredTerm(
230                                    Field.COMPANY_ID, searchContext.getCompanyId());
231    
232                            Query fullQuery = createFullQuery(
233                                    queryBooleanFilter, searchContext);
234    
235                            if (!fullQuery.hasChildren()) {
236                                    BooleanFilter preBooleanFilter =
237                                            fullQuery.getPreBooleanFilter();
238    
239                                    fullQuery = new MatchAllQuery();
240    
241                                    fullQuery.setPreBooleanFilter(preBooleanFilter);
242                            }
243    
244                            QueryConfig queryConfig = searchContext.getQueryConfig();
245    
246                            fullQuery.setQueryConfig(queryConfig);
247    
248                            return IndexSearcherHelperUtil.search(searchContext, fullQuery);
249                    }
250                    catch (Exception e) {
251                            throw new SearchException(e);
252                    }
253            }
254    
255            @Override
256            protected boolean isUseSearchResultPermissionFilter(
257                    SearchContext searchContext) {
258    
259                    if (searchContext.getEntryClassNames() == null) {
260                            return super.isFilterSearch();
261                    }
262    
263                    for (String entryClassName : searchContext.getEntryClassNames()) {
264                            Indexer<?> indexer = IndexerRegistryUtil.getIndexer(entryClassName);
265    
266                            if (indexer == null) {
267                                    continue;
268                            }
269    
270                            if (indexer.isFilterSearch()) {
271                                    return true;
272                            }
273                    }
274    
275                    return super.isFilterSearch();
276            }
277    
278    }