Search in sources :

Example 21 with SolrQueryResponse

use of org.apache.solr.response.SolrQueryResponse in project lucene-solr by apache.

the class BasicFunctionalityTest method testLazyField.

@Test
public void testLazyField() throws IOException {
    assertU(adoc("id", "7777", "title", "keyword", "test_hlt", mkstr(10000), "test_hlt", mkstr(20000), "test_hlt", mkstr(30000), "test_hlt", mkstr(40000)));
    assertU(commit());
    SolrCore core = h.getCore();
    // initial request
    SolrQueryRequest req = req("q", "id:7777", "fl", "id,title");
    SolrQueryResponse rsp = new SolrQueryResponse();
    core.execute(core.getRequestHandler(req.getParams().get(CommonParams.QT)), req, rsp);
    DocList dl = ((ResultContext) rsp.getResponse()).getDocList();
    DocIterator di = dl.iterator();
    Document d1 = req.getSearcher().doc(di.nextDoc());
    IndexableField[] values1 = null;
    // ensure fl field is non lazy, and non-fl field is lazy
    assertFalse(d1.getField("title") instanceof LazyDocument.LazyField);
    assertFalse(d1.getField("id") instanceof LazyDocument.LazyField);
    values1 = d1.getFields("test_hlt");
    assertEquals(4, values1.length);
    for (int i = 0; i < values1.length; i++) {
        assertTrue(values1[i] instanceof LazyDocument.LazyField);
        LazyDocument.LazyField f = (LazyDocument.LazyField) values1[i];
        assertFalse(f.hasBeenLoaded());
    }
    req.close();
    // followup request, different fl
    req = req("q", "id:7777", "fl", "id,test_hlt");
    rsp = new SolrQueryResponse();
    core.execute(core.getRequestHandler(req.getParams().get(CommonParams.QT)), req, rsp);
    dl = ((ResultContext) rsp.getResponse()).getDocList();
    di = dl.iterator();
    Document d2 = req.getSearcher().doc(di.nextDoc());
    // ensure same doc, same lazy field now
    assertTrue("Doc was not cached", d1 == d2);
    IndexableField[] values2 = d2.getFields("test_hlt");
    assertEquals(values1.length, values2.length);
    for (int i = 0; i < values1.length; i++) {
        assertSame("LazyField wasn't reused", values1[i], values2[i]);
        LazyDocument.LazyField f = (LazyDocument.LazyField) values1[i];
        // still not a real boy, no response writer in play
        assertFalse(f.hasBeenLoaded());
    }
    // actuallize one value
    assertNotNull(values2[0].stringValue());
    for (int i = 0; i < values2.length; i++) {
        // now all values for this field should be loaded & cached
        LazyDocument.LazyField f = (LazyDocument.LazyField) values2[i];
        assertTrue(f.hasBeenLoaded());
    }
    req.close();
}
Also used : ResultContext(org.apache.solr.response.ResultContext) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) DocIterator(org.apache.solr.search.DocIterator) SolrCore(org.apache.solr.core.SolrCore) LazyDocument(org.apache.lucene.document.LazyDocument) Document(org.apache.lucene.document.Document) IndexableField(org.apache.lucene.index.IndexableField) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) LazyDocument(org.apache.lucene.document.LazyDocument) DocList(org.apache.solr.search.DocList) Test(org.junit.Test)

Example 22 with SolrQueryResponse

use of org.apache.solr.response.SolrQueryResponse in project lucene-solr by apache.

the class TestCrossCoreJoin method query.

public String query(SolrCore core, SolrQueryRequest req) throws Exception {
    String handler = "standard";
    if (req.getParams().get("qt") != null)
        handler = req.getParams().get("qt");
    SolrQueryResponse rsp = new SolrQueryResponse();
    SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));
    core.execute(core.getRequestHandler(handler), req, rsp);
    if (rsp.getException() != null) {
        throw rsp.getException();
    }
    StringWriter sw = new StringWriter(32000);
    QueryResponseWriter responseWriter = core.getQueryResponseWriter(req);
    responseWriter.write(sw, req, rsp);
    req.close();
    SolrRequestInfo.clearRequestInfo();
    return sw.toString();
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) StringWriter(java.io.StringWriter) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo)

Example 23 with SolrQueryResponse

use of org.apache.solr.response.SolrQueryResponse in project lucene-solr by apache.

the class TestSolrQueryResponse method testResponseHeader.

@Test
public void testResponseHeader() throws Exception {
    final SolrQueryResponse response = new SolrQueryResponse();
    assertEquals("responseHeader initially present", null, response.getResponseHeader());
    final NamedList<Object> newValue = new SimpleOrderedMap<>();
    newValue.add("key1", "value1");
    response.add("key2", "value2");
    response.addResponseHeader(newValue);
    assertEquals("responseHeader new value", newValue, response.getResponseHeader());
    response.removeResponseHeader();
    assertEquals("responseHeader removed value", null, response.getResponseHeader());
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) Test(org.junit.Test)

Example 24 with SolrQueryResponse

use of org.apache.solr.response.SolrQueryResponse in project lucene-solr by apache.

the class TestPHPSerializedResponseWriter method testSolrDocuments.

@Test
public void testSolrDocuments() throws IOException {
    SolrQueryRequest req = req("q", "*:*");
    SolrQueryResponse rsp = new SolrQueryResponse();
    QueryResponseWriter w = new PHPSerializedResponseWriter();
    StringWriter buf = new StringWriter();
    SolrDocument d = new SolrDocument();
    SolrDocument d1 = d;
    d.addField("id", "1");
    d.addField("data1", "hello");
    d.addField("data2", 42);
    d.addField("data3", true);
    // multivalued fields: 
    // extremely odd edge case: value is a map
    // we use LinkedHashMap because we are doing a string comparison 
    // later and we need predictible ordering
    LinkedHashMap<String, String> nl = new LinkedHashMap<>();
    nl.put("data4.1", "hashmap");
    nl.put("data4.2", "hello");
    d.addField("data4", nl);
    // array value 
    d.addField("data5", Arrays.asList("data5.1", "data5.2", "data5.3"));
    // adding one more document to test array indexes
    d = new SolrDocument();
    SolrDocument d2 = d;
    d.addField("id", "2");
    SolrDocumentList sdl = new SolrDocumentList();
    sdl.add(d1);
    sdl.add(d2);
    rsp.addResponse(sdl);
    w.write(buf, req, rsp);
    assertEquals("a:1:{s:8:\"response\";a:3:{s:8:\"numFound\";i:0;s:5:\"start\";i:0;s:4:\"docs\";a:2:{i:0;a:6:{s:2:\"id\";s:1:\"1\";s:5:\"data1\";s:5:\"hello\";s:5:\"data2\";i:42;s:5:\"data3\";b:1;s:5:\"data4\";a:2:{s:7:\"data4.1\";s:7:\"hashmap\";s:7:\"data4.2\";s:5:\"hello\";}s:5:\"data5\";a:3:{i:0;s:7:\"data5.1\";i:1;s:7:\"data5.2\";i:2;s:7:\"data5.3\";}}i:1;a:1:{s:2:\"id\";s:1:\"2\";}}}}", buf.toString());
    req.close();
}
Also used : SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SolrDocument(org.apache.solr.common.SolrDocument) StringWriter(java.io.StringWriter) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) PHPSerializedResponseWriter(org.apache.solr.response.PHPSerializedResponseWriter) SolrDocumentList(org.apache.solr.common.SolrDocumentList) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 25 with SolrQueryResponse

use of org.apache.solr.response.SolrQueryResponse in project lucene-solr by apache.

the class TestSolrQueryResponse method testAddHttpHeader.

@Test
public void testAddHttpHeader() {
    SolrQueryResponse response = new SolrQueryResponse();
    Iterator<Entry<String, String>> it = response.httpHeaders();
    assertFalse(it.hasNext());
    response.addHttpHeader("key1", "value1");
    it = response.httpHeaders();
    assertTrue(it.hasNext());
    Entry<String, String> entry = it.next();
    assertEquals("key1", entry.getKey());
    assertEquals("value1", entry.getValue());
    assertFalse(it.hasNext());
    response.addHttpHeader("key1", "value2");
    it = response.httpHeaders();
    assertTrue(it.hasNext());
    entry = it.next();
    assertEquals("key1", entry.getKey());
    assertEquals("value1", entry.getValue());
    assertTrue(it.hasNext());
    entry = it.next();
    assertEquals("key1", entry.getKey());
    assertEquals("value2", entry.getValue());
    assertFalse(it.hasNext());
    response.addHttpHeader("key2", "value2");
    it = response.httpHeaders();
    assertTrue(it.hasNext());
    entry = it.next();
    assertEquals("key1", entry.getKey());
    assertEquals("value1", entry.getValue());
    assertTrue(it.hasNext());
    entry = it.next();
    assertEquals("key1", entry.getKey());
    assertEquals("value2", entry.getValue());
    assertTrue(it.hasNext());
    entry = it.next();
    assertEquals("key2", entry.getKey());
    assertEquals("value2", entry.getValue());
    assertFalse(it.hasNext());
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) Entry(java.util.Map.Entry) Test(org.junit.Test)

Aggregations

SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)258 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)128 Test (org.junit.Test)100 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)80 NamedList (org.apache.solr.common.util.NamedList)68 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)58 SolrCore (org.apache.solr.core.SolrCore)52 AddUpdateCommand (org.apache.solr.update.AddUpdateCommand)41 SolrInputDocument (org.apache.solr.common.SolrInputDocument)40 SolrException (org.apache.solr.common.SolrException)32 ContentStreamBase (org.apache.solr.common.util.ContentStreamBase)29 ArrayList (java.util.ArrayList)26 BufferingRequestProcessor (org.apache.solr.update.processor.BufferingRequestProcessor)24 SolrRequestHandler (org.apache.solr.request.SolrRequestHandler)22 SolrRequestInfo (org.apache.solr.request.SolrRequestInfo)21 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)20 UpdateRequestProcessor (org.apache.solr.update.processor.UpdateRequestProcessor)20 JsonLoader (org.apache.solr.handler.loader.JsonLoader)17 IOException (java.io.IOException)16 HashMap (java.util.HashMap)16