use of org.apache.solr.client.solrj.response.PivotField 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());
}
use of org.apache.solr.client.solrj.response.PivotField in project lucene-solr by apache.
the class TestCloudPivotFacet method assertTraceOk.
/**
* Verify that the PivotFields we're lookin at doesn't violate any of the expected
* behaviors based on the <code>TRACE_*</code> params found in the base params
*/
private void assertTraceOk(String pivotName, SolrParams baseParams, List<PivotField> constraints) {
if (null == constraints || 0 == constraints.size()) {
return;
}
final int maxIdx = constraints.size() - 1;
final int min = baseParams.getInt(TRACE_MIN, -1);
final boolean expectMissing = baseParams.getBool(TRACE_MISS, false);
final boolean checkCount = "count".equals(baseParams.get(TRACE_SORT, "count"));
int prevCount = Integer.MAX_VALUE;
for (int i = 0; i <= maxIdx; i++) {
final PivotField constraint = constraints.get(i);
final int count = constraint.getCount();
if (0 < min) {
assertTrue(pivotName + ": val #" + i + " of " + maxIdx + ": count(" + count + ") < facet.mincount(" + min + "): " + constraint, min <= count);
}
// and may not exist at all (mincount, none missing for this sub-facet, etc...)
if ((i < maxIdx) || (!expectMissing)) {
assertNotNull(pivotName + ": val #" + i + " of " + maxIdx + " has null value: " + constraint, constraint.getValue());
}
// be "missing"
if (checkCount) {
assertTrue(pivotName + ": val #" + i + " of" + maxIdx + ": count(" + count + ") > prevCount(" + prevCount + "): " + constraint, ((count <= prevCount) || (expectMissing && i == maxIdx && null == constraint.getValue())));
prevCount = count;
}
}
}
use of org.apache.solr.client.solrj.response.PivotField in project lucene-solr by apache.
the class TestCloudPivotFacet method assertPivotCountsAreCorrect.
/**
* Given some query params, executes the request against the cloudClient and
* then walks the pivot facet values in the response, treating each one as a
* filter query to assert the pivot counts are correct.
*/
private void assertPivotCountsAreCorrect(SolrParams baseParams, SolrParams pivotParams) throws SolrServerException {
SolrParams initParams = SolrParams.wrapAppended(pivotParams, baseParams);
log.info("Doing full run: {}", initParams);
countNumFoundChecks = 0;
NamedList<List<PivotField>> pivots = null;
try {
QueryResponse initResponse = cloudClient.query(initParams);
pivots = initResponse.getFacetPivot();
assertNotNull(initParams + " has null pivots?", pivots);
assertEquals(initParams + " num pivots", initParams.getParams("facet.pivot").length, pivots.size());
} catch (Exception e) {
throw new RuntimeException("init query failed: " + initParams + ": " + e.getMessage(), e);
}
try {
for (Map.Entry<String, List<PivotField>> pivot : pivots) {
final String pivotKey = pivot.getKey();
// :HACK: for counting the max possible pivot depth
final int maxDepth = 1 + pivotKey.length() - pivotKey.replace(",", "").length();
assertTraceOk(pivotKey, baseParams, pivot.getValue());
// will catch it.
for (PivotField constraint : pivot.getValue()) {
int depth = assertPivotCountsAreCorrect(pivotKey, baseParams, constraint);
// we can't assert that the depth reached is the same as the depth requested
// because the fq and/or mincount may have pruned the tree too much
assertTrue("went too deep: " + depth + ": " + pivotKey + " ==> " + pivot, depth <= maxDepth);
}
}
} catch (AssertionError e) {
throw new AssertionError(initParams + " ==> " + e.getMessage(), e);
} finally {
log.info("Ending full run (countNumFoundChecks={}): {}", countNumFoundChecks, initParams);
}
}
use of org.apache.solr.client.solrj.response.PivotField in project lucene-solr by apache.
the class DistributedFacetPivotLargeTest method doTestDeepPivotStats.
private void doTestDeepPivotStats() throws Exception {
QueryResponse rsp = query("q", "*:*", "rows", "0", "facet", "true", "facet.pivot", "{!stats=s1}place_s,company_t", "stats", "true", "stats.field", "{!key=avg_price tag=s1}pay_i");
List<PivotField> pivots = rsp.getFacetPivot().get("place_s,company_t");
PivotField cardiffPivotField = pivots.get(0);
assertEquals("cardiff", cardiffPivotField.getValue());
assertEquals(257, cardiffPivotField.getCount());
FieldStatsInfo cardiffStatsInfo = cardiffPivotField.getFieldStatsInfo().get("avg_price");
assertEquals("avg_price", cardiffStatsInfo.getName());
assertEquals(0.0, cardiffStatsInfo.getMin());
assertEquals(8742.0, cardiffStatsInfo.getMax());
assertEquals(257, (long) cardiffStatsInfo.getCount());
assertEquals(0, (long) cardiffStatsInfo.getMissing());
assertEquals(347554.0, cardiffStatsInfo.getSum());
assertEquals(8.20968772E8, cardiffStatsInfo.getSumOfSquares(), 0.1E-7);
assertEquals(1352.35019455253, (double) cardiffStatsInfo.getMean(), 0.1E-7);
assertEquals(1170.86048165857, cardiffStatsInfo.getStddev(), 0.1E-7);
PivotField bbcCardifftPivotField = cardiffPivotField.getPivot().get(0);
assertEquals("bbc", bbcCardifftPivotField.getValue());
assertEquals(101, bbcCardifftPivotField.getCount());
FieldStatsInfo bbcCardifftPivotFieldStatsInfo = bbcCardifftPivotField.getFieldStatsInfo().get("avg_price");
assertEquals(2400.0, bbcCardifftPivotFieldStatsInfo.getMin());
assertEquals(8742.0, bbcCardifftPivotFieldStatsInfo.getMax());
assertEquals(101, (long) bbcCardifftPivotFieldStatsInfo.getCount());
assertEquals(0, (long) bbcCardifftPivotFieldStatsInfo.getMissing());
assertEquals(248742.0, bbcCardifftPivotFieldStatsInfo.getSum());
assertEquals(6.52422564E8, bbcCardifftPivotFieldStatsInfo.getSumOfSquares(), 0.1E-7);
assertEquals(2462.792079208, (double) bbcCardifftPivotFieldStatsInfo.getMean(), 0.1E-7);
assertEquals(631.0525860312, bbcCardifftPivotFieldStatsInfo.getStddev(), 0.1E-7);
PivotField placeholder0PivotField = pivots.get(2);
assertEquals("0placeholder", placeholder0PivotField.getValue());
assertEquals(6, placeholder0PivotField.getCount());
FieldStatsInfo placeholder0PivotFieldStatsInfo = placeholder0PivotField.getFieldStatsInfo().get("avg_price");
assertEquals("avg_price", placeholder0PivotFieldStatsInfo.getName());
assertEquals(2000.0, placeholder0PivotFieldStatsInfo.getMin());
assertEquals(6400.0, placeholder0PivotFieldStatsInfo.getMax());
assertEquals(6, (long) placeholder0PivotFieldStatsInfo.getCount());
assertEquals(0, (long) placeholder0PivotFieldStatsInfo.getMissing());
assertEquals(22700.0, placeholder0PivotFieldStatsInfo.getSum());
assertEquals(1.0105E8, placeholder0PivotFieldStatsInfo.getSumOfSquares(), 0.1E-7);
assertEquals(3783.333333333, (double) placeholder0PivotFieldStatsInfo.getMean(), 0.1E-7);
assertEquals(1741.742422595, placeholder0PivotFieldStatsInfo.getStddev(), 0.1E-7);
PivotField microsoftPlaceholder0PivotField = placeholder0PivotField.getPivot().get(1);
assertEquals("microsoft", microsoftPlaceholder0PivotField.getValue());
assertEquals(6, microsoftPlaceholder0PivotField.getCount());
FieldStatsInfo microsoftPlaceholder0PivotFieldStatsInfo = microsoftPlaceholder0PivotField.getFieldStatsInfo().get("avg_price");
assertEquals("avg_price", microsoftPlaceholder0PivotFieldStatsInfo.getName());
assertEquals(2000.0, microsoftPlaceholder0PivotFieldStatsInfo.getMin());
assertEquals(6400.0, microsoftPlaceholder0PivotFieldStatsInfo.getMax());
assertEquals(6, (long) microsoftPlaceholder0PivotFieldStatsInfo.getCount());
assertEquals(0, (long) microsoftPlaceholder0PivotFieldStatsInfo.getMissing());
assertEquals(22700.0, microsoftPlaceholder0PivotFieldStatsInfo.getSum());
assertEquals(1.0105E8, microsoftPlaceholder0PivotFieldStatsInfo.getSumOfSquares(), 0.1E-7);
assertEquals(3783.333333333, (double) microsoftPlaceholder0PivotFieldStatsInfo.getMean(), 0.1E-7);
assertEquals(1741.742422595, microsoftPlaceholder0PivotFieldStatsInfo.getStddev(), 0.1E-7);
}
use of org.apache.solr.client.solrj.response.PivotField in project lucene-solr by apache.
the class DistributedFacetPivotLargeTest method doTestPivotRanges.
/**
* spot checks some pivot values and the ranges hanging on them
*/
private void doTestPivotRanges() throws Exception {
// note: 'p0' is only a top level range, not included in per-pivot ranges
for (SolrParams p : new SolrParams[] { // results should be identical for all of these
params("facet.range", "{!key=p0 facet.range.gap=500}pay_i", "facet.range", "{!key=p1 tag=t1 facet.range.gap=100}pay_i", "facet.range", "{!key=p2 tag=t1 facet.range.gap=200}pay_i", "facet.range.start", "0", "facet.range.end", "1000"), params("facet.range", "{!key=p0 facet.range.gap=500}pay_i", "facet.range", "{!key=p1 tag=t1 facet.range.gap=100}pay_i", "facet.range", "{!key=p2 tag=t1 facet.range.gap=200}pay_i", "f.pay_i.facet.range.start", "0", "facet.range.end", "1000"), params("facet.range", "{!key=p0 facet.range.gap=500 facet.range.start=0}pay_i", "facet.range", "{!key=p1 tag=t1 facet.range.gap=100 facet.range.start=0}pay_i", "facet.range", "{!key=p2 tag=t1 facet.range.gap=200 facet.range.start=0}pay_i", "facet.range.end", "1000") }) {
QueryResponse rsp = query(SolrParams.wrapDefaults(p, params("q", "*:*", "rows", "0", "facet", "true", "facet.pivot", "{!range=t1}place_s,company_t")));
List<PivotField> pivots = rsp.getFacetPivot().get("place_s,company_t");
// changes as we spot check
PivotField pf = null;
// changes as we spot check
List<RangeFacet.Count> rfc = null;
// 1st sanity check top level ranges
assertEquals(3, rsp.getFacetRanges().size());
assertRange("p0", 0, 500, 1000, 2, rsp.getFacetRanges().get(0));
assertRange("p1", 0, 100, 1000, 10, rsp.getFacetRanges().get(1));
assertRange("p2", 0, 200, 1000, 5, rsp.getFacetRanges().get(2));
// check pivots...
// first top level pivot value
pf = pivots.get(0);
assertPivot("place_s", "cardiff", 257, pf);
assertRange("p1", 0, 100, 1000, 10, pf.getFacetRanges().get(0));
assertRange("p2", 0, 200, 1000, 5, pf.getFacetRanges().get(1));
rfc = pf.getFacetRanges().get(0).getCounts();
assertEquals("200", rfc.get(2).getValue());
assertEquals(14, rfc.get(2).getCount());
assertEquals("300", rfc.get(3).getValue());
assertEquals(15, rfc.get(3).getCount());
rfc = pf.getFacetRanges().get(1).getCounts();
assertEquals("200", rfc.get(1).getValue());
assertEquals(29, rfc.get(1).getCount());
// drill down one level of the pivot
pf = pf.getPivot().get(0);
assertPivot("company_t", "bbc", 101, pf);
assertRange("p1", 0, 100, 1000, 10, pf.getFacetRanges().get(0));
assertRange("p2", 0, 200, 1000, 5, pf.getFacetRanges().get(1));
rfc = pf.getFacetRanges().get(0).getCounts();
for (RangeFacet.Count c : rfc) {
// no docs in our ranges for this pivot drill down
assertEquals(0, c.getCount());
}
// pop back up and spot check a different top level pivot value
pf = pivots.get(53);
assertPivot("place_s", "placeholder0", 1, pf);
assertRange("p1", 0, 100, 1000, 10, pf.getFacetRanges().get(0));
assertRange("p2", 0, 200, 1000, 5, pf.getFacetRanges().get(1));
rfc = pf.getFacetRanges().get(0).getCounts();
assertEquals("0", rfc.get(0).getValue());
assertEquals(1, rfc.get(0).getCount());
assertEquals("100", rfc.get(1).getValue());
assertEquals(0, rfc.get(1).getCount());
// drill down one level of the pivot
pf = pf.getPivot().get(0);
assertPivot("company_t", "compholder0", 1, pf);
assertRange("p1", 0, 100, 1000, 10, pf.getFacetRanges().get(0));
assertRange("p2", 0, 200, 1000, 5, pf.getFacetRanges().get(1));
rfc = pf.getFacetRanges().get(0).getCounts();
assertEquals("0", rfc.get(0).getValue());
assertEquals(1, rfc.get(0).getCount());
assertEquals("100", rfc.get(1).getValue());
assertEquals(0, rfc.get(1).getCount());
}
}
Aggregations