Search in sources :

Example 51 with SolrDocumentList

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

the class SolrExampleTests method testUpdateRequestWithParameters.

@Test
public void testUpdateRequestWithParameters() throws Exception {
    SolrClient client = createNewSolrClient();
    client.deleteByQuery("*:*");
    client.commit();
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField("id", "id1");
    UpdateRequest req = new UpdateRequest();
    req.setParam("overwrite", "false");
    req.add(doc);
    client.request(req);
    client.request(req);
    client.commit();
    SolrQuery query = new SolrQuery();
    query.setQuery("*:*");
    QueryResponse rsp = client.query(query);
    SolrDocumentList out = rsp.getResults();
    assertEquals(2, out.getNumFound());
    if (!(client instanceof EmbeddedSolrServer)) {
        /* Do not close in case of using EmbeddedSolrServer,
       * as that would close the CoreContainer */
        client.close();
    }
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) ErrorTrackingConcurrentUpdateSolrClient(org.apache.solr.client.solrj.embedded.SolrExampleStreamingTest.ErrorTrackingConcurrentUpdateSolrClient) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) AbstractUpdateRequest(org.apache.solr.client.solrj.request.AbstractUpdateRequest) ContentStreamUpdateRequest(org.apache.solr.client.solrj.request.ContentStreamUpdateRequest) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrDocumentList(org.apache.solr.common.SolrDocumentList) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) Test(org.junit.Test)

Example 52 with SolrDocumentList

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

the class SolrExampleTests method testAugmentFields.

@Test
public void testAugmentFields() throws Exception {
    SolrClient client = getSolrClient();
    // Empty the database...
    // delete everything!
    client.deleteByQuery("*:*");
    // Now add something...
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField("id", "111");
    doc.addField("name", "doc1");
    doc.addField("price", 11);
    client.add(doc);
    // make sure this gets in first
    client.commit();
    doc = new SolrInputDocument();
    doc.addField("id", "222");
    doc.addField("name", "doc2");
    doc.addField("price", 22);
    client.add(doc);
    client.commit();
    SolrQuery query = new SolrQuery();
    query.setQuery("*:*");
    query.set(CommonParams.FL, "id,price,[docid],[explain style=nl],score,aaa:[value v=aaa],ten:[value v=10 t=int]");
    query.addSort(new SolrQuery.SortClause("price", SolrQuery.ORDER.asc));
    QueryResponse rsp = client.query(query);
    SolrDocumentList out = rsp.getResults();
    assertEquals(2, out.getNumFound());
    SolrDocument out1 = out.get(0);
    SolrDocument out2 = out.get(1);
    assertEquals("111", out1.getFieldValue("id"));
    assertEquals("222", out2.getFieldValue("id"));
    assertEquals(1.0f, out1.getFieldValue("score"));
    assertEquals(1.0f, out2.getFieldValue("score"));
    // check that the docid is one bigger
    int id1 = (Integer) out1.getFieldValue("[docid]");
    int id2 = (Integer) out2.getFieldValue("[docid]");
    assertTrue("should be bigger [" + id1 + "," + id2 + "]", id2 > id1);
    // The score from explain should be the same as the score
    NamedList explain = (NamedList) out1.getFieldValue("[explain]");
    assertEquals(out1.get("score"), explain.get("value"));
    // Augmented _value_ with alias
    assertEquals("aaa", out1.get("aaa"));
    assertEquals(10, ((Integer) out1.get("ten")).intValue());
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrDocument(org.apache.solr.common.SolrDocument) ErrorTrackingConcurrentUpdateSolrClient(org.apache.solr.client.solrj.embedded.SolrExampleStreamingTest.ErrorTrackingConcurrentUpdateSolrClient) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) NamedList(org.apache.solr.common.util.NamedList) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrDocumentList(org.apache.solr.common.SolrDocumentList) Test(org.junit.Test)

Example 53 with SolrDocumentList

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

the class SolrExampleTests method testMoreLikeThis.

@Test
public void testMoreLikeThis() throws Exception {
    SolrClient client = getSolrClient();
    client.deleteByQuery("*:*");
    for (int i = 0; i < 20; i++) {
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("id", "testMoreLikeThis" + i);
        doc.addField("x_s", "x_" + i);
        doc.addField("y_s", "y_" + (i % 3));
        doc.addField("z_s", "z_" + i);
        client.add(doc);
    }
    client.commit();
    // test with mlt.fl having comma separated values
    SolrQuery q = new SolrQuery("*:*");
    q.setRows(20);
    q.setMoreLikeThisFields("x_s", "y_s", "z_s");
    q.setMoreLikeThisMinTermFreq(0);
    q.setMoreLikeThisCount(2);
    QueryResponse response = client.query(q);
    assertEquals(20, response.getResults().getNumFound());
    NamedList<SolrDocumentList> moreLikeThis = response.getMoreLikeThis();
    assertNotNull("MoreLikeThis response should not have been null", moreLikeThis);
    for (int i = 0; i < 20; i++) {
        String id = "testMoreLikeThis" + i;
        SolrDocumentList mltResp = moreLikeThis.get(id);
        assertNotNull("MoreLikeThis response for id=" + id + " should not be null", mltResp);
        assertTrue("MoreLikeThis response for id=" + id + " had numFound=0", mltResp.getNumFound() > 0);
        assertTrue("MoreLikeThis response for id=" + id + " had not returned exactly 2 documents", mltResp.size() == 2);
    }
    // now test with multiple mlt.fl parameters
    q = new SolrQuery("*:*");
    q.setRows(20);
    q.setParam("mlt", "true");
    q.setParam("mlt.fl", "x_s", "y_s", "z_s");
    q.setMoreLikeThisMinTermFreq(0);
    q.setMoreLikeThisCount(2);
    response = client.query(q);
    assertEquals(20, response.getResults().getNumFound());
    moreLikeThis = response.getMoreLikeThis();
    assertNotNull("MoreLikeThis response should not have been null", moreLikeThis);
    for (int i = 0; i < 20; i++) {
        String id = "testMoreLikeThis" + i;
        SolrDocumentList mltResp = moreLikeThis.get(id);
        assertNotNull("MoreLikeThis response for id=" + id + " should not be null", mltResp);
        assertTrue("MoreLikeThis response for id=" + id + " had numFound=0", mltResp.getNumFound() > 0);
        assertTrue("MoreLikeThis response for id=" + id + " had not returned exactly 2 documents", mltResp.size() == 2);
    }
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) ErrorTrackingConcurrentUpdateSolrClient(org.apache.solr.client.solrj.embedded.SolrExampleStreamingTest.ErrorTrackingConcurrentUpdateSolrClient) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrDocumentList(org.apache.solr.common.SolrDocumentList) StringContains.containsString(org.junit.internal.matchers.StringContains.containsString) Test(org.junit.Test)

Example 54 with SolrDocumentList

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

the class TestNamedListCodec method testSimple.

public void testSimple() throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    NamedList nl = new NamedList();
    Float fval = new Float(10.01f);
    Boolean bval = Boolean.TRUE;
    String sval = "12qwaszx";
    // Set up a simple document
    NamedList r = new NamedList();
    nl.add("responseHeader", r);
    r.add("status", 0);
    r.add("QTime", 63);
    NamedList p = new NamedList();
    r.add("params", p);
    p.add("rows", 10);
    p.add("start", 0);
    p.add("indent", "on");
    p.add("q", "ipod");
    SolrDocumentList list = new SolrDocumentList();
    nl.add("response", list);
    list.setMaxScore(1.0f);
    list.setStart(10);
    list.setNumFound(12);
    SolrDocument doc = new SolrDocument();
    doc.addField("f", fval);
    doc.addField("b", bval);
    doc.addField("s", sval);
    doc.addField("f", 100);
    list.add(doc);
    doc = new SolrDocument();
    doc.addField("f", fval);
    doc.addField("b", bval);
    doc.addField("s", sval);
    doc.addField("f", 101);
    list.add(doc);
    nl.add("zzz", doc);
    new JavaBinCodec(null).marshal(nl, baos);
    byte[] arr = baos.toByteArray();
    nl = (NamedList) new JavaBinCodec().unmarshal(new ByteArrayInputStream(arr));
    assertEquals(3, nl.size());
    assertEquals("ipod", ((NamedList) ((NamedList) nl.getVal(0)).get("params")).get("q"));
    list = (SolrDocumentList) nl.getVal(1);
    assertEquals(12, list.getNumFound());
    assertEquals(10, list.getStart());
    assertEquals(101, ((List) list.get(1).getFieldValue("f")).get(1));
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SolrDocumentList(org.apache.solr.common.SolrDocumentList)

Example 55 with SolrDocumentList

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

the class QueryResponseTest method testSimpleGroupResponse.

@Test
public void testSimpleGroupResponse() throws Exception {
    XMLResponseParser parser = new XMLResponseParser();
    InputStream is = new SolrResourceLoader().openResource("solrj/sampleSimpleGroupResponse.xml");
    assertNotNull(is);
    Reader in = new InputStreamReader(is, StandardCharsets.UTF_8);
    NamedList<Object> response = parser.processResponse(in);
    in.close();
    QueryResponse qr = new QueryResponse(response, null);
    assertNotNull(qr);
    GroupResponse groupResponse = qr.getGroupResponse();
    assertNotNull(groupResponse);
    List<GroupCommand> commands = groupResponse.getValues();
    assertNotNull(commands);
    assertEquals(1, commands.size());
    GroupCommand fieldCommand = commands.get(0);
    assertEquals("acco_id", fieldCommand.getName());
    assertEquals(30000000, fieldCommand.getMatches());
    assertEquals(5687, fieldCommand.getNGroups().intValue());
    List<Group> fieldCommandGroups = fieldCommand.getValues();
    assertEquals(1, fieldCommandGroups.size());
    assertEquals("acco_id", fieldCommandGroups.get(0).getGroupValue());
    SolrDocumentList documents = fieldCommandGroups.get(0).getResult();
    assertNotNull(documents);
    assertEquals(10, documents.size());
    assertEquals("116_AR", documents.get(0).getFieldValue("acco_id"));
    assertEquals("116_HI", documents.get(1).getFieldValue("acco_id"));
    assertEquals("953_AR", documents.get(2).getFieldValue("acco_id"));
    assertEquals("953_HI", documents.get(3).getFieldValue("acco_id"));
    assertEquals("954_AR", documents.get(4).getFieldValue("acco_id"));
    assertEquals("954_HI", documents.get(5).getFieldValue("acco_id"));
    assertEquals("546_AR", documents.get(6).getFieldValue("acco_id"));
    assertEquals("546_HI", documents.get(7).getFieldValue("acco_id"));
    assertEquals("708_AR", documents.get(8).getFieldValue("acco_id"));
    assertEquals("708_HI", documents.get(9).getFieldValue("acco_id"));
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrResourceLoader(org.apache.solr.core.SolrResourceLoader) XMLResponseParser(org.apache.solr.client.solrj.impl.XMLResponseParser) Test(org.junit.Test)

Aggregations

SolrDocumentList (org.apache.solr.common.SolrDocumentList)161 SolrDocument (org.apache.solr.common.SolrDocument)89 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)52 Test (org.junit.Test)52 NamedList (org.apache.solr.common.util.NamedList)36 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)33 SolrQuery (org.apache.solr.client.solrj.SolrQuery)32 ArrayList (java.util.ArrayList)29 SolrParams (org.apache.solr.common.params.SolrParams)28 SolrServerException (org.apache.solr.client.solrj.SolrServerException)20 SolrInputDocument (org.apache.solr.common.SolrInputDocument)18 IOException (java.io.IOException)15 Map (java.util.Map)14 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)14 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)13 HashMap (java.util.HashMap)12 List (java.util.List)10 Date (java.util.Date)8 LinkedHashMap (java.util.LinkedHashMap)8 SolrClient (org.apache.solr.client.solrj.SolrClient)8