Search in sources :

Example 76 with QueryResponse

use of org.apache.solr.client.solrj.response.QueryResponse in project lucene-solr by apache.

the class SolrExampleTests method testQueryWithParams.

@Test
public void testQueryWithParams() throws SolrServerException, IOException {
    SolrClient client = getSolrClient();
    SolrQuery q = new SolrQuery("query");
    q.setParam("debug", true);
    QueryResponse resp = client.query(q);
    assertEquals("server didn't respond with debug=true, didn't we pass in the parameter?", "true", ((NamedList) resp.getResponseHeader().get("params")).get("debug"));
}
Also used : 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) Test(org.junit.Test)

Example 77 with QueryResponse

use of org.apache.solr.client.solrj.response.QueryResponse in project lucene-solr by apache.

the class SolrExampleTests method testChildDoctransformer.

@Test
public void testChildDoctransformer() throws IOException, SolrServerException {
    SolrClient client = getSolrClient();
    client.deleteByQuery("*:*");
    client.commit();
    int numRootDocs = TestUtil.nextInt(random(), 10, 100);
    int maxDepth = TestUtil.nextInt(random(), 2, 5);
    Map<String, SolrInputDocument> allDocs = new HashMap<>();
    for (int i = 0; i < numRootDocs; i++) {
        client.add(genNestedDocuments(allDocs, 0, maxDepth));
    }
    client.commit();
    // sanity check
    SolrQuery q = new SolrQuery("*:*");
    QueryResponse resp = client.query(q);
    assertEquals("Doc count does not match", allDocs.size(), resp.getResults().getNumFound());
    // base check - we know there is an exact number of these root docs
    q = new SolrQuery("level_i:0");
    q.setFields("*", "[child parentFilter=\"level_i:0\"]");
    resp = client.query(q);
    assertEquals("topLevel count does not match", numRootDocs, resp.getResults().getNumFound());
    for (SolrDocument outDoc : resp.getResults()) {
        String docId = (String) outDoc.getFieldValue("id");
        SolrInputDocument origDoc = allDocs.get(docId);
        assertNotNull("docId not found: " + docId, origDoc);
        assertEquals("kids mismatch", origDoc.hasChildDocuments(), outDoc.hasChildDocuments());
        if (outDoc.hasChildDocuments()) {
            for (SolrDocument kid : outDoc.getChildDocuments()) {
                String kidId = (String) kid.getFieldValue("id");
                SolrInputDocument origChild = findDecendent(origDoc, kidId);
                assertNotNull(docId + " doesn't have decendent " + kidId, origChild);
            }
        }
    }
    // simple check: direct verification of direct children on random docs
    {
        int parentLevel = TestUtil.nextInt(random(), 0, maxDepth);
        int kidLevel = parentLevel + 1;
        String parentFilter = "level_i:" + parentLevel;
        String childFilter = "level_i:" + kidLevel;
        int maxKidCount = TestUtil.nextInt(random(), 1, 37);
        q = new SolrQuery("*:*");
        q.setFilterQueries(parentFilter);
        q.setFields("id,[child parentFilter=\"" + parentFilter + "\" childFilter=\"" + childFilter + "\" limit=\"" + maxKidCount + "\"]");
        resp = client.query(q);
        for (SolrDocument outDoc : resp.getResults()) {
            String docId = (String) outDoc.getFieldValue("id");
            SolrInputDocument origDoc = allDocs.get(docId);
            assertNotNull("docId not found: " + docId, origDoc);
            assertEquals("kids mismatch", origDoc.hasChildDocuments(), outDoc.hasChildDocuments());
            if (outDoc.hasChildDocuments()) {
                // since we know we are looking at our direct children
                // we can verify the count
                int numOrigKids = origDoc.getChildDocuments().size();
                int numOutKids = outDoc.getChildDocuments().size();
                assertEquals("Num kids mismatch: " + numOrigKids + "/" + maxKidCount, (maxKidCount < numOrigKids ? maxKidCount : numOrigKids), numOutKids);
                for (SolrDocument kid : outDoc.getChildDocuments()) {
                    String kidId = (String) kid.getFieldValue("id");
                    assertEquals("kid is the wrong level", kidLevel, (int) kid.getFieldValue("level_i"));
                    SolrInputDocument origChild = findDecendent(origDoc, kidId);
                    assertNotNull(docId + " doesn't have decendent " + kidId, origChild);
                }
            }
        }
    }
    // fully randomized
    // verifications are driven only by the results
    {
        int parentLevel = TestUtil.nextInt(random(), 0, maxDepth - 1);
        int kidLevelMin = TestUtil.nextInt(random(), parentLevel + 1, maxDepth);
        int kidLevelMax = TestUtil.nextInt(random(), kidLevelMin, maxDepth);
        String parentFilter = "level_i:" + parentLevel;
        String childFilter = "level_i:[" + kidLevelMin + " TO " + kidLevelMax + "]";
        int maxKidCount = TestUtil.nextInt(random(), 1, 7);
        q = new SolrQuery("*:*");
        if (random().nextBoolean()) {
            String name = names[TestUtil.nextInt(random(), 0, names.length - 1)];
            q = new SolrQuery("name:" + name);
        }
        q.setFilterQueries(parentFilter);
        q.setFields("id,[child parentFilter=\"" + parentFilter + "\" childFilter=\"" + childFilter + "\" limit=\"" + maxKidCount + "\"]");
        resp = client.query(q);
        for (SolrDocument outDoc : resp.getResults()) {
            String docId = (String) outDoc.getFieldValue("id");
            SolrInputDocument origDoc = allDocs.get(docId);
            assertNotNull("docId not found: " + docId, origDoc);
            // might not go deep enough for childFilter...
            if (outDoc.hasChildDocuments()) {
                // ...however if there are out kids, there *have* to be orig kids
                assertTrue("orig doc had no kids at all", origDoc.hasChildDocuments());
                for (SolrDocument kid : outDoc.getChildDocuments()) {
                    String kidId = (String) kid.getFieldValue("id");
                    int kidLevel = (int) kid.getFieldValue("level_i");
                    assertTrue("kid level to high: " + kidLevelMax + "<" + kidLevel, kidLevel <= kidLevelMax);
                    assertTrue("kid level to low: " + kidLevelMin + ">" + kidLevel, kidLevelMin <= kidLevel);
                    SolrInputDocument origChild = findDecendent(origDoc, kidId);
                    assertNotNull(docId + " doesn't have decendent " + kidId, origChild);
                }
            }
        }
    }
}
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) HashMap(java.util.HashMap) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) StringContains.containsString(org.junit.internal.matchers.StringContains.containsString) Test(org.junit.Test)

Example 78 with QueryResponse

use of org.apache.solr.client.solrj.response.QueryResponse in project lucene-solr by apache.

the class SolrExampleTests method doPivotFacetTest.

private void doPivotFacetTest(boolean missing) throws Exception {
    SolrClient client = getSolrClient();
    // Empty the database...
    // delete everything!
    client.deleteByQuery("*:*");
    client.commit();
    // make sure it got in
    assertNumFound("*:*", 0);
    int id = 1;
    ArrayList<SolrInputDocument> docs = new ArrayList<>();
    docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "a", "inStock", true));
    docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "a", "inStock", false));
    docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "a", "inStock", true));
    docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "b", "inStock", false));
    docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "b", "inStock", true));
    docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "a", "inStock", false));
    docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "a", "inStock", true));
    docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "b", "inStock", false));
    docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "b", "inStock", true));
    docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "b", "inStock", false));
    docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "b", "inStock", true));
    // something not matching all fields
    docs.add(makeTestDoc("id", id++, "cat", "b"));
    client.add(docs);
    client.commit();
    SolrQuery query = new SolrQuery("*:*");
    query.addFacetPivotField("features,cat", "cat,features", "features,cat,inStock");
    query.setFacetMinCount(0);
    query.setFacetMissing(missing);
    query.setRows(0);
    QueryResponse rsp = client.query(query);
    assertEquals(docs.size(), rsp.getResults().getNumFound());
    NamedList<List<PivotField>> pivots = rsp.getFacetPivot();
    assertEquals(3, pivots.size());
    //    for(Map.Entry<String, List<PivotField>> entry : pivots ) {
    //      System.out.println( "PIVOT: "+entry.getKey() );
    //      for( PivotField p : entry.getValue() ) {
    //        p.write(System.out, 0 );
    //      }
    //      System.out.println();
    //    }
    //  PIVOT: features,cat
    //  features=bbb (6)
    //    cat=b (4)
    //    cat=a (2)
    //  features=aaa (5)
    //    cat=a (3)
    //    cat=b (2)
    //  features missing (1)
    //    cat=b (1)
    assertEquals("features,cat", pivots.getName(0));
    List<PivotField> pivot = pivots.getVal(0);
    assertEquals(missing ? 3 : 2, pivot.size());
    PivotField ff = pivot.get(0);
    assertEquals("features", ff.getField());
    assertEquals("bbb", ff.getValue());
    assertEquals(6, ff.getCount());
    List<PivotField> counts = ff.getPivot();
    assertEquals(2, counts.size());
    assertEquals("cat", counts.get(0).getField());
    assertEquals("b", counts.get(0).getValue());
    assertEquals(4, counts.get(0).getCount());
    assertEquals("a", counts.get(1).getValue());
    assertEquals(2, counts.get(1).getCount());
    ff = pivot.get(1);
    assertEquals("features", ff.getField());
    assertEquals("aaa", ff.getValue());
    assertEquals(5, ff.getCount());
    counts = ff.getPivot();
    assertEquals(2, counts.size());
    assertEquals("cat", counts.get(0).getField());
    assertEquals("a", counts.get(0).getValue());
    assertEquals(3, counts.get(0).getCount());
    assertEquals("b", counts.get(1).getValue());
    assertEquals(2, counts.get(1).getCount());
    if (missing) {
        ff = pivot.get(2);
        assertEquals("features", ff.getField());
        assertEquals(null, ff.getValue());
        assertEquals(1, ff.getCount());
        counts = ff.getPivot();
        assertEquals(1, counts.size());
        assertEquals("cat", counts.get(0).getField());
        assertEquals("b", counts.get(0).getValue());
        assertEquals(1, counts.get(0).getCount());
    }
    //  PIVOT: cat,features
    //  cat=b (7)
    //    features=bbb (4)
    //    features=aaa (2)
    //    features missing (1)
    //  cat=a (5)
    //    features=aaa (3)
    //    features=bbb (2)
    assertEquals("cat,features", pivots.getName(1));
    pivot = pivots.getVal(1);
    assertEquals(2, pivot.size());
    ff = pivot.get(0);
    assertEquals("cat", ff.getField());
    assertEquals("b", ff.getValue());
    assertEquals(7, ff.getCount());
    counts = ff.getPivot();
    assertEquals(missing ? 3 : 2, counts.size());
    assertEquals("features", counts.get(0).getField());
    assertEquals("bbb", counts.get(0).getValue());
    assertEquals(4, counts.get(0).getCount());
    assertEquals("aaa", counts.get(1).getValue());
    assertEquals(2, counts.get(1).getCount());
    if (missing) {
        assertEquals(null, counts.get(2).getValue());
        assertEquals(1, counts.get(2).getCount());
    }
    ff = pivot.get(1);
    assertEquals("cat", ff.getField());
    assertEquals("a", ff.getValue());
    assertEquals(5, ff.getCount());
    counts = ff.getPivot();
    assertEquals(2, counts.size());
    assertEquals("features", counts.get(0).getField());
    assertEquals("aaa", counts.get(0).getValue());
    assertEquals(3, counts.get(0).getCount());
    assertEquals("bbb", counts.get(1).getValue());
    assertEquals(2, counts.get(1).getCount());
    // Three deep:
    //  PIVOT: features,cat,inStock
    //  features=bbb (6)
    //    cat=b (4)
    //      inStock=false (2)
    //      inStock=true (2)
    //    cat=a (2)
    //      inStock=false (1)
    //      inStock=true (1)
    //  features=aaa (5)
    //    cat=a (3)
    //      inStock=true (2)
    //      inStock=false (1)
    //    cat=b (2)
    //      inStock=false (1)
    //      inStock=true (1)
    //  features missing (1)
    //    cat=b (1)
    //      inStock missing (1)
    assertEquals("features,cat,inStock", pivots.getName(2));
    pivot = pivots.getVal(2);
    assertEquals(missing ? 3 : 2, pivot.size());
    // get(1) should be features=AAAA, then get(0) should be cat=a
    PivotField p = pivot.get(1).getPivot().get(0);
    assertEquals("cat", p.getField());
    assertEquals("a", p.getValue());
    counts = p.getPivot();
    //  p.write(System.out, 5 );
    // 2 trues and 1 false under features=AAAA,cat=a
    assertEquals(2, counts.size());
    assertEquals("inStock", counts.get(0).getField());
    assertEquals(Boolean.TRUE, counts.get(0).getValue());
    assertEquals(2, counts.get(0).getCount());
    if (missing) {
        p = pivot.get(2);
        assertEquals("features", p.getField());
        assertEquals(null, p.getValue());
        assertEquals(1, p.getCount());
        assertEquals(1, p.getPivot().size());
        p = p.getPivot().get(0);
        assertEquals("cat", p.getField());
        assertEquals("b", p.getValue());
        assertEquals(1, p.getCount());
        assertEquals(1, p.getPivot().size());
        p = p.getPivot().get(0);
        assertEquals("inStock", p.getField());
        assertEquals(null, p.getValue());
        assertEquals(1, p.getCount());
        assertEquals(null, p.getPivot());
    }
    // -- SOLR-2255 Test excluding a filter Query --
    // this test is a slight modification to the first pivot facet test
    query = new SolrQuery("*:*");
    query.addFacetPivotField("{!ex=mytag key=mykey}features,cat");
    //filters out one
    query.addFilterQuery("{!tag=mytag}-(features:bbb AND cat:a AND inStock:true)");
    query.setFacetMinCount(0);
    query.setRows(0);
    rsp = client.query(query);
    //one less due to filter
    assertEquals(docs.size() - 1, rsp.getResults().getNumFound());
    //The rest of this test should be just like the original since we've
    // excluded the 'fq' from the facet count
    pivots = rsp.getFacetPivot();
    pivot = pivots.getVal(0);
    assertEquals("mykey", pivots.getName(0));
    assertEquals(2, pivot.size());
    ff = pivot.get(0);
    assertEquals("features", ff.getField());
    assertEquals("bbb", ff.getValue());
    assertEquals(6, ff.getCount());
    counts = ff.getPivot();
    assertEquals(2, counts.size());
    assertEquals("cat", counts.get(0).getField());
    assertEquals("b", counts.get(0).getValue());
    assertEquals(4, counts.get(0).getCount());
    assertEquals("a", counts.get(1).getValue());
    assertEquals(2, counts.get(1).getCount());
}
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) ArrayList(java.util.ArrayList) PivotField(org.apache.solr.client.solrj.response.PivotField) SolrDocumentList(org.apache.solr.common.SolrDocumentList) List(java.util.List) ArrayList(java.util.ArrayList) NamedList(org.apache.solr.common.util.NamedList)

Example 79 with QueryResponse

use of org.apache.solr.client.solrj.response.QueryResponse in project lucene-solr by apache.

the class SolrExampleTests method testSetNullUpdates.

@Test
public void testSetNullUpdates() throws Exception {
    SolrClient solrClient = getSolrClient();
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField("id", "testSetNullUpdates");
    doc.addField("single_s", "test-value");
    doc.addField("multi_ss", Arrays.asList("first", "second"));
    solrClient.add(doc);
    solrClient.commit(true, true);
    doc.removeField("single_s");
    doc.removeField("multi_ss");
    Map<String, Object> map = Maps.newHashMap();
    map.put("set", null);
    doc.addField("multi_ss", map);
    solrClient.add(doc);
    solrClient.commit(true, true);
    QueryResponse response = solrClient.query(new SolrQuery("id:testSetNullUpdates"));
    assertNotNull("Entire doc was replaced because null update was not written", response.getResults().get(0).getFieldValue("single_s"));
    assertNull("Null update failed. Value still exists in document", response.getResults().get(0).getFieldValue("multi_ss"));
}
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) StringContains.containsString(org.junit.internal.matchers.StringContains.containsString) Test(org.junit.Test)

Example 80 with QueryResponse

use of org.apache.solr.client.solrj.response.QueryResponse in project lucene-solr by apache.

the class SolrExampleTests method testMultiContentStreamRequest.

@Test
public void testMultiContentStreamRequest() throws Exception {
    SolrClient client = getSolrClient();
    // delete everything!
    client.deleteByQuery("*:*");
    client.commit();
    QueryResponse rsp = client.query(new SolrQuery("*:*"));
    Assert.assertEquals(0, rsp.getResults().getNumFound());
    ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update");
    // 2
    up.addFile(getFile("solrj/docs1.xml"), "application/xml");
    // 3
    up.addFile(getFile("solrj/docs2.xml"), "application/xml");
    up.setParam("a", "ሴ");
    up.setParam(CommonParams.HEADER_ECHO_PARAMS, CommonParams.EchoParamStyle.ALL.toString());
    up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
    NamedList<Object> result = client.request(up);
    Assert.assertEquals("ሴ", ((NamedList) ((NamedList) result.get("responseHeader")).get("params")).get("a"));
    assertNotNull("Couldn't upload xml files", result);
    rsp = client.query(new SolrQuery("*:*"));
    Assert.assertEquals(5, rsp.getResults().getNumFound());
}
Also used : 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) ContentStreamUpdateRequest(org.apache.solr.client.solrj.request.ContentStreamUpdateRequest) Test(org.junit.Test)

Aggregations

QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)293 SolrQuery (org.apache.solr.client.solrj.SolrQuery)129 Test (org.junit.Test)111 SolrDocument (org.apache.solr.common.SolrDocument)81 SolrInputDocument (org.apache.solr.common.SolrInputDocument)67 SolrDocumentList (org.apache.solr.common.SolrDocumentList)61 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)58 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)56 SolrServerException (org.apache.solr.client.solrj.SolrServerException)47 ArrayList (java.util.ArrayList)41 IOException (java.io.IOException)39 NamedList (org.apache.solr.common.util.NamedList)32 SolrParams (org.apache.solr.common.params.SolrParams)28 SolrClient (org.apache.solr.client.solrj.SolrClient)27 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)27 ErrorTrackingConcurrentUpdateSolrClient (org.apache.solr.client.solrj.embedded.SolrExampleStreamingTest.ErrorTrackingConcurrentUpdateSolrClient)25 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)25 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)23 HashMap (java.util.HashMap)22 List (java.util.List)20