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.hits;
016    
017    import com.liferay.portal.kernel.search.Hits;
018    import com.liferay.portal.kernel.search.QueryConfig;
019    import com.liferay.portal.kernel.search.SearchContext;
020    import com.liferay.portal.kernel.search.SearchEngineUtil;
021    import com.liferay.portal.kernel.search.SearchException;
022    import com.liferay.portal.kernel.search.suggest.SuggestionConstants;
023    
024    import java.util.Locale;
025    
026    /**
027     * @author Michael C. Han
028     * @author Josef Sustacek
029     */
030    public class QueryIndexingHitsProcessor implements HitsProcessor {
031    
032            @Override
033            public boolean process(SearchContext searchContext, Hits hits)
034                    throws SearchException {
035    
036                    QueryConfig queryConfig = searchContext.getQueryConfig();
037    
038                    if (!queryConfig.isQueryIndexingEnabled()) {
039                            return true;
040                    }
041    
042                    if (hits.getLength() >= queryConfig.getQueryIndexingThreshold()) {
043                            addDocument(
044                                    searchContext.getCompanyId(), searchContext.getKeywords(),
045                                    searchContext.getLocale());
046                    }
047    
048                    return true;
049            }
050    
051            protected void addDocument(long companyId, String keywords, Locale locale)
052                    throws SearchException {
053    
054                    SearchEngineUtil.indexKeyword(
055                            companyId, keywords, 0, SuggestionConstants.TYPE_QUERY_SUGGESTION,
056                            locale);
057            }
058    
059    }