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 Serializable getAttribute(String name) {
067                    return _attributes.get(name);
068            }
069    
070            public Map<String, Serializable> getAttributes() {
071                    return _attributes;
072            }
073    
074            public int getCollatedSpellCheckResultScoresThreshold() {
075                    return GetterUtil.getInteger(
076                            _attributes.get(
077                                    PropsKeys.
078                                            INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_SCORES_THRESHOLD),
079                                    _INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_SCORES_THRESHOLD);
080            }
081    
082            public String[] getHighlightFieldNames() {
083                    String[] highlightFieldNames = (String[])_attributes.get(
084                            _HIGHLIGHT_FIELD_NAMES);
085    
086                    if (highlightFieldNames != null) {
087                            return highlightFieldNames;
088                    }
089    
090                    return StringPool.EMPTY_ARRAY;
091            }
092    
093            public int getHighlightFragmentSize() {
094                    return GetterUtil.getInteger(
095                            _attributes.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_FRAGMENT_SIZE),
096                            _INDEX_SEARCH_HIGHLIGHT_FRAGMENT_SIZE);
097            }
098    
099            public int getHighlightSnippetSize() {
100                    return GetterUtil.getInteger(
101                            _attributes.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_SNIPPET_SIZE),
102                            _INDEX_SEARCH_HIGHLIGHT_SNIPPET_SIZE);
103            }
104    
105            public Locale getLocale() {
106                    Locale locale = (Locale)_attributes.get(LOCALE);
107    
108                    if (locale == null) {
109                            locale = LocaleUtil.getMostRelevantLocale();
110                    }
111    
112                    return locale;
113            }
114    
115            public int getQueryIndexingThreshold() {
116                    return GetterUtil.getInteger(
117                            _attributes.get(PropsKeys.INDEX_SEARCH_QUERY_INDEXING_THRESHOLD),
118                            _INDEX_SEARCH_QUERY_INDEXING_THRESHOLD);
119            }
120    
121            public int getQuerySuggestionMax() {
122                    return GetterUtil.getInteger(
123                            _attributes.get(PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_MAX),
124                            _INDEX_SEARCH_QUERY_SUGGESTION_MAX);
125            }
126    
127            public int getQuerySuggestionScoresThreshold() {
128                    return GetterUtil.getInteger(
129                            _attributes.get(
130                                    PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_SCORES_THRESHOLD),
131                            _INDEX_SEARCH_QUERY_SUGGESTION_SCORES_THRESHOLD);
132            }
133    
134            public String[] getSelectedFieldNames() {
135                    String[] selectedFieldNames = (String[])_attributes.get(
136                            _SELECTED_FIELD_NAMES);
137    
138                    if (selectedFieldNames != null) {
139                            return selectedFieldNames;
140                    }
141    
142                    return StringPool.EMPTY_ARRAY;
143            }
144    
145            public boolean isAllFieldsSelected() {
146                    String[] selectedFieldNames = getSelectedFieldNames();
147    
148                    if (ArrayUtil.isEmpty(selectedFieldNames)) {
149                            return true;
150                    }
151    
152                    if ((selectedFieldNames.length == 1) &&
153                            selectedFieldNames[0].equals(Field.ANY)) {
154    
155                            return true;
156                    }
157    
158                    return false;
159            }
160    
161            public boolean isCollatedSpellCheckResultEnabled() {
162                    return GetterUtil.getBoolean(
163                            _attributes.get(
164                                    PropsKeys.INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_ENABLED),
165                            _INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_ENABLED);
166            }
167    
168            public boolean isHighlightEnabled() {
169                    return GetterUtil.getBoolean(
170                            _attributes.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_ENABLED),
171                            _INDEX_SEARCH_HIGHLIGHT_ENABLED);
172            }
173    
174            public boolean isHighlightRequireFieldMatch() {
175                    return GetterUtil.getBoolean(
176                            _attributes.get(
177                                    PropsKeys.INDEX_SEARCH_HIGHLIGHT_REQUIRE_FIELD_MATCH),
178                            _INDEX_SEARCH_HIGHLIGHT_REQUIRE_FIELD_MATCH);
179            }
180    
181            public boolean isHitsProcessingEnabled() {
182                    return GetterUtil.getBoolean(
183                            _attributes.get(_HITS_PROCESSING_ENABLED), true);
184            }
185    
186            public boolean isQueryIndexingEnabled() {
187                    return GetterUtil.getBoolean(
188                            _attributes.get(PropsKeys.INDEX_SEARCH_QUERY_INDEXING_ENABLED),
189                            _INDEX_SEARCH_QUERY_INDEXING_ENABLED);
190            }
191    
192            public boolean isQuerySuggestionEnabled() {
193                    return GetterUtil.getBoolean(
194                            _attributes.get(PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_ENABLED),
195                            _INDEX_SEARCH_QUERY_SUGGESTION_ENABLED);
196            }
197    
198            public boolean isScoreEnabled() {
199                    return GetterUtil.getBoolean(
200                            _attributes.get(PropsKeys.INDEX_SEARCH_SCORING_ENABLED),
201                            _INDEX_SEARCH_SCORING_ENABLED);
202            }
203    
204            public boolean isSearchSubfolders() {
205                    return GetterUtil.getBoolean(_attributes.get(SEARCH_SUBFOLDERS));
206            }
207    
208            public Serializable removeAttribute(String name) {
209                    return _attributes.remove(name);
210            }
211    
212            public void setAttribute(String name, Serializable value) {
213                    _attributes.put(name, value);
214            }
215    
216            public void setCollatedSpellCheckResultEnabled(
217                    boolean collatedSpellCheckResultEnabled) {
218    
219                    _attributes.put(
220                            PropsKeys.INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_ENABLED,
221                            collatedSpellCheckResultEnabled);
222            }
223    
224            public void setCollatedSpellCheckResultScoresThreshold(
225                    int collatedSpellCheckResultScoresThreshold) {
226    
227                    _attributes.put(
228                            PropsKeys.
229                                    INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_SCORES_THRESHOLD,
230                            collatedSpellCheckResultScoresThreshold);
231            }
232    
233            public void setHighlightEnabled(boolean highlightEnabled) {
234                    if (_INDEX_SEARCH_HIGHLIGHT_ENABLED) {
235                            _attributes.put(
236                                    PropsKeys.INDEX_SEARCH_HIGHLIGHT_ENABLED, highlightEnabled);
237                    }
238                    else {
239                            _attributes.put(PropsKeys.INDEX_SEARCH_HIGHLIGHT_ENABLED, false);
240                    }
241            }
242    
243            public void setHighlightFieldNames(String... highlightFieldNames) {
244                    _attributes.put(_HIGHLIGHT_FIELD_NAMES, highlightFieldNames);
245            }
246    
247            public void setHighlightFragmentSize(int highlightFragmentSize) {
248                    _attributes.put(
249                            PropsKeys.INDEX_SEARCH_HIGHLIGHT_FRAGMENT_SIZE,
250                            highlightFragmentSize);
251            }
252    
253            public void setHighlightRequireFieldMatch(
254                    boolean highlightRequireFieldMatch) {
255    
256                    if (_INDEX_SEARCH_HIGHLIGHT_REQUIRE_FIELD_MATCH) {
257                            _attributes.put(
258                                    PropsKeys.INDEX_SEARCH_HIGHLIGHT_REQUIRE_FIELD_MATCH,
259                                    highlightRequireFieldMatch);
260                    }
261                    else {
262                            _attributes.put(
263                                    PropsKeys.INDEX_SEARCH_HIGHLIGHT_REQUIRE_FIELD_MATCH, false);
264                    }
265            }
266    
267            public void setHighlightSnippetSize(int highlightSnippetSize) {
268                    _attributes.put(
269                            PropsKeys.INDEX_SEARCH_HIGHLIGHT_SNIPPET_SIZE,
270                            highlightSnippetSize);
271            }
272    
273            public void setHitsProcessingEnabled(boolean hitsProcessingEnabled) {
274                    _attributes.put(_HITS_PROCESSING_ENABLED, hitsProcessingEnabled);
275            }
276    
277            public void setLocale(Locale locale) {
278                    _attributes.put(LOCALE, locale);
279            }
280    
281            public void setQueryIndexingEnabled(boolean enabled) {
282                    _attributes.put(PropsKeys.INDEX_SEARCH_QUERY_INDEXING_ENABLED, enabled);
283            }
284    
285            public void setQueryIndexingThreshold(int queryIndexingThreshold) {
286                    _attributes.put(
287                            PropsKeys.INDEX_SEARCH_QUERY_INDEXING_THRESHOLD,
288                            queryIndexingThreshold);
289            }
290    
291            public void setQuerySuggestionEnabled(boolean querySuggestionEnabled) {
292                    _attributes.put(
293                            PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_ENABLED,
294                            querySuggestionEnabled);
295            }
296    
297            public void setQuerySuggestionScoresThreshold(
298                    int querySuggestionScoresThreshold) {
299    
300                    _attributes.put(
301                            PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_SCORES_THRESHOLD,
302                            querySuggestionScoresThreshold);
303            }
304    
305            public void setQuerySuggestionsMax(int querySuggestionMax) {
306                    _attributes.put(
307                            PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_MAX, querySuggestionMax);
308            }
309    
310            public void setScoreEnabled(boolean scoreEnabled) {
311                    _attributes.put(PropsKeys.INDEX_SEARCH_SCORING_ENABLED, scoreEnabled);
312            }
313    
314            public void setSearchSubfolders(boolean searchSubfolders) {
315                    _attributes.put(SEARCH_SUBFOLDERS, searchSubfolders);
316            }
317    
318            public void setSelectedFieldNames(String... selectedFieldNames) {
319                    _attributes.put(_SELECTED_FIELD_NAMES, selectedFieldNames);
320            }
321    
322            private static final String _HIGHLIGHT_FIELD_NAMES = "highlightFieldNames";
323    
324            private static final String _HITS_PROCESSING_ENABLED =
325                    "hitsProcessingEnabled";
326    
327            private static final boolean
328                    _INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_ENABLED =
329                            GetterUtil.getBoolean(
330                                    PropsUtil.get(
331                                            PropsKeys.INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_ENABLED),
332                                    true);
333    
334            private static final int
335                    _INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_SCORES_THRESHOLD =
336                            GetterUtil.getInteger(
337                                    PropsUtil.get(
338                                            PropsKeys.
339                                                    INDEX_SEARCH_COLLATED_SPELL_CHECK_RESULT_SCORES_THRESHOLD),
340                                    50);
341    
342            private static final boolean _INDEX_SEARCH_HIGHLIGHT_ENABLED =
343                    GetterUtil.getBoolean(
344                            PropsUtil.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_ENABLED));
345    
346            private static final int _INDEX_SEARCH_HIGHLIGHT_FRAGMENT_SIZE =
347                    GetterUtil.getInteger(
348                            PropsUtil.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_FRAGMENT_SIZE));
349    
350            private static final boolean _INDEX_SEARCH_HIGHLIGHT_REQUIRE_FIELD_MATCH =
351                    GetterUtil.getBoolean(
352                            PropsUtil.get(
353                                    PropsKeys.INDEX_SEARCH_HIGHLIGHT_REQUIRE_FIELD_MATCH));
354    
355            private static final int _INDEX_SEARCH_HIGHLIGHT_SNIPPET_SIZE =
356                    GetterUtil.getInteger(
357                            PropsUtil.get(PropsKeys.INDEX_SEARCH_HIGHLIGHT_SNIPPET_SIZE));
358    
359            private static final boolean _INDEX_SEARCH_QUERY_INDEXING_ENABLED =
360                    GetterUtil.getBoolean(
361                            PropsUtil.get(PropsKeys.INDEX_SEARCH_QUERY_INDEXING_ENABLED), true);
362    
363            private static final int _INDEX_SEARCH_QUERY_INDEXING_THRESHOLD =
364                    GetterUtil.getInteger(
365                            PropsUtil.get(PropsKeys.INDEX_SEARCH_QUERY_INDEXING_THRESHOLD), 50);
366    
367            private static final boolean _INDEX_SEARCH_QUERY_SUGGESTION_ENABLED =
368                    GetterUtil.getBoolean(
369                            PropsUtil.get(PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_ENABLED),
370                            true);
371    
372            private static final int _INDEX_SEARCH_QUERY_SUGGESTION_MAX =
373                    GetterUtil.getInteger(
374                            PropsUtil.get(PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_MAX), 5);
375    
376            private static final int
377                    _INDEX_SEARCH_QUERY_SUGGESTION_SCORES_THRESHOLD = GetterUtil.getInteger(
378                            PropsUtil.get(
379                                    PropsKeys.INDEX_SEARCH_QUERY_SUGGESTION_SCORES_THRESHOLD),
380                            50);
381    
382            private static final boolean _INDEX_SEARCH_SCORING_ENABLED =
383                    GetterUtil.getBoolean(
384                            PropsUtil.get(PropsKeys.INDEX_SEARCH_SCORING_ENABLED));
385    
386            private static final String _SELECTED_FIELD_NAMES = "selectedFieldNames";
387    
388            private final Map<String, Serializable> _attributes =
389                    new HashMap<String, Serializable>();
390    
391    }