use of org.apache.solr.client.solrj.response.FacetField.Count in project lucene-solr by apache.
the class BlockJoinFacetDistribTest method testBJQFacetComponent.
@Test
public void testBJQFacetComponent() throws Exception {
assert !colors.removeAll(sizes) : "there is no colors in sizes";
Collections.shuffle(colors, random());
List<String> matchingColors = colors.subList(0, Math.min(atLeast(random(), 2), colors.size()));
Map<String, Set<Integer>> parentIdsByAttrValue = new HashMap<String, Set<Integer>>() {
@Override
public Set<Integer> get(Object key) {
return super.get(key) == null && put((String) key, new HashSet<>()) == null ? super.get(key) : super.get(key);
}
};
cluster.getSolrClient().deleteByQuery(collection, "*:*");
final int parents = atLeast(10);
boolean aggregationOccurs = false;
List<SolrInputDocument> parentDocs = new ArrayList<>();
for (int parent = 0; parent < parents || !aggregationOccurs; parent++) {
assert parent < 2000000 : "parent num " + parent + " aggregationOccurs:" + aggregationOccurs + ". Sorry! too tricky loop condition.";
SolrInputDocument pdoc = new SolrInputDocument();
pdoc.addField("id", parent);
pdoc.addField("type_s", "parent");
final String parentBrand = "brand" + (random().nextInt(5));
pdoc.addField("BRAND_s", parentBrand);
for (int child = 0; child < atLeast(colors.size() / 2); child++) {
SolrInputDocument childDoc = new SolrInputDocument();
final String color = colors.get(random().nextInt(colors.size()));
childDoc.addField("COLOR_s", color);
final String size = sizes.get(random().nextInt(sizes.size()));
childDoc.addField("SIZE_s", size);
if (matchingColors.contains(color)) {
final boolean colorDupe = !parentIdsByAttrValue.get(color).add(parent);
final boolean sizeDupe = !parentIdsByAttrValue.get(size).add(parent);
aggregationOccurs |= colorDupe || sizeDupe;
}
pdoc.addChildDocument(childDoc);
}
parentDocs.add(pdoc);
if (!parentDocs.isEmpty() && rarely()) {
indexDocs(parentDocs);
parentDocs.clear();
cluster.getSolrClient().commit(collection, false, false, true);
}
}
if (!parentDocs.isEmpty()) {
indexDocs(parentDocs);
}
cluster.getSolrClient().commit(collection);
// to parent query
final String childQueryClause = "COLOR_s:(" + (matchingColors.toString().replaceAll("[,\\[\\]]", " ")) + ")";
final boolean oldFacetsEnabled = random().nextBoolean();
QueryResponse results = query("q", "{!parent which=\"type_s:parent\"}" + childQueryClause, // try to enforce multiple phases
"facet", // try to enforce multiple phases
oldFacetsEnabled ? "true" : "false", oldFacetsEnabled ? "facet.field" : "ignore", "BRAND_s", oldFacetsEnabled && usually() ? "facet.limit" : "ignore", "1", oldFacetsEnabled && usually() ? "facet.mincount" : "ignore", "2", oldFacetsEnabled && usually() ? "facet.overrequest.count" : "ignore", "0", "qt", random().nextBoolean() ? "blockJoinDocSetFacetRH" : "blockJoinFacetRH", "child.facet.field", "COLOR_s", "child.facet.field", "SIZE_s", "distrib.singlePass", random().nextBoolean() ? "true" : "false", "rows", random().nextBoolean() ? "0" : "10");
NamedList<Object> resultsResponse = results.getResponse();
assertNotNull(resultsResponse);
FacetField color_s = results.getFacetField("COLOR_s");
FacetField size_s = results.getFacetField("SIZE_s");
String msg = "" + parentIdsByAttrValue + " " + color_s + " " + size_s;
for (FacetField facet : new FacetField[] { color_s, size_s }) {
for (Count c : facet.getValues()) {
assertEquals(c.getName() + "(" + msg + ")", parentIdsByAttrValue.get(c.getName()).size(), c.getCount());
}
}
assertEquals(msg, parentIdsByAttrValue.size(), color_s.getValueCount() + size_s.getValueCount());
//System.out.println(parentIdsByAttrValue);
}
Aggregations