solrbook.ch11.solrj.cli.command.SearchCommand.java Source code

Java tutorial

Introduction

Here is the source code for solrbook.ch11.solrj.cli.command.SearchCommand.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */
package solrbook.ch11.solrj.cli.command;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;

/**
 * SearchCommand ?????? ?????
 */
public class SearchCommand extends Command {
    public static final String NAME = "search";
    public static final String HELP = "? Solr ??";

    /*
     * -q, --query-string ?
     */
    public static final String[] ARG_QUERY_STRING_FLAGS = { "-q", "--query-string" };
    public static final String[] ARG_QUERY_STRING_METAVAR = { "QUERY_STRING" };
    public static final String ARG_QUERY_STRING_DEST = "queryString";
    public static final String ARG_QUERY_STRING_DEFAULT = null;
    public static final String ARG_QUERY_STRING_HELP = "Solr ?";

    /*
     * -s, --start ?
     */
    public static final String[] ARG_START_FLAGS = { "-s", "--start" };
    public static final String[] ARG_START_METAVAR = { "START" };
    public static final String ARG_START_DEST = "start";
    public static final Integer ARG_START_DEFAULT = 0;
    public static final String ARG_START_HELP = "?????";

    /*
     * -r, --rows ?
     */
    public static final String[] ARG_ROWS_FLAGS = { "-r", "--rows" };
    public static final String[] ARG_ROWS_METAVAR = { "ROWS" };
    public static final String ARG_ROWS_DEST = "rows";
    public static final Integer ARG_ROWS_DEFAULT = 10;
    public static final String ARG_ROWS_HELP = "????";

    /*
     * -S, --sort-field ?
     */
    public static final String[] ARG_SORT_FIELD_FLAGS = { "-S", "--sort-field" };
    public static final String[] ARG_SORT_FIELD_METAVAR = { "SORT_FIELD" };
    public static final String ARG_SORT_FIELD_DEST = "sortField";
    public static final String ARG_SORT_FIELD_DEFAULT = null;
    public static final String ARG_SORT_FIELD_HELP = "???";

    /*
     * -o, --sort-order ?
     */
    public static final String[] ARG_SORT_ORDER_FLAGS = { "-o", "--sort-order" };
    public static final String[] ARG_SORT_ORDER_METAVAR = { "SORT_ORDER" };
    public static final String ARG_SORT_ORDER_DEST = "sortOrder";
    public static final String ARG_SORT_ORDER_DEFAULT = null;
    public static final String ARG_SORT_ORDER_HELP = " (asc | desc)";

    /*
     * -l, --field-list ?
     */
    public static final String[] ARG_FIELD_LIST_FLAGS = { "-l", "--field-list" };
    public static final String[] ARG_FIELD_LIST_METAVAR = { "FIELD_LIST" };
    public static final String ARG_FIELD_LIST_DEST = "fieldList";
    public static final String ARG_FIELD_LIST_DEFAULT = "*,score";
    public static final String ARG_FIELD_LIST_HELP = "??????? : title,description";

    public String queryString = ARG_QUERY_STRING_DEFAULT;
    public int start = ARG_START_DEFAULT;
    public int rows = ARG_ROWS_DEFAULT;
    public String sortField = ARG_SORT_FIELD_DEFAULT;
    public String sortOrder = ARG_SORT_ORDER_DEFAULT;
    public String fieldList = ARG_FIELD_LIST_DEFAULT;

    /**
     * 
     */
    public SearchCommand() {
        super();

        /*
         * ???
         */
        name = NAME;
        help = HELP;
    }

    /*
     * (non-Javadoc)
     * 
     * @see Command#preProcess(java.util.Map)
     */
    @Override
    public void preProcess(Map<String, Object> parameters) throws Exception {
        super.preProcess(parameters);

        /*
         * 
         */
        if (parameters.containsKey(ARG_QUERY_STRING_DEST)) {
            queryString = (String) parameters.get(ARG_QUERY_STRING_DEST);
        }

        /*
         * ?????
         */
        if (parameters.containsKey(ARG_START_DEST)) {
            start = (Integer) parameters.get(ARG_START_DEST);
        }

        /*
         * ????
         */
        if (parameters.containsKey(ARG_ROWS_DEST)) {
            rows = (Integer) parameters.get(ARG_ROWS_DEST);
        }

        /*
         * ???
         */
        if (parameters.containsKey(ARG_SORT_FIELD_DEST)) {
            sortField = (String) parameters.get(ARG_SORT_FIELD_DEST);
        }

        /*
         * 
         */
        if (parameters.containsKey(ARG_SORT_ORDER_DEST)) {
            sortOrder = (String) parameters.get(ARG_SORT_ORDER_DEST);
        }

        /*
         * ??
         */
        if (parameters.containsKey(ARG_FIELD_LIST_DEST)) {
            fieldList = (String) parameters.get(ARG_FIELD_LIST_DEST);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see Command#mainProcess(java.util.Map)
     */
    @Override
    public void mainProcess(Map<String, Object> parameters) throws Exception {
        try {
            /*
             * SolrQuery ?
             */
            SolrQuery solrQuery = new SolrQuery(queryString);

            /*
             * ??
             */
            solrQuery.setStart(start);
            solrQuery.setRows(rows);

            /*
             * ??????
             */
            if (StringUtils.isNotEmpty(sortField) && StringUtils.isNotEmpty(sortOrder)) {
                /*
                 * ?????
                 */
                solrQuery.setSort(sortField,
                        Enum.valueOf(org.apache.solr.client.solrj.SolrQuery.ORDER.class, sortOrder));
            }

            /*
             * ?????
             */
            for (String f : fieldList.split(",")) {
                if (StringUtils.isNotEmpty(f)) {
                    solrQuery.addField(f.trim());
                }
            }

            /*
             * SolrClient ??
             */
            QueryResponse queryResponse = solrClient.query(solrQuery);

            /*
             * ??????? List ?
             */
            List<Map<String, Object>> documentList = new LinkedList<Map<String, Object>>();

            /*
             * ???
             */
            SolrDocumentList solrDocumentList = queryResponse.getResults();

            /*
             * ???
             */
            for (SolrDocument solrDocument : solrDocumentList) {
                /*
                 * ???? Map ?
                 */
                Map<String, Object> documentMap = new HashMap<String, Object>();

                /*
                 * ???????
                 */
                for (String fieldName : solrDocument.getFieldNames()) {
                    /*
                     * ??? Map ?
                     */
                    Object fieldValue = solrDocument.getFieldValue(fieldName);
                    documentMap.put(fieldName, fieldValue);
                }

                /*
                 * ? Map ?
                 */
                documentMap.put("score", solrDocument.getFieldValue("score"));

                /*
                 * ???
                 */
                documentList.add(documentMap);
            }

            /*
             * ?????
             */
            response.put("QTime", queryResponse.getQTime());

            /*
             * ????
             */
            response.put("maxScore", solrDocumentList.getMaxScore());

            /*
             * ????
             */
            response.put("numFound", solrDocumentList.getNumFound());

            /*
             * ????
             */
            response.put("result", documentList);

            status = STATUS_SUCCESS;
            message = SUCCESS_MESSAGE;
        } catch (Exception e) {
            /*
             * ????
             */
            status = STATUS_ERROR;
            message = e.getMessage();
        }
    }

}