use of org.apache.solr.client.solrj.response.PivotField in project lucene-solr by apache.
the class SolrExampleTests method testPivotFacetsQueries.
@Test
public void testPivotFacetsQueries() 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, "popularity", 12, "price", .017));
docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "a", "inStock", false, "popularity", 13, "price", 16.04));
docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "a", "inStock", true, "popularity", 14, "price", 12.34));
docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "b", "inStock", false, "popularity", 24, "price", 51.39));
docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "b", "inStock", true, "popularity", 28, "price", 131.39));
docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "a", "inStock", false, "popularity", 32));
docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "a", "inStock", true, "popularity", 31, "price", 131.39));
docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "b", "inStock", false, "popularity", 36));
docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "b", "inStock", true, "popularity", 37, "price", 1.39));
docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "b", "inStock", false, "popularity", 38, "price", 47.98));
docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "b", "inStock", true, "popularity", -38));
// something not matching all fields
docs.add(makeTestDoc("id", id++, "cat", "b"));
client.add(docs);
client.commit();
SolrQuery query = new SolrQuery("*:*");
query.addFacetPivotField("{!query=s1}features,manu");
query.addFacetQuery("{!key=highPrice tag=s1}price:[100 TO *]");
query.addFacetQuery("{!tag=s1 key=lowPrice}price:[0 TO 50]");
query.setFacetMinCount(0);
query.setRows(0);
QueryResponse rsp = client.query(query);
Map<String, Integer> map = rsp.getFacetQuery();
assertEquals(2, map.get("highPrice").intValue());
assertEquals(5, map.get("lowPrice").intValue());
NamedList<List<PivotField>> pivots = rsp.getFacetPivot();
List<PivotField> pivotValues = pivots.get("features,manu");
PivotField featuresBBBPivot = pivotValues.get(0);
assertEquals("features", featuresBBBPivot.getField());
assertEquals("bbb", featuresBBBPivot.getValue());
assertNotNull(featuresBBBPivot.getFacetQuery());
assertEquals(2, featuresBBBPivot.getFacetQuery().size());
assertEquals(1, featuresBBBPivot.getFacetQuery().get("highPrice").intValue());
assertEquals(2, featuresBBBPivot.getFacetQuery().get("lowPrice").intValue());
PivotField featuresAAAPivot = pivotValues.get(1);
assertEquals("features", featuresAAAPivot.getField());
assertEquals("aaa", featuresAAAPivot.getValue());
assertNotNull(featuresAAAPivot.getFacetQuery());
assertEquals(2, featuresAAAPivot.getFacetQuery().size());
assertEquals(1, featuresAAAPivot.getFacetQuery().get("highPrice").intValue());
assertEquals(3, featuresAAAPivot.getFacetQuery().get("lowPrice").intValue());
}
use of org.apache.solr.client.solrj.response.PivotField in project lucene-solr by apache.
the class SolrExampleTests method testPivotFacetsRanges.
@Test
public void testPivotFacetsRanges() 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, "popularity", 12, "price", .017));
docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "a", "inStock", false, "popularity", 13, "price", 16.04));
docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "a", "inStock", true, "popularity", 14, "price", 12.34));
docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "b", "inStock", false, "popularity", 24, "price", 51.39));
docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "b", "inStock", true, "popularity", 28, "price", 131.39));
docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "a", "inStock", false, "popularity", 32));
docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "a", "inStock", true, "popularity", 31, "price", 131.39));
docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "b", "inStock", false, "popularity", 36));
docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "b", "inStock", true, "popularity", 37, "price", 1.39));
docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "b", "inStock", false, "popularity", 38, "price", 47.98));
docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "b", "inStock", true, "popularity", -38));
// something not matching all fields
docs.add(makeTestDoc("id", id++, "cat", "b"));
client.add(docs);
client.commit();
SolrQuery query = new SolrQuery("*:*");
query.addFacetPivotField("{!range=s1}features,manu");
query.add(FacetParams.FACET_RANGE, "{!key=price1 tag=s1}price");
query.add(String.format(Locale.ROOT, "f.%s.%s", "price", FacetParams.FACET_RANGE_START), "0");
query.add(String.format(Locale.ROOT, "f.%s.%s", "price", FacetParams.FACET_RANGE_END), "200");
query.add(String.format(Locale.ROOT, "f.%s.%s", "price", FacetParams.FACET_RANGE_GAP), "50");
query.set(FacetParams.FACET, true);
query.add(FacetParams.FACET_RANGE, "{!key=price2 tag=s1}price");
query.setFacetMinCount(0);
query.setRows(0);
QueryResponse rsp = client.query(query);
List<RangeFacet> list = rsp.getFacetRanges();
assertEquals(2, list.size());
@SuppressWarnings("unchecked") RangeFacet<Float, Float> range1 = list.get(0);
assertEquals("price1", range1.getName());
assertEquals(0, range1.getStart().intValue());
assertEquals(200, range1.getEnd().intValue());
assertEquals(50, range1.getGap().intValue());
List<Count> counts1 = range1.getCounts();
assertEquals(4, counts1.size());
assertEquals(5, counts1.get(0).getCount());
assertEquals("0.0", counts1.get(0).getValue());
assertEquals(1, counts1.get(1).getCount());
assertEquals("50.0", counts1.get(1).getValue());
assertEquals(2, counts1.get(2).getCount());
assertEquals("100.0", counts1.get(2).getValue());
assertEquals(0, counts1.get(3).getCount());
assertEquals("150.0", counts1.get(3).getValue());
@SuppressWarnings("unchecked") RangeFacet<Float, Float> range2 = list.get(1);
assertEquals("price2", range2.getName());
assertEquals(0, range2.getStart().intValue());
assertEquals(200, range2.getEnd().intValue());
assertEquals(50, range2.getGap().intValue());
List<Count> counts2 = range2.getCounts();
assertEquals(4, counts2.size());
assertEquals(5, counts2.get(0).getCount());
assertEquals("0.0", counts2.get(0).getValue());
assertEquals(1, counts2.get(1).getCount());
assertEquals("50.0", counts2.get(1).getValue());
assertEquals(2, counts2.get(2).getCount());
assertEquals("100.0", counts2.get(2).getValue());
assertEquals(0, counts2.get(3).getCount());
assertEquals("150.0", counts2.get(3).getValue());
NamedList<List<PivotField>> pivots = rsp.getFacetPivot();
List<PivotField> pivotValues = pivots.get("features,manu");
PivotField featuresBBBPivot = pivotValues.get(0);
assertEquals("features", featuresBBBPivot.getField());
assertEquals("bbb", featuresBBBPivot.getValue());
List<RangeFacet> featuresBBBRanges = featuresBBBPivot.getFacetRanges();
for (RangeFacet range : featuresBBBRanges) {
if (range.getName().equals("price1")) {
assertNotNull(range);
assertEquals(0, ((Float) range.getStart()).intValue());
assertEquals(200, ((Float) range.getEnd()).intValue());
assertEquals(50, ((Float) range.getGap()).intValue());
List<Count> counts = range.getCounts();
assertEquals(4, counts.size());
for (Count count : counts) {
switch(count.getValue()) {
case "0.0":
assertEquals(2, count.getCount());
break;
case "50.0":
assertEquals(0, count.getCount());
break;
case "100.0":
assertEquals(1, count.getCount());
break;
case "150.0":
assertEquals(0, count.getCount());
break;
}
}
} else if (range.getName().equals("price2")) {
assertNotNull(range);
assertEquals(0, ((Float) range.getStart()).intValue());
assertEquals(200, ((Float) range.getEnd()).intValue());
assertEquals(50, ((Float) range.getGap()).intValue());
List<Count> counts = range.getCounts();
assertEquals(4, counts.size());
for (Count count : counts) {
switch(count.getValue()) {
case "0.0":
assertEquals(2, count.getCount());
break;
case "50.0":
assertEquals(0, count.getCount());
break;
case "100.0":
assertEquals(1, count.getCount());
break;
case "150.0":
assertEquals(0, count.getCount());
break;
}
}
}
}
PivotField featuresAAAPivot = pivotValues.get(1);
assertEquals("features", featuresAAAPivot.getField());
assertEquals("aaa", featuresAAAPivot.getValue());
List<RangeFacet> facetRanges = featuresAAAPivot.getFacetRanges();
for (RangeFacet range : facetRanges) {
if (range.getName().equals("price1")) {
assertNotNull(range);
assertEquals(0, ((Float) range.getStart()).intValue());
assertEquals(200, ((Float) range.getEnd()).intValue());
assertEquals(50, ((Float) range.getGap()).intValue());
List<Count> counts = range.getCounts();
assertEquals(4, counts.size());
for (Count count : counts) {
switch(count.getValue()) {
case "0.0":
assertEquals(3, count.getCount());
break;
case "50.0":
assertEquals(1, count.getCount());
break;
case "100.0":
assertEquals(1, count.getCount());
break;
case "150.0":
assertEquals(0, count.getCount());
break;
}
}
} else if (range.getName().equals("price2")) {
assertNotNull(range);
assertEquals(0, ((Float) range.getStart()).intValue());
assertEquals(200, ((Float) range.getEnd()).intValue());
assertEquals(50, ((Float) range.getGap()).intValue());
List<Count> counts = range.getCounts();
assertEquals(4, counts.size());
for (Count count : counts) {
switch(count.getValue()) {
case "0.0":
assertEquals(3, count.getCount());
break;
case "50.0":
assertEquals(1, count.getCount());
break;
case "100.0":
assertEquals(1, count.getCount());
break;
case "150.0":
assertEquals(0, count.getCount());
break;
}
}
}
}
}
use of org.apache.solr.client.solrj.response.PivotField in project lucene-solr by apache.
the class TestCloudPivotFacet method assertPivotCountsAreCorrect.
/**
* Recursive Helper method for asserting that pivot constraint counts match
* results when filtering on those constraints. Returns the recursive depth reached
* (for sanity checking)
*/
private int assertPivotCountsAreCorrect(String pivotName, SolrParams baseParams, PivotField constraint) throws SolrServerException {
SolrParams p = SolrParams.wrapAppended(baseParams, params("fq", buildFilter(constraint)));
List<PivotField> subPivots = null;
try {
assertPivotData(pivotName, constraint, p);
subPivots = constraint.getPivot();
} catch (Exception e) {
throw new RuntimeException(pivotName + ": count query failed: " + p + ": " + e.getMessage(), e);
}
int depth = 0;
if (null != subPivots) {
assertTraceOk(pivotName, baseParams, subPivots);
for (PivotField subPivot : subPivots) {
depth = assertPivotCountsAreCorrect(pivotName, p, subPivot);
}
}
return depth + 1;
}
use of org.apache.solr.client.solrj.response.PivotField in project ddf by codice.
the class SolrMetacardClientImpl method getContentTypes.
@Override
public Set<ContentType> getContentTypes() {
Set<ContentType> finalSet = new HashSet<>();
String contentTypeField = resolver.getField(Metacard.CONTENT_TYPE, AttributeType.AttributeFormat.STRING, true);
String contentTypeVersionField = resolver.getField(Metacard.CONTENT_TYPE_VERSION, AttributeType.AttributeFormat.STRING, true);
/*
* If we didn't find the field, it most likely means it does not exist. If it does not
* exist, then we can safely say that no content types are in this catalog provider
*/
if (contentTypeField == null || contentTypeVersionField == null) {
return finalSet;
}
SolrQuery query = new SolrQuery(contentTypeField + ":[* TO *]");
query.setFacet(true);
query.addFacetField(contentTypeField);
query.addFacetPivotField(contentTypeField + "," + contentTypeVersionField);
try {
QueryResponse solrResponse = client.query(query, SolrRequest.METHOD.POST);
List<FacetField> facetFields = solrResponse.getFacetFields();
for (Map.Entry<String, List<PivotField>> entry : solrResponse.getFacetPivot()) {
// however, the content type names can still be obtained via the facet fields.
if (CollectionUtils.isEmpty(entry.getValue())) {
LOGGER.debug("No content type versions found associated with any available content types.");
if (CollectionUtils.isNotEmpty(facetFields)) {
// values (content type names).
for (FacetField.Count currContentType : facetFields.get(0).getValues()) {
// unknown version, so setting it to null
ContentType contentType = new ContentTypeImpl(currContentType.getName(), null);
finalSet.add(contentType);
}
}
} else {
for (PivotField pf : entry.getValue()) {
String contentTypeName = pf.getValue().toString();
LOGGER.debug("contentTypeName: {}", contentTypeName);
if (CollectionUtils.isEmpty(pf.getPivot())) {
// if there are no sub-pivots, that means that there are no content type
// versions
// associated with this content type name
LOGGER.debug("Content type does not have associated contentTypeVersion: {}", contentTypeName);
ContentType contentType = new ContentTypeImpl(contentTypeName, null);
finalSet.add(contentType);
} else {
for (PivotField innerPf : pf.getPivot()) {
LOGGER.debug("contentTypeVersion: {}. For contentTypeName: {}", innerPf.getValue(), contentTypeName);
ContentType contentType = new ContentTypeImpl(contentTypeName, innerPf.getValue().toString());
finalSet.add(contentType);
}
}
}
}
}
} catch (SolrServerException | IOException e) {
LOGGER.info("Solr exception getting content types", e);
}
return finalSet;
}
Aggregations