001    /**
002     * Copyright (c) 2000-2013 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.LocaleUtil;
018    import com.liferay.portal.util.PortletKeys;
019    
020    import java.util.Locale;
021    
022    /**
023     * @author Michael C. Han
024     * @author Josef Sustacek
025     */
026    public class QueryIndexingHitsProcessor implements HitsProcessor {
027    
028            @Override
029            public boolean process(SearchContext searchContext, Hits hits)
030                    throws SearchException {
031    
032                    QueryConfig queryConfig = searchContext.getQueryConfig();
033    
034                    if (!queryConfig.isQueryIndexingEnabled()) {
035                            return true;
036                    }
037    
038                    if (hits.getLength() >= queryConfig.getQueryIndexingThreshold()) {
039                            addDocument(
040                                    searchContext.getCompanyId(), searchContext.getKeywords(),
041                                    searchContext.getLocale());
042                    }
043    
044                    return true;
045            }
046    
047            public void setDocument(Document document) {
048                    _document = document;
049            }
050    
051            protected void addDocument(long companyId, String keywords, Locale locale)
052                    throws SearchException {
053    
054                    Document document = (Document)_document.clone();
055    
056                    document.addKeyword(Field.COMPANY_ID, companyId);
057                    document.addKeyword(Field.KEYWORD_SEARCH, keywords);
058                    document.addKeyword(Field.LANGUAGE_ID, LocaleUtil.toLanguageId(locale));
059                    document.addKeyword(Field.PORTLET_ID, PortletKeys.SEARCH);
060    
061                    SearchEngineUtil.addDocument(
062                            SearchEngineUtil.getDefaultSearchEngineId(), companyId, document);
063            }
064    
065            private Document _document;
066    
067    }