de.fhg.iais.cortex.nodestore.searcher.NodeSearcherImpl.java Source code

Java tutorial

Introduction

Here is the source code for de.fhg.iais.cortex.nodestore.searcher.NodeSearcherImpl.java

Source

package de.fhg.iais.cortex.nodestore.searcher;

/******************************************************************************
 * Copyright 2011 (c) Fraunhofer IAIS Netmedia  http://www.iais.fraunhofer.de *
 * ************************************************************************** *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may    *
 * not use this file except in compliance with the License.                   *
 * You may obtain a copy of the License at                                    *
 * http://www.apache.org/licenses/LICENSE-2.0                                 *
 * Unless required by applicable law or agreed to in writing,                 *
 * software distributed under the License is distributed on an "AS IS" BASIS, *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
 * See the License for the specific language governing permissions and        *
 * limitations under the License.                                             *
 ******************************************************************************/

import javax.inject.Inject;
import javax.inject.Named;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.SortClause;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Strings;

import de.fhg.iais.commons.annotation.UsedBy;
import de.fhg.iais.cortex.nodestore.NodeStoreException;
import de.fhg.iais.cortex.search.types.Query;

/**
 * Saves the hierarchy in a solr index.
 */
@UsedBy("guice")
public class NodeSearcherImpl implements INodeSearcher {

    private static final Logger LOG = LoggerFactory.getLogger(NodeSearcherImpl.class);

    protected SolrServer solrServer;

    @Inject
    public NodeSearcherImpl(@Named("nodes.search") SolrServer solrServer) {
        this.solrServer = solrServer;
    }

    @Override
    public SolrDocumentList getHierarchyNodes(String ingestId, String providerId) {
        StringBuilder querySb = new StringBuilder(INGEST_ID + ":\"" + ingestId + "\" AND " + PROVIDER_ID + ":");
        if ("*".equals(providerId)) {
            querySb.append(providerId);
        } else {
            querySb.append("\"" + providerId + "\"");
        }
        SolrQuery query = new SolrQuery(querySb.toString());
        query.setSort(SortClause.create(POSITION, SolrQuery.ORDER.asc));
        query.setStart(0);
        query.setRows(Integer.MAX_VALUE);

        try {
            QueryResponse response = this.solrServer.query(query);
            return response.getResults();
        } catch (SolrServerException e) {
            throw new NodeStoreException(
                    "Could not retrieve nodes for ingest " + ingestId + " and provider " + providerId, e);
        }
    }

    @Override
    public SolrDocument getHierarchyNode(String id) {
        SolrQuery parentQuery = new SolrQuery(ID + ":\"" + id + "\"");
        parentQuery.setRows(1);
        try {
            QueryResponse response = this.solrServer.query(parentQuery);
            SolrDocumentList results = response.getResults();
            if (results.size() > 1) {
                LOG.error("Found more than 1 parent for the node with id {}", id);
                throw new NodeStoreException("Found more than 1 parent for the node with id " + id);
            } else if (results.isEmpty()) {
                return null;
            } else {
                SolrDocument document = results.get(0);
                return document;
            }
        } catch (SolrServerException e) {
            throw new NodeStoreException("Could not retrieve the node with id " + id, e);
        }
    }

    @Override
    public SolrDocumentList getHierarchyChildren(String id, Query filterQuery) {
        SolrQuery childQuery = new SolrQuery(PARENT + ":\"" + id + "\"");
        if (!Strings.isNullOrEmpty(filterQuery.getQuery()) && !filterQuery.getQuery().equals(Query.ALL_QUERY)) {
            childQuery.addFilterQuery(filterQuery.getQuery());
        }
        childQuery.setStart(filterQuery.getOffset());
        childQuery.setRows(filterQuery.getRows());
        childQuery.setSort(SortClause.create(POSITION, SolrQuery.ORDER.asc));

        try {
            QueryResponse response = this.solrServer.query(childQuery);
            return response.getResults();
        } catch (SolrServerException e) {
            throw new NodeStoreException("Could not retrieve the node with id " + id, e);
        }
    }

    @Override
    public long hasChildren(String id) {
        SolrQuery childQuery = new SolrQuery(PARENT + ":\"" + id + "\"");
        childQuery.setStart(0);
        childQuery.setRows(1);

        try {
            QueryResponse response = this.solrServer.query(childQuery);
            return response.getResults().getNumFound();
        } catch (SolrServerException e) {
            throw new NodeStoreException("Could not determine the number of children for the node with id " + id,
                    e);
        }
    }

}