solrj错误:数组越界

时间:2013-07-24 03:57:44

标签: java arrays solr jetty solrj

嗨,我试图运行这个代码,我似乎可以弄清楚出了什么问题。我很新闻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

1 个答案:

答案 0 :(得分:0)

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

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

java MyClass abc def

所以在上面的例子中args将是:

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

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