Search in sources :

Example 1 with SolrDocumentList

use of org.apache.solr.common.SolrDocumentList in project Xponents by OpenSextant.

the class SolrProxy method searchGazetteer.

/**
     * Search an OpenSextant solr gazetteer.
     *
     * @param index solr server handle
     * @param qparams search parameters
     * @return list of places
     * @throws SolrServerException on err
     */
public static List<Place> searchGazetteer(SolrServer index, SolrParams qparams) throws SolrServerException {
    QueryResponse response = index.query(qparams, SolrRequest.METHOD.GET);
    List<Place> places = new ArrayList<>();
    SolrDocumentList docList = response.getResults();
    for (SolrDocument solrDoc : docList) {
        places.add(SolrProxy.createPlace(solrDoc));
    }
    return places;
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) ArrayList(java.util.ArrayList) SolrDocumentList(org.apache.solr.common.SolrDocumentList) Place(org.opensextant.data.Place)

Example 2 with SolrDocumentList

use of org.apache.solr.common.SolrDocumentList in project lucene-solr by apache.

the class TestContentStreamDataSource method testSimple.

@Test
public void testSimple() throws Exception {
    DirectXmlRequest req = new DirectXmlRequest("/dataimport", xml);
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set("command", "full-import");
    params.set("clean", "false");
    req.setParams(params);
    try (HttpSolrClient solrClient = getHttpSolrClient(buildUrl(jetty.getLocalPort(), "/solr/collection1"))) {
        solrClient.request(req);
        ModifiableSolrParams qparams = new ModifiableSolrParams();
        qparams.add("q", "*:*");
        QueryResponse qres = solrClient.query(qparams);
        SolrDocumentList results = qres.getResults();
        assertEquals(2, results.getNumFound());
        SolrDocument doc = results.get(0);
        assertEquals("1", doc.getFieldValue("id"));
        assertEquals("Hello C1", ((List) doc.getFieldValue("desc")).get(0));
    }
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrDocumentList(org.apache.solr.common.SolrDocumentList) DirectXmlRequest(org.apache.solr.client.solrj.request.DirectXmlRequest) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) Test(org.junit.Test)

Example 3 with SolrDocumentList

use of org.apache.solr.common.SolrDocumentList in project lucene-solr by apache.

the class MockSolrEntityProcessor method buildIterator.

//@Override
//protected SolrDocumentList doQuery(int start) {
//  queryCount++;
//  return getDocs(start, rows);
// }
@Override
protected void buildIterator() {
    if (rowIterator == null || (!rowIterator.hasNext() && ((SolrDocumentListIterator) rowIterator).hasMoreRows())) {
        queryCount++;
        SolrDocumentList docs = getDocs(start, rows);
        rowIterator = new SolrDocumentListIterator(docs);
        start += docs.size();
    }
}
Also used : SolrDocumentList(org.apache.solr.common.SolrDocumentList)

Example 4 with SolrDocumentList

use of org.apache.solr.common.SolrDocumentList in project lucene-solr by apache.

the class MockSolrEntityProcessor method getDocs.

private SolrDocumentList getDocs(int start, int rows) {
    SolrDocumentList docs = new SolrDocumentList();
    docs.setNumFound(docsData.size());
    docs.setStart(start);
    int endIndex = start + rows;
    int end = docsData.size() < endIndex ? docsData.size() : endIndex;
    for (int i = start; i < end; i++) {
        SolrDocument doc = new SolrDocument();
        SolrTestCaseJ4.Doc testDoc = docsData.get(i);
        doc.addField("id", testDoc.id);
        doc.addField("description", testDoc.getValues("description"));
        docs.add(doc);
    }
    return docs;
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrTestCaseJ4(org.apache.solr.SolrTestCaseJ4)

Example 5 with SolrDocumentList

use of org.apache.solr.common.SolrDocumentList in project lucene-solr by apache.

the class QueryComponent method returnFields.

protected void returnFields(ResponseBuilder rb, ShardRequest sreq) {
    if ((sreq.purpose & ShardRequest.PURPOSE_GET_FIELDS) != 0) {
        boolean returnScores = (rb.getFieldFlags() & SolrIndexSearcher.GET_SCORES) != 0;
        String keyFieldName = rb.req.getSchema().getUniqueKeyField().getName();
        boolean removeKeyField = !rb.rsp.getReturnFields().wantsField(keyFieldName);
        if (rb.rsp.getReturnFields().getFieldRenames().get(keyFieldName) != null) {
            // if id was renamed we need to use the new name
            keyFieldName = rb.rsp.getReturnFields().getFieldRenames().get(keyFieldName);
        }
        for (ShardResponse srsp : sreq.responses) {
            if (srsp.getException() != null) {
                // Don't try to get the documents if there was an exception in the shard
                if (rb.req.getParams().getBool(ShardParams.SHARDS_INFO, false)) {
                    @SuppressWarnings("unchecked") NamedList<Object> shardInfo = (NamedList<Object>) rb.rsp.getValues().get(ShardParams.SHARDS_INFO);
                    @SuppressWarnings("unchecked") SimpleOrderedMap<Object> nl = (SimpleOrderedMap<Object>) shardInfo.get(srsp.getShard());
                    if (nl.get("error") == null) {
                        // Add the error to the shards info section if it wasn't added before
                        Throwable t = srsp.getException();
                        if (t instanceof SolrServerException) {
                            t = ((SolrServerException) t).getCause();
                        }
                        nl.add("error", t.toString());
                        StringWriter trace = new StringWriter();
                        t.printStackTrace(new PrintWriter(trace));
                        nl.add("trace", trace.toString());
                    }
                }
                continue;
            }
            SolrDocumentList docs = (SolrDocumentList) srsp.getSolrResponse().getResponse().get("response");
            for (SolrDocument doc : docs) {
                Object id = doc.getFieldValue(keyFieldName);
                ShardDoc sdoc = rb.resultIds.get(id.toString());
                if (sdoc != null) {
                    if (returnScores) {
                        doc.setField("score", sdoc.score);
                    } else {
                        // Score might have been added (in createMainQuery) to shard-requests (and therefore in shard-response-docs)
                        // Remove score if the outer request did not ask for it returned
                        doc.remove("score");
                    }
                    if (removeKeyField) {
                        doc.removeFields(keyFieldName);
                    }
                    rb.getResponseDocs().set(sdoc.positionInResponse, doc);
                }
            }
        }
    }
}
Also used : NamedList(org.apache.solr.common.util.NamedList) SolrServerException(org.apache.solr.client.solrj.SolrServerException) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) SolrDocument(org.apache.solr.common.SolrDocument) StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter)

Aggregations

SolrDocumentList (org.apache.solr.common.SolrDocumentList)263 SolrDocument (org.apache.solr.common.SolrDocument)153 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)118 SolrQuery (org.apache.solr.client.solrj.SolrQuery)83 Test (org.junit.Test)73 ArrayList (java.util.ArrayList)61 SolrServerException (org.apache.solr.client.solrj.SolrServerException)50 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)44 NamedList (org.apache.solr.common.util.NamedList)44 IOException (java.io.IOException)43 SolrParams (org.apache.solr.common.params.SolrParams)32 SolrInputDocument (org.apache.solr.common.SolrInputDocument)26 HashMap (java.util.HashMap)22 Map (java.util.Map)21 SolrClient (org.apache.solr.client.solrj.SolrClient)17 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)17 SolrException (org.apache.solr.common.SolrException)16 List (java.util.List)15 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)14 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)14