com.liferay.wiki.search.WikiPageTitleSearcher.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.wiki.search.WikiPageTitleSearcher.java

Source

/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.wiki.search;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.search.BaseSearcher;
import com.liferay.portal.kernel.search.BooleanClauseOccur;
import com.liferay.portal.kernel.search.BooleanQuery;
import com.liferay.portal.kernel.search.Field;
import com.liferay.portal.kernel.search.Indexer;
import com.liferay.portal.kernel.search.Query;
import com.liferay.portal.kernel.search.SearchContext;
import com.liferay.portal.kernel.search.filter.BooleanFilter;
import com.liferay.portal.kernel.search.filter.TermsFilter;
import com.liferay.portal.kernel.search.generic.MatchQuery;
import com.liferay.portal.kernel.util.ArrayUtil;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.wiki.model.WikiPage;
import com.liferay.wiki.service.WikiNodeService;

import java.util.Collections;
import java.util.Map;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
 * @author Roberto Daz
 */
@Component(immediate = true, service = WikiPageTitleSearcher.class)
public class WikiPageTitleSearcher extends BaseSearcher {

    public static final String[] CLASS_NAMES = { WikiPage.class.getName() };

    public static Indexer<?> getInstance() {
        return new WikiPageTitleSearcher();
    }

    public WikiPageTitleSearcher() {
        setDefaultSelectedFieldNames(Field.TITLE);
        setFilterSearch(true);
        setPermissionAware(true);
    }

    @Override
    public String[] getSearchClassNames() {
        return CLASS_NAMES;
    }

    @Override
    public void postProcessContextBooleanFilter(BooleanFilter contextBooleanFilter, SearchContext searchContext)
            throws Exception {

        addStatus(contextBooleanFilter, searchContext);

        long[] nodeIds = searchContext.getNodeIds();

        if (ArrayUtil.isNotEmpty(nodeIds)) {
            TermsFilter nodesIdTermsFilter = new TermsFilter(Field.NODE_ID);

            for (long nodeId : nodeIds) {
                try {
                    _wikiNodeService.getNode(nodeId);
                } catch (Exception e) {
                    if (_log.isDebugEnabled()) {
                        _log.debug("Unable to get wiki node " + nodeId, e);
                    }

                    continue;
                }

                nodesIdTermsFilter.addValue(String.valueOf(nodeId));
            }

            if (!nodesIdTermsFilter.isEmpty()) {
                contextBooleanFilter.add(nodesIdTermsFilter, BooleanClauseOccur.MUST);
            }
        }
    }

    @Override
    public void postProcessSearchQuery(BooleanQuery searchQuery, BooleanFilter fullQueryBooleanFilter,
            SearchContext searchContext) throws Exception {

        MatchQuery matchQuery = new MatchQuery(Field.TITLE, StringUtil.toLowerCase(searchContext.getKeywords()));

        matchQuery.setType(MatchQuery.Type.PHRASE_PREFIX);

        searchQuery.add(matchQuery, BooleanClauseOccur.MUST);
    }

    protected Map<String, Query> addSearchKeywords(BooleanQuery searchQuery, SearchContext searchContext)
            throws Exception {

        return Collections.emptyMap();
    }

    @Reference(unbind = "-")
    protected void setWikiNodeService(WikiNodeService wikiNodeService) {
        _wikiNodeService = wikiNodeService;
    }

    private static final Log _log = LogFactoryUtil.getLog(WikiPageTitleSearcher.class);

    private WikiNodeService _wikiNodeService;

}