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

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

Introduction

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

Prototype

public QueryResponse() 

Source Link

Usage

From source file:SolrUpdate.java

License:Apache License

public void getAllSolrRecords() {
    String pmid;/*from   w ww  .ja  va  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:com.frank.search.solr.SolrRealtimeGetRequest.java

License:Apache License

@Override
protected QueryResponse createResponse(SolrClient client) {
    return new QueryResponse();
}

From source file:com.idealista.solrmeter.BaseTestCase.java

License:Apache License

/**
 * Creates a query response//from   w w w.j a  v a 2s .  co  m
 * @param qTime
 * @return
 */
protected QueryResponse createQueryResponse(int qTime) {
    QueryResponse response = new QueryResponse();
    NamedList<Object> headerNamedList = new NamedList<Object>();
    headerNamedList.add("QTime", qTime);
    NamedList<Object> responseNamedList = new NamedList<Object>();
    responseNamedList.add("responseHeader", headerNamedList);

    SolrDocumentList resultsNamedList = new SolrDocumentList();
    resultsNamedList.add(new SolrDocument());
    resultsNamedList.add(new SolrDocument());
    resultsNamedList.add(new SolrDocument());
    resultsNamedList.setNumFound(10);
    responseNamedList.add("response", resultsNamedList);
    response.setResponse(responseNamedList);
    return response;
}

From source file:edu.jhu.cvrg.ceptools.main.PubMedSearch.java

License:Apache License

public void SearchSolr(int mypmid) {

    int currpmid = mypmid;

    try {//from   w w  w.ja va  2  s. c  o m

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

        SolrServer solr = new HttpSolrServer("http://localhost:8983/solr");
        String query;
        query = "pmid:" + currpmid;
        SolrQuery theq = new SolrQuery();
        theq.setQuery(query);

        QueryResponse response = new QueryResponse();
        response = solr.query(theq, org.apache.solr.client.solrj.SolrRequest.METHOD.POST);
        SolrDocumentList list = response.getResults();

        for (SolrDocument doc : list) {
            Publication currlist = new Publication();
            currlist.setTitle(doc.getFieldValue("ptitle").toString());
            currlist.setAbstract(doc.getFieldValue("abstract").toString());
            currlist.setPmid(Integer.valueOf(doc.getFieldValue("pmid").toString()));

            results.add(currlist);

        }

    } catch (Exception ex) {
        logger.info(ex);
        StringWriter stack = new StringWriter();
        ex.printStackTrace(new PrintWriter(stack));

    }
}

From source file:edu.jhu.cvrg.ceptools.main.PubMedSearch.java

License:Apache License

public void SearchSolrList(List<Integer> mypmids) {

    String querylist = "pmid:(";
    List<Publication> results2 = new ArrayList<Publication>();

    for (Integer currint : mypmids) {
        querylist += currint + " OR ";

    }//from  w w  w.  j  a v a  2s .  co m

    if (querylist.length() > 5) {
        querylist = querylist.substring(0, querylist.length() - 4);
        querylist += ")";
    }

    try {

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

        SolrServer solr = new HttpSolrServer("http://localhost:8983/solr");
        SolrQuery theq = new SolrQuery();
        theq.setQuery(querylist);
        QueryResponse response = new QueryResponse();

        response = solr.query(theq, org.apache.solr.client.solrj.SolrRequest.METHOD.POST);
        SolrDocumentList list = response.getResults();

        for (SolrDocument doc : list) {
            Publication currlist = new Publication();
            solrindex = 0;
            currlist.setTitle(doc.getFieldValue("ptitle").toString());
            currlist.setAbstract(doc.getFieldValue("abstract").toString());
            currlist.setPmid(Integer.valueOf(doc.getFieldValue("pmid").toString()));
            currlist.setCompleted(Boolean.valueOf(doc.getFieldValue("completion").toString()));
            if (doc.getFieldValue("lruid") != null) {
                currlist.setUserid(Integer.valueOf(doc.getFieldValue("lruid").toString()));
            } else {
                currlist.setUserid(0);
            }
            if (doc.getFieldValues("pfileinfo") != null) {

                Collection<Object> currcoll = doc.getFieldValues("pfileinfo");

                for (Object currobj : currcoll) {
                    currlist = convertStore(String.valueOf(currobj), currlist);
                }

            }

            results2.add(currlist);

        }

        int currcounter = 0;
        for (Publication solrmatch : results2) {
            currcounter = 0;
            for (Publication publistrecord : publications) {
                if (solrmatch.getPmid() == publistrecord.getPmid()) {

                    publications.get(currcounter).setExists(true);
                    publications.get(currcounter).setCompleted(solrmatch.getCompleted());
                    publications.get(currcounter).setFstorefiles(solrmatch.getFstorefiles());
                    publications.get(currcounter).setUserid(solrmatch.getUserid());

                }
                currcounter++;
            }

        }

    } catch (Exception ex) {
        logger.error("Error : ", ex);

    }
}

From source file:edu.jhu.cvrg.ceptools.main.SearchPubs.java

License:Apache License

@SuppressWarnings("unchecked")
public void SearchSolr() {

    try {/*from  www  . j  av  a2  s . c  o  m*/

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

        SolrServer solr = new HttpSolrServer("http://localhost:8983/solr");
        String query;
        query = "collector:" + searchentry;

        SolrQuery theq = new SolrQuery();
        theq.setQuery(query);
        theq.setRows(1000);

        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 = "";

            currlist.setTitle(doc.getFieldValue("ptitle").toString());

            currlist.setAbstract(doc.getFieldValue("abstract").toString());
            currlist.setPmid(Integer.valueOf(doc.getFieldValue("pmid").toString()));

            pmid = String.valueOf(currlist.getPmid());

            if (doc.getFieldValue("lruid") != null) {
                currlist.setUserid(doc.getFieldValue("lruid").toString());
            } else {
                currlist.setUserid("");
            }

            if (doc.getFieldValue("journalname") != null) {
                currlist.setJournalname(doc.getFieldValue("journalname").toString());
            }
            if (doc.getFieldValue("completion") != null) {
                currlist.setCompleted(Boolean.valueOf(doc.getFieldValue("completion").toString()));
            } else {
                currlist.setCompleted(false);
            }
            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("publication_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) {
                    convertStore(String.valueOf(currobj), currlist);
                }

            }

            List<String> fieldValue = (List<String>) doc.getFieldValue("author_firstname");
            currlist.setFauthors(fieldValue);
            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());
            }
            if (doc.getFieldValue("author_fullname_list") != null) {

                currlist.setAuthorfull(doc.getFieldValue("author_fullname_list").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);

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

    } catch (Exception ex) {
        logger.info(ex);
        StringWriter stack = new StringWriter();
        ex.printStackTrace(new PrintWriter(stack));

    }
}

From source file:eu.prestoprime.search.Searcher.java

License:Open Source License

/**
 * Submits the SolrQuery object to SolrServer and handles sorting and
 * faceting. The portion of the results that is shown ranges from result
 * #from to #(from+resultCount)//  ww  w  . java 2 s. co  m
 * 
 * @param query
 *            SolrQuery object
 * @param from
 *            specifies which portion of all results is shown
 * @param resultCount
 *            specifies how many results are displayed
 * @param sortField
 *            the field that is used for sorting
 * @param sortAsc
 *            sortorder, if true -> ascending, else -> descending
 * @param facetFilters
 *            Map object containing all fields to facet on as key and, if
 *            already selected, the filter value for this field
 * @return a SearchResults object containing all POJOs from the
 *         QueryResponse
 */
private SearchResults search(SolrQuery query, final int from, final int resultCount,
        final P4SortField sortField, final boolean sortAsc, Map<Schema.P4FacetField, String> facetFilters) {

    QueryResponse response = new QueryResponse();
    SearchResults results = new SearchResults();

    SolrQueryUtils.setResultRange(query, from, resultCount);

    // TODO deal with sorting on multivalued fields -> copyfield of one
    // value to a dedicated sortfield is done in P4IndexObject's respective
    // setters
    if (sortField == null) {
        LOGGER.debug("SortField is NULL! setting sortTitle and descending");
        SolrQueryUtils.setSortField(query, P4SortField.TITLE, sortAsc);
    } else {
        LOGGER.debug("SortField is " + sortField.getFieldName() + ". SortAsc? " + sortAsc);
        SolrQueryUtils.setSortField(query, sortField, sortAsc);
    }

    if (facetFilters == null) {
        SolrQueryUtils.enableFacets(query);
        facetFilters = SolrQueryUtils.getDefaultFacetMap();
    } else {
        SolrQueryUtils.setFacets(query, facetFilters);
    }

    try {
        LOGGER.debug("Query = " + query.getQuery());

        response = SolrServerConnection.getInstance().getSolrServer().query(query);

        results = ResultProcessor.extractResults(response, facetFilters);
        // ResultProcessor.setSelectedFacets(results, facetFilters);

        // set query params in results object
        results.getParams().setQuery(query.getQuery());
        results.getParams().setFrom(from);
        results.getParams().setResultCount(resultCount);
        results.getParams()
                .setSortField(sortField != null ? sortField.getFieldName() : P4SortField.TITLE.getFieldName());
        results.getParams().setSortAscending(sortAsc);

    } catch (SolrServerException e) {
        LOGGER.fatal(e.getMessage());
        LOGGER.fatal("Invalid Query = '" + query.getQuery() + "'.");
        results.setErrorMessage(e.getMessage());
    }

    return results;
}

From source file:eu.prestoprime.search.Searcher.java

License:Open Source License

/**
 * Queries Solr for auto-complete suggestions for an entered term. See
 * SearchHandler "suggest" in solrConfig.xml for tweaking.
 * /*from   ww  w .ja  v a 2 s  .  c o  m*/
 * @param term
 * @return
 */
public P4Suggestions getSuggestion(String term) {
    P4Suggestions suggs = new P4Suggestions(term);
    QueryResponse response = new QueryResponse();
    SolrQuery query = new SolrQuery(term);
    query.setQueryType("/suggest");

    try {
        response = SolrServerConnection.getInstance().getSolrServer().query(query);
        if (response.getSpellCheckResponse() != null
                && !response.getSpellCheckResponse().getSuggestionMap().entrySet().isEmpty()) {
            Map<String, Suggestion> resultMap = response.getSpellCheckResponse().getSuggestionMap();
            for (Entry<String, Suggestion> entry : resultMap.entrySet()) {
                suggs.suggestions.add(new P4Suggestion(entry.getKey(), entry.getValue().getAlternatives()));
            }
        }
    } catch (SolrServerException e) {
        LOGGER.fatal(e);
        LOGGER.fatal("Could not query Solr for suggestions. Query = '" + query.getQuery() + "'.");
    }
    return suggs;
}

From source file:net.yacy.cora.federate.solr.connector.MirrorSolrConnector.java

License:Open Source License

@Override
public QueryResponse getResponseByParams(ModifiableSolrParams query) throws IOException, SolrException {
    Integer count0 = query.getInt(CommonParams.ROWS);
    int count = count0 == null ? 10 : count0.intValue();
    Integer start0 = query.getInt(CommonParams.START);
    int start = start0 == null ? 0 : start0.intValue();
    if (this.solr0 == null && this.solr1 == null)
        return new QueryResponse();

    if (this.solr0 != null && this.solr1 == null) {
        QueryResponse list = this.solr0.getResponseByParams(query);
        return list;
    }//from  w  w w.  j  a v a 2s.com
    if (this.solr1 != null && this.solr0 == null) {
        QueryResponse list = this.solr1.getResponseByParams(query);
        return list;
    }

    // combine both lists
    QueryResponse rsp = this.solr0.getResponseByParams(query);
    final SolrDocumentList l = rsp.getResults();
    if (l.size() >= count)
        return rsp;

    // at this point we need to know how many results are in solr0
    // compute this with a very bad hack; replace with better method later
    int size0 = 0;
    { //bad hack - TODO: replace
        query.set(CommonParams.START, 0);
        query.set(CommonParams.ROWS, Integer.MAX_VALUE);
        QueryResponse lHack = this.solr0.getResponseByParams(query);
        query.set(CommonParams.START, start);
        query.set(CommonParams.ROWS, count);
        size0 = lHack.getResults().size();
    }

    // now use the size of the first query to do a second query
    query.set(CommonParams.START, start + l.size() - size0);
    query.set(CommonParams.ROWS, count - l.size());
    QueryResponse rsp1 = this.solr1.getResponseByParams(query);
    query.set(CommonParams.START, start);
    query.set(CommonParams.ROWS, count);
    // TODO: combine both
    return rsp1;
}

From source file:net.yacy.cora.federate.solr.instance.ResponseAccumulator.java

License:Open Source License

public static QueryResponse combineResponses(Collection<QueryResponse> qrl) {
    ResponseAccumulator acc = new ResponseAccumulator();
    for (final QueryResponse rsp : qrl) {
        NamedList<Object> response = rsp.getResponse();
        acc.addResponse(response);/*from   w  ww . j  a va2  s.  com*/
    }

    // prepare combined response
    QueryResponse rspAcc = new QueryResponse();
    rspAcc.setResponse(acc.getAccumulatedResponse());
    return rspAcc;
}