0

I am thinking that how do I change the following code, so I can store the query from Solr into an ArrayList?

public static void main(String[] args) throws MalformedURLException, SolrServerException {
    HttpSolrServer solr = new HttpSolrServer("http://localhost:8983/solr");

    Scanner sc = new Scanner(System.in);
    System.out.println("What are you looking for: ")     
    String look = sc.nextLine();   

    SolrQuery query = new SolrQuery();
    query.setQuery(look);
    query.setFields("id");
    query.setStart(0);    


    QueryResponse response = solr.query(query);
    SolrDocumentList results = response.getResults();
    for (int i = 0; i < results.size(); ++i) {
      System.out.println(results.get(i));
    }
  }
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Pablo Java
  • 11
  • 2
  • do you know how to use arrayList? – Shreyas Sarvothama Oct 26 '16 at 03:45
  • 1
    I assume you want to store the query's result rather than the query itself, but why would you want that? `SolrDocumentList` extends `ArrayList` – Aaron Oct 26 '16 at 03:45
  • I am thinking that, so later on I can print out the result line by line in JSP. @Aaron – Pablo Java Oct 26 '16 at 03:55
  • 1
    You should be able to : since `SolrDocumentList` extends `ArrayList`, you can use every method you could use with an `ArrayList`. – Aaron Oct 26 '16 at 04:02
  • You may also create a small POJO that models your response and use `response.getBeans(...)`, this is described here http://stackoverflow.com/questions/19114554/retrieve-object-from-solr – cheffe Oct 26 '16 at 05:30
  • 1
    So now you've asked the same question (and variations thereof) five times. – MatsLindh Oct 26 '16 at 07:58
  • I am just trying any method that may work. And I deleted those question that I didn't phrase properly, I am just trying to learn. Peace. @MatsLindh – Pablo Java Oct 27 '16 at 02:37

1 Answers1

0

you are forgetting the important part.. put the info in the list

lets suppose that response.getResults() returns an object holding Strings then you need a list of Strings :)

List<String> myList = new ArrayList();
SolrDocumentList results = response.getResults();
    for (int i = 0; i < results.size(); ++i) {
      System.out.println(results.get(i));
      myList.add(results.get(i));
    }
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97