Example usage for org.apache.solr.client.solrj.response QueryResponse getResults

List of usage examples for org.apache.solr.client.solrj.response QueryResponse getResults

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj.response QueryResponse getResults.

Prototype

public SolrDocumentList getResults() 

Source Link

Usage

From source file:SolrUpdate.java

License:Apache License

public void getAllSolrRecords() {
    String pmid;/*from  w  w w .  java  2  s.c o m*/
    try {

        CoreAdminRequest adminRequest = new CoreAdminRequest();
        adminRequest.setAction(CoreAdminAction.RELOAD);

        SolrServer solr = new HttpSolrServer("http://localhost:8983/solr");

        String query;

        query = "pmid:*";

        SolrQuery theq = new SolrQuery();
        theq.setQuery(query);
        theq.setStart(0);
        theq.setRows(10000);

        QueryResponse response = new QueryResponse();

        response = solr.query(theq);

        SolrDocumentList list = response.getResults();

        int docnum = 1;
        for (SolrDocument doc : list) {
            Publication currlist = new Publication();

            List<String> fullnames = new ArrayList<String>();
            String currepubsum1 = "", currepubsum2 = "";

            if (doc.getFieldValue("abstract") != null) {
                currlist.setAbstract(doc.getFieldValue("abstract").toString());
            }
            if (doc.getFieldValue("ptitle") != null) {
                currlist.setTitle(doc.getFieldValue("ptitle").toString());
            }
            if (doc.getFieldValue("author_fullname_list") != null) {
                currlist.setFirst5authors(doc.getFieldValue("author_fullname_list").toString());
            }
            if (doc.getFieldValue("pmid") != null) {
                currlist.setPmid(Integer.valueOf(doc.getFieldValue("pmid").toString()));
            }
            if (doc.getFieldValue("completion") != null) {
                currlist.setCompletion(Boolean.valueOf(doc.getFieldValue("completion").toString()));
            }
            if (doc.getFieldValue("lruid") != null) {
                currlist.setLruid(doc.getFieldValue("lruid").toString());
            }
            if (doc.getFieldValue("draftpoint") != null) {
                currlist.setDraftpoint(Integer.valueOf(doc.getFieldValue("draftpoint").toString()));
            }

            if (doc.getFieldValue("journalname") != null) {
                currlist.setJournalname(doc.getFieldValue("journalname").toString());
            }

            if (doc.getFieldValue("journalyear") != null) {
                currlist.setJournalyear(doc.getFieldValue("journalyear").toString());
            }
            if (doc.getFieldValue("journalday") != null) {
                currlist.setJournalday(doc.getFieldValue("journalday").toString());
            }
            if (doc.getFieldValue("journalmonth") != null) {
                currlist.setJournalmonth(doc.getFieldValue("journalmonth").toString());
            }
            if (doc.getFieldValue("journalpage") != null) {
                currlist.setJournalstartpg(doc.getFieldValue("journalpage").toString());
            }
            if (doc.getFieldValue("journalissue") != null) {
                currlist.setJournalissue(doc.getFieldValue("journalissue").toString());
            }
            if (doc.getFieldValue("journalvolume") != null) {
                currlist.setJournalvolume(doc.getFieldValue("journalvolume").toString());
            }
            if (doc.getFieldValue("publicationdate_year") != null) {
                currlist.setYear(doc.getFieldValue("publicationdate_year").toString());
            }
            if (doc.getFieldValue("doi") != null) {
                currlist.setDoi(doc.getFieldValue("doi").toString());
            }

            if (doc.getFieldValues("pfileinfo") != null) {

                Collection<Object> currcoll = doc.getFieldValues("pfileinfo");
                for (Object currobj : currcoll) {
                    currlist.getFilesanddata().add(currobj.toString());
                }

            }
            if (doc.getFieldValue("author_firstname") != null) {
                currlist.setFauthors((List<String>) doc.getFieldValue("author_firstname"));
            }
            if (doc.getFieldValue("author_lastname") != null) {
                currlist.setLauthors((List<String>) doc.getFieldValue("author_lastname"));
            }

            if (doc.getFieldValue("epubmonth") != null) {
                currlist.setEpubmonth(doc.getFieldValue("epubmonth").toString());
            }

            if (doc.getFieldValue("epubyear") != null) {
                currlist.setEpubyear(doc.getFieldValue("epubyear").toString());
            }

            if (doc.getFieldValue("epubday") != null) {
                currlist.setEpubday(doc.getFieldValue("epubday").toString());
            }

            int counter = 0;

            for (String currstring : currlist.getFauthors()) {
                currstring += " " + currlist.getLauthors().get(counter);
                fullnames.add(currstring);
                counter++;
            }

            currlist.setFullnames(fullnames);

            if (currlist.getJournalvolume().length() > 0) {
                currepubsum2 += currlist.getJournalvolume();
            }

            if (currlist.getJournalissue().length() > 0) {
                currepubsum2 += "(" + currlist.getJournalissue() + ")" + ":";
            }

            if (currlist.getJournalstartpg().length() > 0) {
                currepubsum2 += currlist.getJournalstartpg() + ".";
            }

            if (currlist.getEpubday().length() < 1 && currlist.getEpubmonth().length() < 1
                    && currlist.getEpubyear().length() < 1) {
                currepubsum1 = "[Epub ahead of print]";
            } else if (currlist.getEpubyear().length() > 0) {
                currepubsum1 = "Epub " + currlist.getEpubyear() + " " + currlist.getEpubmonth() + " "
                        + currlist.getEpubday();
            } else {
                currepubsum1 = "";
            }

            currlist.setEpubsum(currepubsum1);
            currlist.setEpubsum2(currepubsum2);
            currlist.setIndex(docnum);

            if (currlist.getCompletion() == false) {
                currlist.setComp("Hidden");
            } else {
                currlist.setComp("Visible");
            }

            solrrecords.add(currlist);
            docnum++;
        }

    } catch (Exception ex) {
        System.out.println(ex);

    }

    System.out.println("There are a total of this many records gathered: " + solrrecords.size());
}

From source file:QueryLoop.java

License:GNU General Public License

@Override
public void run() {
    Random ran = new Random();
    long querycount = 1;
    long resultcount = 1;
    long oldresultcount = 1;
    long time = System.currentTimeMillis();
    double timesum = 0;
    double docsum = 0;
    int seccount = 0;
    System.out.println("Starting");

    BufferedWriter out = null;/*from   ww  w. j ava 2s .  c o m*/

    try {
        InetAddress addr = InetAddress.getLocalHost();
        String hostname = addr.getHostName();
        String fname = date + "_" + hostname.toString() + "_N" + node_num + "_S" + shard_num + "_Z" + zk_num
                + "_T" + thread_num + ".log";
        System.out.println(fname);

        FileWriter fstream = new FileWriter(fname, false);
        out = new BufferedWriter(fstream);
        while (true) {

            SolrQuery query = new SolrQuery();
            query.setQuery(qs[ran.nextInt(len)]);
            //if we want to restrict other fields
            //         query.setQuery("test sony");
            //query.addFilterQuery("cat:electronics","store:amazon.com");
            //         query.addFilterQuery("blogurl:"+"http\\" + "://www.bloomberg.com");
            //         query.setFields("id","blogurl","url","published");
            //         query.set("defType", "edismax");
            //         query.setStart(0);    

            //         System.out.println(query.toString());
            SolrDocumentList results = null;
            QueryResponse response = solr_node.query(query);
            results = response.getResults();
            querycount++;
            for (int i = 0; i < results.size(); ++i) {
                //              System.out.println(results.get(i));
                resultcount++;
            }

            if ((querycount % 1000) == 1) {
                long newtime = System.currentTimeMillis();
                System.out.println("ThreadID: " + thread_num + ", 1000 Queries, Time passed: "
                        + (newtime - time) + ", #doc results: " + (resultcount - oldresultcount));
                System.out.println(query);
                //            System.out.println(results);
                out.write("ThreadID: " + thread_num + ", 1000 Queries, Time passed: " + (newtime - time)
                        + ", #doc results: " + (resultcount - oldresultcount) + "\n");
                out.flush();
                timesum += newtime - time;
                docsum += (resultcount - oldresultcount);

                time = newtime;
                oldresultcount = resultcount;
                seccount++;
                if (seccount >= 60) {
                    out.write("Average Time:" + timesum / seccount + "\n");
                    out.flush();
                    System.out.println(("Average Time:" + timesum / seccount + ", Average Docs: "
                            + docsum / seccount + "\n"));
                    break;
                }
            }

        } //while

    } catch (Exception e) {
        e.printStackTrace();
        System.exit(-1);
    }
    try {
        if (out != null) {
            out.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:argendata.service.impl.AppServiceImpl.java

private List<App> searchApp(String myTerms, List<String> mySortByFields, List<String> myKeywords)
        throws MalformedURLException, SolrServerException {

    QueryResponse queryRsp = searchQueryApp(myTerms, mySortByFields, myKeywords);

    SolrDocumentList docs = queryRsp.getResults();
    List<SolrDocument> list = docs.subList(0, docs.size());
    List<App> resp = new ArrayList<App>();

    for (SolrDocument sd : list) {

        App anApp = this.getApprovedAppByQName("Semanticapp:" + (String) sd.getFieldValue("titleId"));

        resp.add(anApp);//  w w w . j  a  v  a 2s .c o  m
    }

    return resp;

}

From source file:argendata.service.impl.DatasetServiceImpl.java

private List<Dataset> getDatasets(QueryResponse rsp) {

    List<Dataset> datasets = new ArrayList<Dataset>();
    SolrDocumentList docs = rsp.getResults();

    List<SolrDocument> list = docs.subList(0, docs.size());

    Set<String> keywords;
    for (SolrDocument sd : list) {
        keywords = new HashSet<String>();
        Object obj = sd.getFieldValue("keyword");
        if (obj instanceof String) {
            keywords.add((String) obj);
        } else//from  w w w  .java 2 s . co m
            keywords.addAll((List<String>) obj);

        datasets.add(new Dataset((String) (sd.getFieldValue("title")), (String) (sd.getFieldValue("license")),
                keywords, (String) (sd.getFieldValue("dataQuality")),
                (String) ((Date) (sd.getFieldValue("modified"))).toString(),
                (String) (sd.getFieldValue("spatial")), (String) (sd.getFieldValue("temporal")),
                (String) (sd.getFieldValue("location")), (String) (sd.getFieldValue("publisher")),
                (String) (sd.getFieldValue("resourceType")), (String) (sd.getFieldValue("titleId"))));
    }

    return datasets;
}

From source file:at.pagu.soldockr.core.SolrTemplate.java

License:Apache License

@Override
public <T> T executeObjectQuery(Query query, Class<T> clazz) {
    Assert.notNull(query, "Query must not be 'null'.");
    Assert.notNull(clazz, "Target class must not be 'null'.");

    query.setPageRequest(new PageRequest(0, 1));
    QueryResponse response = executeQuery(query);

    if (response.getResults().size() > 0) {
        if (response.getResults().size() > 1) {
            LOGGER.warn(/*ww  w . j  av a2 s . c om*/
                    "More than 1 result found for singe result query ('{}'), returning first entry in list");
        }
        return response.getBeans(clazz).get(0);
    }
    return null;
}

From source file:at.pagu.soldockr.core.SolrTemplate.java

License:Apache License

public <T> Page<T> executeListQuery(Query query, Class<T> clazz) {
    Assert.notNull(query, "Query must not be 'null'.");
    Assert.notNull(clazz, "Target class must not be 'null'.");

    QueryResponse response = executeQuery(query);
    // TODO: implement the following for grouping results
    // if (query.hasGroupBy() && query.getGroupBy().size() > 1) {
    // return SolrResultHelper.flattenGroupedQueryResult(query, response, clazz, getSolrServer().getBinder());
    // }/* w  w  w .j a v  a 2s . co  m*/
    return new PageImpl<T>(response.getBeans(clazz), query.getPageRequest(),
            response.getResults().getNumFound());
}

From source file:at.pagu.soldockr.core.SolrTemplate.java

License:Apache License

@Override
public <T> FacetPage<T> executeFacetQuery(FacetQuery query, Class<T> clazz) {
    Assert.notNull(query, "Query must not be 'null'.");
    Assert.notNull(clazz, "Target class must not be 'null'.");

    QueryResponse response = executeQuery(query);

    FacetPage<T> page = new FacetPage<T>(response.getBeans(clazz), query.getPageRequest(),
            response.getResults().getNumFound());
    page.addAllFacetResultPages(ResultHelper.convertFacetQueryResponseToFacetPageMap(query, response));

    return page;//from  w  w  w.j  ava 2s.c o  m
}

From source file:at.pagu.soldockr.core.SolrTemplateTest.java

License:Apache License

@Test
public void testCount() throws SolrServerException {
    ArgumentCaptor<SolrQuery> captor = ArgumentCaptor.forClass(SolrQuery.class);
    QueryResponse responseMock = Mockito.mock(QueryResponse.class);
    SolrDocumentList resultList = new SolrDocumentList();
    resultList.setNumFound(10);/*from   w w  w.j  ava 2  s  .c  o  m*/
    Mockito.when(responseMock.getResults()).thenReturn(resultList);
    Mockito.when(solrServerMock.query(Mockito.any(SolrQuery.class))).thenReturn(responseMock);

    long result = solrTemplate.executeCount(new SimpleQuery(new Criteria("field_1").is("value1")));
    Assert.assertEquals(resultList.getNumFound(), result);

    Mockito.verify(solrServerMock, Mockito.times(1)).query(captor.capture());

    Assert.assertEquals(Integer.valueOf(0), captor.getValue().getStart());
    Assert.assertEquals(Integer.valueOf(0), captor.getValue().getRows());
}

From source file:at.pagu.soldockr.core.SolrTemplateTest.java

License:Apache License

@Test
public void testCountWhenPagingSet() throws SolrServerException {
    ArgumentCaptor<SolrQuery> captor = ArgumentCaptor.forClass(SolrQuery.class);
    QueryResponse responseMock = Mockito.mock(QueryResponse.class);
    SolrDocumentList resultList = new SolrDocumentList();
    resultList.setNumFound(10);/*from w ww.ja  va 2s . c om*/
    Mockito.when(responseMock.getResults()).thenReturn(resultList);
    Mockito.when(solrServerMock.query(Mockito.any(SolrQuery.class))).thenReturn(responseMock);

    Query query = new SimpleQuery(new Criteria("field_1").is("value1"));
    query.setPageRequest(new PageRequest(0, 5));
    long result = solrTemplate.executeCount(query);
    Assert.assertEquals(resultList.getNumFound(), result);

    Mockito.verify(solrServerMock, Mockito.times(1)).query(captor.capture());

    Assert.assertEquals(Integer.valueOf(0), captor.getValue().getStart());
    Assert.assertEquals(Integer.valueOf(0), captor.getValue().getRows());
}

From source file:at.tugraz.kmi.medokyservice.rec.io.SolrDBClient.java

License:Open Source License

public List<ContentTags> getContentTagsByUser(String username) throws SolrServerException {

    List<ContentTags> tagList = new LinkedList<ContentTags>();

    SolrQuery query = new SolrQuery("username:" + username).setFields("tags, timestamp")
            .setFilterQueries("tags:['' TO *]").setRows(10000000);

    QueryResponse rsp = this.query(query);

    SolrDocumentList docs = rsp.getResults();

    for (SolrDocument doc : docs) {
        Date date = (Date) doc.getFieldValue("timestamp");
        if (date == null || doc.getFieldValue("tags") == null)
            continue;

        ContentTags tagEntry = new ContentTags(date);
        tagEntry.tags = ((ArrayList<String>) doc.getFieldValue("tags"));
        tagList.add(tagEntry);/*from  w w w  . j  a  va2  s  .  co  m*/
    }

    return tagList;
}