Search in sources :

Example 61 with SolrDocument

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

the class SolrExampleTestsBase method testStreamingRequest.

@Test
public void testStreamingRequest() throws Exception {
    SolrClient client = getSolrClient();
    // Empty the database...
    // delete everything!
    client.deleteByQuery("*:*");
    client.commit();
    // make sure it got in
    assertNumFound("*:*", 0);
    // Add some docs to the index
    UpdateRequest req = new UpdateRequest();
    for (int i = 0; i < 10; i++) {
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("id", "" + i);
        doc.addField("cat", "foocat");
        req.add(doc);
    }
    req.setAction(ACTION.COMMIT, true, true);
    req.process(client);
    // Make sure it ran OK
    SolrQuery query = new SolrQuery("*:*");
    query.set(CommonParams.FL, "id,score,_docid_");
    QueryResponse response = client.query(query);
    assertEquals(0, response.getStatus());
    assertEquals(10, response.getResults().getNumFound());
    // Now make sure each document gets output
    final AtomicInteger cnt = new AtomicInteger(0);
    client.queryAndStreamResponse(query, new StreamingResponseCallback() {

        @Override
        public void streamDocListInfo(long numFound, long start, Float maxScore) {
            assertEquals(10, numFound);
        }

        @Override
        public void streamSolrDocument(SolrDocument doc) {
            cnt.incrementAndGet();
            // Make sure the transformer works for streaming
            Float score = (Float) doc.get("score");
            assertEquals("should have score", new Float(1.0), score);
        }
    });
    assertEquals(10, cnt.get());
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrDocument(org.apache.solr.common.SolrDocument) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) Test(org.junit.Test)

Example 62 with SolrDocument

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

the class SolrExampleTests method testExampleConfig.

/**
   * query the example
   */
@Test
public void testExampleConfig() throws Exception {
    SolrClient client = getSolrClient();
    // Empty the database...
    // delete everything!
    client.deleteByQuery("*:*");
    // Now add something...
    SolrInputDocument doc = new SolrInputDocument();
    String docID = "1112211111";
    doc.addField("id", docID);
    doc.addField("name", "my name!");
    Assert.assertEquals(null, doc.getField("foo"));
    Assert.assertTrue(doc.getField("name").getValue() != null);
    UpdateResponse upres = client.add(doc);
    // System.out.println( "ADD:"+upres.getResponse() );
    Assert.assertEquals(0, upres.getStatus());
    upres = client.commit(true, true);
    // System.out.println( "COMMIT:"+upres.getResponse() );
    Assert.assertEquals(0, upres.getStatus());
    upres = client.optimize(true, true);
    // System.out.println( "OPTIMIZE:"+upres.getResponse() );
    Assert.assertEquals(0, upres.getStatus());
    SolrQuery query = new SolrQuery();
    query.setQuery("id:" + docID);
    QueryResponse response = client.query(query);
    Assert.assertEquals(docID, response.getResults().get(0).getFieldValue("id"));
    // Now add a few docs for facet testing...
    List<SolrInputDocument> docs = new ArrayList<>();
    SolrInputDocument doc2 = new SolrInputDocument();
    doc2.addField("id", "2");
    doc2.addField("inStock", true);
    doc2.addField("price", 2);
    doc2.addField("timestamp_dt", new java.util.Date());
    docs.add(doc2);
    SolrInputDocument doc3 = new SolrInputDocument();
    doc3.addField("id", "3");
    doc3.addField("inStock", false);
    doc3.addField("price", 3);
    doc3.addField("timestamp_dt", new java.util.Date());
    docs.add(doc3);
    SolrInputDocument doc4 = new SolrInputDocument();
    doc4.addField("id", "4");
    doc4.addField("inStock", true);
    doc4.addField("price", 4);
    doc4.addField("timestamp_dt", new java.util.Date());
    docs.add(doc4);
    SolrInputDocument doc5 = new SolrInputDocument();
    doc5.addField("id", "5");
    doc5.addField("inStock", false);
    doc5.addField("price", 5);
    doc5.addField("timestamp_dt", new java.util.Date());
    docs.add(doc5);
    upres = client.add(docs);
    // System.out.println( "ADD:"+upres.getResponse() );
    Assert.assertEquals(0, upres.getStatus());
    upres = client.commit(true, true);
    // System.out.println( "COMMIT:"+upres.getResponse() );
    Assert.assertEquals(0, upres.getStatus());
    upres = client.optimize(true, true);
    // System.out.println( "OPTIMIZE:"+upres.getResponse() );
    Assert.assertEquals(0, upres.getStatus());
    query = new SolrQuery("*:*");
    query.addFacetQuery("price:[* TO 2]");
    query.addFacetQuery("price:[2 TO 4]");
    query.addFacetQuery("price:[5 TO *]");
    query.addFacetField("inStock");
    query.addFacetField("price");
    query.addFacetField("timestamp_dt");
    query.removeFilterQuery("inStock:true");
    response = client.query(query);
    Assert.assertEquals(0, response.getStatus());
    Assert.assertEquals(5, response.getResults().getNumFound());
    Assert.assertEquals(3, response.getFacetQuery().size());
    Assert.assertEquals(2, response.getFacetField("inStock").getValueCount());
    Assert.assertEquals(4, response.getFacetField("price").getValueCount());
    // test a second query, test making a copy of the main query
    SolrQuery query2 = query.getCopy();
    query2.addFilterQuery("inStock:true");
    Assert.assertFalse(query.getFilterQueries() == query2.getFilterQueries());
    response = client.query(query2);
    Assert.assertEquals(1, query2.getFilterQueries().length);
    Assert.assertEquals(0, response.getStatus());
    Assert.assertEquals(2, response.getResults().getNumFound());
    for (SolrDocument outDoc : response.getResults()) {
        assertEquals(true, outDoc.getFieldValue("inStock"));
    }
    // sanity check round tripping of params...
    query = new SolrQuery("foo");
    query.addFilterQuery("{!field f=inStock}true");
    query.addFilterQuery("{!term f=name}hoss");
    query.addFacetQuery("price:[* TO 2]");
    query.addFacetQuery("price:[2 TO 4]");
    response = client.query(query);
    assertTrue("echoed params are not a NamedList: " + response.getResponseHeader().get("params").getClass(), response.getResponseHeader().get("params") instanceof NamedList);
    NamedList echo = (NamedList) response.getResponseHeader().get("params");
    List values = null;
    assertEquals("foo", echo.get("q"));
    assertTrue("echoed fq is not a List: " + echo.get("fq").getClass(), echo.get("fq") instanceof List);
    values = (List) echo.get("fq");
    Assert.assertEquals(2, values.size());
    Assert.assertEquals("{!field f=inStock}true", values.get(0));
    Assert.assertEquals("{!term f=name}hoss", values.get(1));
    assertTrue("echoed facet.query is not a List: " + echo.get("facet.query").getClass(), echo.get("facet.query") instanceof List);
    values = (List) echo.get("facet.query");
    Assert.assertEquals(2, values.size());
    Assert.assertEquals("price:[* TO 2]", values.get(0));
    Assert.assertEquals("price:[2 TO 4]", values.get(1));
    if (jetty != null) {
        // check system wide system handler + "/admin/info/system"
        String url = jetty.getBaseUrl().toString();
        try (HttpSolrClient adminClient = getHttpSolrClient(url)) {
            SolrQuery q = new SolrQuery();
            q.set("qt", "/admin/info/system");
            QueryResponse rsp = adminClient.query(q);
            assertNotNull(rsp.getResponse().get("mode"));
            assertNotNull(rsp.getResponse().get("lucene"));
        }
    }
}
Also used : NamedList(org.apache.solr.common.util.NamedList) ArrayList(java.util.ArrayList) StringContains.containsString(org.junit.internal.matchers.StringContains.containsString) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) UpdateResponse(org.apache.solr.client.solrj.response.UpdateResponse) 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) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrDocumentList(org.apache.solr.common.SolrDocumentList) List(java.util.List) ArrayList(java.util.ArrayList) NamedList(org.apache.solr.common.util.NamedList) Test(org.junit.Test)

Example 63 with SolrDocument

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

the class SolrExampleTests method testRawFields.

@Test
public void testRawFields() throws Exception {
    String rawJson = "{ \"raw\": 1.234, \"id\":\"111\" }";
    String rawXml = "<hello>this is <some/><xml/></hello>";
    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("json_s", rawJson);
    doc.addField("xml_s", rawXml);
    client.add(doc);
    // make sure this gets in first
    client.commit();
    SolrQuery query = new SolrQuery();
    query.setQuery("*:*");
    query.set(CommonParams.FL, "id,json_s:[json],xml_s:[xml]");
    QueryRequest req = new QueryRequest(query);
    req.setResponseParser(new BinaryResponseParser());
    QueryResponse rsp = req.process(client);
    SolrDocumentList out = rsp.getResults();
    assertEquals(1, out.getNumFound());
    SolrDocument out1 = out.get(0);
    assertEquals("111", out1.getFieldValue("id"));
    // Check that the 'raw' fields are unchanged using the standard formats
    assertEquals(rawJson, out1.get("json_s"));
    assertEquals(rawXml, out1.get("xml_s"));
    if (client instanceof EmbeddedSolrServer) {
        // the EmbeddedSolrServer ignores the configured parser
        return;
    }
    // Check raw JSON Output
    query.set("fl", "id,json_s:[json],xml_s:[xml]");
    query.set(CommonParams.WT, "json");
    req = new QueryRequest(query);
    req.setResponseParser(new NoOpResponseParser("json"));
    NamedList<Object> resp = client.request(req);
    String raw = (String) resp.get("response");
    // Check that the response parses as JSON
    JSONParser parser = new JSONParser(raw);
    int evt = parser.nextEvent();
    while (evt != JSONParser.EOF) {
        evt = parser.nextEvent();
    }
    // no escaping
    assertTrue(raw.indexOf(rawJson) > 0);
    // quoted xml
    assertTrue(raw.indexOf('"' + rawXml + '"') > 0);
    // Check raw XML Output
    req.setResponseParser(new NoOpResponseParser("xml"));
    query.set("fl", "id,json_s:[json],xml_s:[xml]");
    query.set(CommonParams.WT, "xml");
    req = new QueryRequest(query);
    req.setResponseParser(new NoOpResponseParser("xml"));
    resp = client.request(req);
    raw = (String) resp.get("response");
    // Check that we get raw xml and json is escaped
    // escaped
    assertTrue(raw.indexOf('>' + rawJson + '<') > 0);
    // raw xml
    assertTrue(raw.indexOf(rawXml) > 0);
}
Also used : NoOpResponseParser(org.apache.solr.client.solrj.impl.NoOpResponseParser) QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) StringContains.containsString(org.junit.internal.matchers.StringContains.containsString) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrDocument(org.apache.solr.common.SolrDocument) BinaryResponseParser(org.apache.solr.client.solrj.impl.BinaryResponseParser) 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) JSONParser(org.noggit.JSONParser) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) Test(org.junit.Test)

Example 64 with SolrDocument

use of org.apache.solr.common.SolrDocument 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 65 with SolrDocument

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

the class JavaBinCodec method writeSolrDocument.

public void writeSolrDocument(SolrDocument doc) throws IOException {
    List<SolrDocument> children = doc.getChildDocuments();
    int fieldsCount = 0;
    if (writableDocFields == null || writableDocFields.wantsAllFields() || ignoreWritable) {
        fieldsCount = doc.size();
    } else {
        for (Entry<String, Object> e : doc) {
            if (toWrite(e.getKey()))
                fieldsCount++;
        }
    }
    int sz = fieldsCount + (children == null ? 0 : children.size());
    writeTag(SOLRDOC);
    writeTag(ORDERED_MAP, sz);
    for (Map.Entry<String, Object> entry : doc) {
        String name = entry.getKey();
        if (toWrite(name)) {
            writeExternString(name);
            Object val = entry.getValue();
            writeVal(val);
        }
    }
    if (children != null) {
        try {
            ignoreWritable = true;
            for (SolrDocument child : children) {
                writeSolrDocument(child);
            }
        } finally {
            ignoreWritable = false;
        }
    }
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

SolrDocument (org.apache.solr.common.SolrDocument)230 SolrDocumentList (org.apache.solr.common.SolrDocumentList)93 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)81 ArrayList (java.util.ArrayList)50 SolrQuery (org.apache.solr.client.solrj.SolrQuery)47 Test (org.junit.Test)46 SolrParams (org.apache.solr.common.params.SolrParams)38 SolrServerException (org.apache.solr.client.solrj.SolrServerException)35 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)35 IOException (java.io.IOException)32 SolrInputDocument (org.apache.solr.common.SolrInputDocument)28 HashMap (java.util.HashMap)26 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)24 NamedList (org.apache.solr.common.util.NamedList)21 Map (java.util.Map)20 List (java.util.List)18 SolrClient (org.apache.solr.client.solrj.SolrClient)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)12 SolrException (org.apache.solr.common.SolrException)12