0

嗨,我正在尝试运行此代码,我似乎可以找出问题所在..对不起,我是 solrj 的新手

import org.apache.solr.common.SolrDocument;
import java.util.Map;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrDocument;
import java.util.Map;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.FacetField;


public class SolrjTest
{
    public void query(String q)
    {
        //CommonsHttpSolrServer server = null;
        SolrServer server = null;

        try
        {
            server = new HttpSolrServer("http://localhost:8983/solr/");
            //server = new CommonsHttpSolrServer("http://localhost:8080/solr/");
            System.out.println("connected");
        }
        catch(Exception e)
        {
            System.out.println("not connected");
            e.printStackTrace();
        }

        SolrQuery query = new SolrQuery();
        query.setQuery(q);
        query.setQueryType("camera");
        query.setFacet(true);
        query.addFacetField("name");
        query.addFacetField("locality4");
        query.setFacetMinCount(2);
        query.setIncludeScore(true);

        try
        {
            QueryResponse qr = server.query(query);

            SolrDocumentList sdl = qr.getResults();

            System.out.println("Found: " + sdl.getNumFound());
            System.out.println("Start: " + sdl.getStart());
            System.out.println("Max Score: " + sdl.getMaxScore());
            System.out.println("--------------------------------");

            ArrayList<HashMap<String, Object>> hitsOnPage = new   ArrayList<HashMap<String, Object>>();

             for(SolrDocument d : sdl)
            {
                HashMap<String, Object> values = new HashMap<String, Object>();

                for(Iterator<Map.Entry<String, Object>> i = d.iterator(); i.hasNext();        )
                {
                    Map.Entry<String, Object> e2 = i.next();

                    values.put(e2.getKey(), e2.getValue());
                }

                 hitsOnPage.add(values);
                System.out.println(values.get("displayname") + " (" +  values.get("displayphone") + ")");
            }

            List<FacetField> facets = qr.getFacetFields();

            for(FacetField facet : facets)
            {
                List<FacetField.Count> facetEntries = facet.getValues();

                for(FacetField.Count fcount : facetEntries)
                {
                    System.out.println(fcount.getName() + ": " + fcount.getCount());
                }
            }
        }
        catch (SolrServerException e)
        {
            e.printStackTrace();
        }

    }

    public static void main(String[] args)
    {
        SolrjTest solrj = new SolrjTest();
        solrj.query(args[0]);

    }
}

我有这个错误

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at SolrjTest.main(SolrjTest.java:98)

它指向:

solrj.query(args[0]);

每当我在 solrj 中运行示例程序时,在获得此输出之前,我还有一个额外的问题原因

{time=218.0,sub1={time=125.0,sub1.1={time=31.0}}}

你能告诉我这意味着什么以及关于数组越界的错误吗?在此先感谢真的非常需要这个并且对solrj不太了解

4

1 回答 1

0

原因是您在程序执行开始时没有提供任何运行时参数。

solrj.query(args[0]);中,args指的是传递给程序的参数,如:

java MyClass abc def

所以在上述情况下args将是:

args[0]   ----->   "abc"
args[1]   ----->   "def"

因此,请确保在执行程序时传递正确的参数。

于 2013-07-24T04:10:19.293 回答