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.util.ArrayUtil;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.LocaleUtil;
020    import com.liferay.portal.kernel.util.PropsKeys;
021    import com.liferay.portal.kernel.util.PropsUtil;
022    import com.liferay.portal.kernel.util.SetUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    
025    import java.io.Serializable;
026    
027    import java.util.Arrays;
028    import java.util.HashMap;
029    import java.util.Locale;
030    import java.util.Map;
031    import java.util.Set;
032    
033    /**
034     * @author Michael C. Han
035     */
036    public class QueryConfig implements Serializable {
037    
038            public static final String LOCALE = "locale";
039    
040            public static final String SEARCH_SUBFOLDERS = "search.subfolders";
041    
042            public void addHighlightFieldNames(String... highlightFieldNames) {
043                    Set<String> highlightFieldNamesSet = SetUtil.fromArray(
044                            (String[])_attributes.get(_HIGHLIGHT_FIELD_NAMES));
045    
046                    highlightFieldNamesSet.addAll(Arrays.asList(highlightFieldNames));
047    
048                    _attributes.put(
049                            _HIGHLIGHT_FIELD_NAMES,
050                            highlightFieldNamesSet.toArray(
051                                    new String[highlightFieldNamesSet.size()]));
052            }
053    
054            public void addSelectedFieldNames(String... selectedFieldNames) {
055                    Set<String> selectedFieldNamesSet = SetUtil.fromArray(
056                            (String[])_attributes.get(_SELECTED_FIELD_NAMES));
057    
058                    selectedFieldNamesSet.addAll(Arrays.asList(selectedFieldNames));
059    
060                    _attributes.put(
061                            _SELECTED_FIELD_NAMES,
062                            selectedFieldNamesSet.toArray(
063                                    new String[selectedFieldNamesSet.size()]));
064            }
065    
066            public String getAlternateUidFieldName() {
067                    return (String)_attributes.get(_ALTERNATE_UID_FIELD_NAME);
068            }
069    
070            public Serializable getAttribute(String name) {
071                    return _attributes.get(name);
072            }
073    
074            public Map<String, Serializable> getAttributes() {
075                    return _attributes;
076            }
077    
078            public int getCollatedSpellCheckResultScoresThreshold() {
079                    return GetterUtil.getInteger(
080                            _attributes.get(
081                                    PropsKeys.
082                                            INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_SCORES_THRESHOLD),
083                            _INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_SCORES_THRESHOLD);
084            }
085    
086            public String[] getHighlightFieldNames() {
087                    String[] highlightFieldNames = (String[])_attributes.get(
088                            _HIGHLIGHT_FIELD_NAMES);
089    
090                    if (highlightFieldNames != null) {
091                            return highlightFieldNames;
092                    }
093    
094                    return StringPool.EMPTY_ARRAY;
095            }
096    
097            public int getHighlightFragmentSize() {
098                    return GetterUtil.getInteger(
099                            _attributes.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_FRAGMENT_SIZE),
100                            _INDEX_SEARCH_HIGHLIGHT_FRAGMENT_SIZE);
101            }
102    
103            public int getHighlightSnippetSize() {
104                    return GetterUtil.getInteger(
105                            _attributes.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_SNIPPET_SIZE),
106                            _INDEX_SEARCH_HIGHLIGHT_SNIPPET_SIZE);
107            }
108    
109            public Locale getLocale() {
110                    Locale locale = (Locale)_attributes.get(LOCALE);
111    
112                    if (locale == null) {
113                            locale = LocaleUtil.getMostRelevantLocale();
114                    }
115    
116                    return locale;
117            }
118    
119            public int getQueryIndexingThreshold() {
120                    return GetterUtil.getInteger(
121                            _attributes.get(PropsKeys.INDEX_SEARCH_QUERY_INDEXING_THRESHOLD),
122                            _INDEX_SEARCH_QUERY_INDEXING_THRESHOLD);
123            }
124    
125            public int getQuerySuggestionMax() {
126                    return GetterUtil.getInteger(
127                            _attributes.get(PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_MAX),
128                            _INDEX_SEARCH_QUERY_SUGGESTION_MAX);
129            }
130    
131            public int getQuerySuggestionScoresThreshold() {
132                    return GetterUtil.getInteger(
133                            _attributes.get(
134                                    PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_SCORES_THRESHOLD),
135                            _INDEX_SEARCH_QUERY_SUGGESTION_SCORES_THRESHOLD);
136            }
137    
138            public String[] getSelectedFieldNames() {
139                    String[] selectedFieldNames = (String[])_attributes.get(
140                            _SELECTED_FIELD_NAMES);
141    
142                    if (ArrayUtil.isEmpty(selectedFieldNames)) {
143                            return StringPool.EMPTY_ARRAY;
144                    }
145    
146                    return selectedFieldNames;
147            }
148    
149            public String[] getSelectedIndexNames() {
150                    String[] selectedIndexNames = (String[])_attributes.get(
151                            _SELECTED_INDEX_NAMES);
152    
153                    if (ArrayUtil.isEmpty(selectedIndexNames)) {
154                            return StringPool.EMPTY_ARRAY;
155                    }
156    
157                    return selectedIndexNames;
158            }
159    
160            public String[] getSelectedTypes() {
161                    String[] selectedTypes = (String[])_attributes.get(_SELECTED_TYPES);
162    
163                    if (ArrayUtil.isEmpty(selectedTypes)) {
164                            return StringPool.EMPTY_ARRAY;
165                    }
166    
167                    return selectedTypes;
168            }
169    
170            public boolean isAllFieldsSelected() {
171                    String[] selectedFieldNames = getSelectedFieldNames();
172    
173                    if (ArrayUtil.isEmpty(selectedFieldNames)) {
174                            return true;
175                    }
176    
177                    if ((selectedFieldNames.length == 1) &&
178                            selectedFieldNames[0].equals(Field.ANY)) {
179    
180                            return true;
181                    }
182    
183                    return false;
184            }
185    
186            public boolean isCollatedSpellCheckResultEnabled() {
187                    return GetterUtil.getBoolean(
188                            _attributes.get(
189                                    PropsKeys.INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_ENABLED),
190                            _INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_ENABLED);
191            }
192    
193            public boolean isHighlightEnabled() {
194                    return GetterUtil.getBoolean(
195                            _attributes.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_ENABLED),
196                            _INDEX_SEARCH_HIGHLIGHT_ENABLED);
197            }
198    
199            public boolean isHighlightRequireFieldMatch() {
200                    return GetterUtil.getBoolean(
201                            _attributes.get(
202                                    PropsKeys.INDEX_SEARCH_HIGHLIGHT_REQUIRE_FIELD_MATCH),
203                            _INDEX_SEARCH_HIGHLIGHT_REQUIRE_FIELD_MATCH);
204            }
205    
206            public boolean isHitsProcessingEnabled() {
207                    return GetterUtil.getBoolean(
208                            _attributes.get(_HITS_PROCESSING_ENABLED), true);
209            }
210    
211            public boolean isQueryIndexingEnabled() {
212                    return GetterUtil.getBoolean(
213                            _attributes.get(PropsKeys.INDEX_SEARCH_QUERY_INDEXING_ENABLED),
214                            _INDEX_SEARCH_QUERY_INDEXING_ENABLED);
215            }
216    
217            public boolean isQuerySuggestionEnabled() {
218                    return GetterUtil.getBoolean(
219                            _attributes.get(PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_ENABLED),
220                            _INDEX_SEARCH_QUERY_SUGGESTION_ENABLED);
221            }
222    
223            public boolean isScoreEnabled() {
224                    return GetterUtil.getBoolean(
225                            _attributes.get(PropsKeys.INDEX_SEARCH_SCORING_ENABLED),
226                            _INDEX_SEARCH_SCORING_ENABLED);
227            }
228    
229            public boolean isSearchSubfolders() {
230                    return GetterUtil.getBoolean(_attributes.get(SEARCH_SUBFOLDERS));
231            }
232    
233            public Serializable removeAttribute(String name) {
234                    return _attributes.remove(name);
235            }
236    
237            public void setAlternateUidFieldName(String name) {
238                    _attributes.put(_ALTERNATE_UID_FIELD_NAME, name);
239            }
240    
241            public void setAttribute(String name, Serializable value) {
242                    _attributes.put(name, value);
243            }
244    
245            public void setCollatedSpellCheckResultEnabled(
246                    boolean collatedSpellCheckResultEnabled) {
247    
248                    _attributes.put(
249                            PropsKeys.INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_ENABLED,
250                            collatedSpellCheckResultEnabled);
251            }
252    
253            public void setCollatedSpellCheckResultScoresThreshold(
254                    int collatedSpellCheckResultScoresThreshold) {
255    
256                    _attributes.put(
257                            PropsKeys.
258                                    INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_SCORES_THRESHOLD,
259                            collatedSpellCheckResultScoresThreshold);
260            }
261    
262            public void setHighlightEnabled(boolean highlightEnabled) {
263                    if (_INDEX_SEARCH_HIGHLIGHT_ENABLED) {
264                            _attributes.put(
265                                    PropsKeys.INDEX_SEARCH_HIGHLIGHT_ENABLED, highlightEnabled);
266                    }
267                    else {
268                            _attributes.put(PropsKeys.INDEX_SEARCH_HIGHLIGHT_ENABLED, false);
269                    }
270            }
271    
272            public void setHighlightFieldNames(String... highlightFieldNames) {
273                    _attributes.put(_HIGHLIGHT_FIELD_NAMES, highlightFieldNames);
274            }
275    
276            public void setHighlightFragmentSize(int highlightFragmentSize) {
277                    _attributes.put(
278                            PropsKeys.INDEX_SEARCH_HIGHLIGHT_FRAGMENT_SIZE,
279                            highlightFragmentSize);
280            }
281    
282            public void setHighlightRequireFieldMatch(
283                    boolean highlightRequireFieldMatch) {
284    
285                    if (_INDEX_SEARCH_HIGHLIGHT_REQUIRE_FIELD_MATCH) {
286                            _attributes.put(
287                                    PropsKeys.INDEX_SEARCH_HIGHLIGHT_REQUIRE_FIELD_MATCH,
288                                    highlightRequireFieldMatch);
289                    }
290                    else {
291                            _attributes.put(
292                                    PropsKeys.INDEX_SEARCH_HIGHLIGHT_REQUIRE_FIELD_MATCH, false);
293                    }
294            }
295    
296            public void setHighlightSnippetSize(int highlightSnippetSize) {
297                    _attributes.put(
298                            PropsKeys.INDEX_SEARCH_HIGHLIGHT_SNIPPET_SIZE,
299                            highlightSnippetSize);
300            }
301    
302            public void setHitsProcessingEnabled(boolean hitsProcessingEnabled) {
303                    _attributes.put(_HITS_PROCESSING_ENABLED, hitsProcessingEnabled);
304            }
305    
306            public void setLocale(Locale locale) {
307                    _attributes.put(LOCALE, locale);
308            }
309    
310            public void setQueryIndexingEnabled(boolean enabled) {
311                    _attributes.put(PropsKeys.INDEX_SEARCH_QUERY_INDEXING_ENABLED, enabled);
312            }
313    
314            public void setQueryIndexingThreshold(int queryIndexingThreshold) {
315                    _attributes.put(
316                            PropsKeys.INDEX_SEARCH_QUERY_INDEXING_THRESHOLD,
317                            queryIndexingThreshold);
318            }
319    
320            public void setQuerySuggestionEnabled(boolean querySuggestionEnabled) {
321                    _attributes.put(
322                            PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_ENABLED,
323                            querySuggestionEnabled);
324            }
325    
326            public void setQuerySuggestionScoresThreshold(
327                    int querySuggestionScoresThreshold) {
328    
329                    _attributes.put(
330                            PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_SCORES_THRESHOLD,
331                            querySuggestionScoresThreshold);
332            }
333    
334            public void setQuerySuggestionsMax(int querySuggestionMax) {
335                    _attributes.put(
336                            PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_MAX, querySuggestionMax);
337            }
338    
339            public void setScoreEnabled(boolean scoreEnabled) {
340                    _attributes.put(PropsKeys.INDEX_SEARCH_SCORING_ENABLED, scoreEnabled);
341            }
342    
343            public void setSearchSubfolders(boolean searchSubfolders) {
344                    _attributes.put(SEARCH_SUBFOLDERS, searchSubfolders);
345            }
346    
347            public void setSelectedFieldNames(String... selectedFieldNames) {
348                    _attributes.put(_SELECTED_FIELD_NAMES, selectedFieldNames);
349            }
350    
351            public void setSelectedIndexNames(String... selectedIndexNames) {
352                    _attributes.put(_SELECTED_INDEX_NAMES, selectedIndexNames);
353            }
354    
355            public void setSelectedTypes(String... selectedTypes) {
356                    _attributes.put(_SELECTED_TYPES, selectedTypes);
357            }
358    
359            private static final String _ALTERNATE_UID_FIELD_NAME =
360                    "alternateUidFieldName";
361    
362            private static final String _HIGHLIGHT_FIELD_NAMES = "highlightFieldNames";
363    
364            private static final String _HITS_PROCESSING_ENABLED =
365                    "hitsProcessingEnabled";
366    
367            private static final boolean
368                    _INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_ENABLED =
369                            GetterUtil.getBoolean(
370                                    PropsUtil.get(
371                                            PropsKeys.INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_ENABLED),
372                                    true);
373    
374            private static final int
375                    _INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_SCORES_THRESHOLD =
376                            GetterUtil.getInteger(
377                                    PropsUtil.get(
378                                            PropsKeys.
379                                                    INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_SCORES_THRESHOLD),
380                                    50);
381    
382            private static final boolean _INDEX_SEARCH_HIGHLIGHT_ENABLED =
383                    GetterUtil.getBoolean(
384                            PropsUtil.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_ENABLED));
385    
386            private static final int _INDEX_SEARCH_HIGHLIGHT_FRAGMENT_SIZE =
387                    GetterUtil.getInteger(
388                            PropsUtil.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_FRAGMENT_SIZE));
389    
390            private static final boolean _INDEX_SEARCH_HIGHLIGHT_REQUIRE_FIELD_MATCH =
391                    GetterUtil.getBoolean(
392                            PropsUtil.get(
393                                    PropsKeys.INDEX_SEARCH_HIGHLIGHT_REQUIRE_FIELD_MATCH));
394    
395            private static final int _INDEX_SEARCH_HIGHLIGHT_SNIPPET_SIZE =
396                    GetterUtil.getInteger(
397                            PropsUtil.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_SNIPPET_SIZE));
398    
399            private static final boolean _INDEX_SEARCH_QUERY_INDEXING_ENABLED =
400                    GetterUtil.getBoolean(
401                            PropsUtil.get(PropsKeys.INDEX_SEARCH_QUERY_INDEXING_ENABLED), true);
402    
403            private static final int _INDEX_SEARCH_QUERY_INDEXING_THRESHOLD =
404                    GetterUtil.getInteger(
405                            PropsUtil.get(PropsKeys.INDEX_SEARCH_QUERY_INDEXING_THRESHOLD), 50);
406    
407            private static final boolean _INDEX_SEARCH_QUERY_SUGGESTION_ENABLED =
408                    GetterUtil.getBoolean(
409                            PropsUtil.get(PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_ENABLED),
410                            true);
411    
412            private static final int _INDEX_SEARCH_QUERY_SUGGESTION_MAX =
413                    GetterUtil.getInteger(
414                            PropsUtil.get(PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_MAX), 5);
415    
416            private static final int
417                    _INDEX_SEARCH_QUERY_SUGGESTION_SCORES_THRESHOLD = GetterUtil.getInteger(
418                            PropsUtil.get(
419                                    PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_SCORES_THRESHOLD),
420                            50);
421    
422            private static final boolean _INDEX_SEARCH_SCORING_ENABLED =
423                    GetterUtil.getBoolean(
424                            PropsUtil.get(PropsKeys.INDEX_SEARCH_SCORING_ENABLED));
425    
426            private static final String _SELECTED_FIELD_NAMES = "selectedFieldNames";
427    
428            private static final String _SELECTED_INDEX_NAMES = "selectedIndexNames";
429    
430            private static final String _SELECTED_TYPES = "selectedTypes";
431    
432            private final Map<String, Serializable> _attributes = new HashMap<>();
433    
434    }