Search code examples
javaarrayssolrjettysolrj

solrj error: array out of bounds


hi im trying to run this code and i can seem to figure out what is wrong..sorry im new to 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]);

    }
}

im having this error

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

and it is pointing in:

solrj.query(args[0]);

i also have an additional question cause before im having this output whenever i run a sample program in solrj

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

can you tell me what this means and the error about array out of bounds? thanks in advance really need this badly and dont know much about solrj


Solution

  • The reason is you are not supplying any runtime parameters while program execution starts.

    In solrj.query(args[0]); , args refers to the parameters passed to the program like:

    java MyClass abc def
    

    So here in the above case args will be :

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

    So make sure you are passing proper parameters while executing your program.