Skip to content

Commit

Permalink
change stat facets to allow the sum of a facet to be zero (#69)
Browse files Browse the repository at this point in the history
* change stat facets to allow the sum of a facet to be zero or negative
  • Loading branch information
mdavis95 authored May 18, 2022
1 parent 0d1d04b commit 8b832b9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,27 +259,22 @@ public List<ZuliaQuery.FacetStats> getTopChildren(String field, int topN, String

int ord = children[dimOrd];

double doubleSumValues = 0;
double doubleBottomValue = 0;

long longSumValues = 0;
long longBottomValue = 0;
double doubleBottomValue = Double.NEGATIVE_INFINITY;
long longBottomValue = Long.MIN_VALUE;

while (ord != TaxonomyReader.INVALID_ORDINAL) {
Stats stat = stats[ord];
if (stat != null) {
stat.ordinal = ord;
if (stat.doubleSum > 0) {
doubleSumValues += stat.doubleSum;
if (stat.isFloatingPoint()) {
if (stat.doubleSum > doubleBottomValue) {
q.insertWithOverflow(stat);
if (q.size() == topN) {
doubleBottomValue = q.top().doubleSum;
}
}
}
else if (stat.longSum > 0) {
longSumValues += stat.longSum;
else {
if (stat.longSum > longBottomValue) {
q.insertWithOverflow(stat);
if (q.size() == topN) {
Expand All @@ -292,9 +287,6 @@ else if (stat.longSum > 0) {
ord = siblings[ord];
}

if (doubleSumValues == 0 && longSumValues == 0) {
return null;
}

ZuliaQuery.FacetStats[] facetStats = new ZuliaQuery.FacetStats[q.size()];
for (int i = facetStats.length - 1; i >= 0; i--) {
Expand Down Expand Up @@ -339,6 +331,7 @@ else if (a.doubleSum > b.doubleSum || a.longSum > b.longSum) {

public static class Stats {

private final boolean floatingPoint;
private int ordinal;
private long docCount;
private long allDocCount;
Expand All @@ -361,6 +354,7 @@ public Stats(boolean floatingPoint) {
doubleMinValue = 0;
doubleMaxValue = 0;
}
this.floatingPoint = floatingPoint;
}

public void newDoc(boolean countNonNull) {
Expand Down Expand Up @@ -391,6 +385,10 @@ public void newValue(long newValue) {
}
this.valueCount++;
}

public boolean isFloatingPoint() {
return floatingPoint;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void index() throws Exception {
indexRecord(i * uniqueDocs + 3, "something special", "top2/middle/bottom3", "bar", 2, List.of(0.5));
indexRecord(i * uniqueDocs + 4, "something really special", "top3/middle/bottom4", "bar", 5, List.of(3.0));
indexRecord(i * uniqueDocs + 5, "something really special", "top3/middle/bottom4", null, 4, List.of());
indexRecord(i * uniqueDocs + 6, "boring", "top4/middle/bottom5", "other", 1, List.of());
indexRecord(i * uniqueDocs + 6, "boring", "top4/middle/bottom5", "other", 1, List.of(0.0));
}

}
Expand Down Expand Up @@ -132,6 +132,18 @@ public void statTest() throws Exception {
Assertions.assertThrows(Exception.class, () -> zuliaWorkPool.search(finalSearch),
"Expecting: Search: Numeric field <authorCount> must be indexed as a SORTABLE numeric field");

search = new Search(STAT_TEST_INDEX);
search.addStat(new StatFacet("rating", "normalFacet"));
search.addQuery(new FilterQuery("title:boring"));
searchResult = zuliaWorkPool.search(search);
Assertions.assertEquals(repeatCount, searchResult.getTotalHits());
List<FacetStats> ratingByFacet = searchResult.getFacetFieldStat("rating", "normalFacet");
Assertions.assertEquals(1, ratingByFacet.size());
Assertions.assertEquals(repeatCount, ratingByFacet.get(0).getDocCount());
Assertions.assertEquals(0, ratingByFacet.get(0).getMin().getDoubleValue());
Assertions.assertEquals(0, ratingByFacet.get(0).getMax().getDoubleValue());
Assertions.assertEquals(0, ratingByFacet.get(0).getSum().getDoubleValue());

}

private void ratingTest(FacetStats ratingStat) {
Expand Down Expand Up @@ -279,6 +291,24 @@ public void confirm() throws Exception {
authorCountPathFacetTest(searchResult);
ratingNormalTest(searchResult);
ratingPathTest(searchResult);


search = new Search(STAT_TEST_INDEX);
search.addStat(new StatFacet("rating", "pathFacet"));
search.addQuery(new FilterQuery("\"something really special\"").addQueryField("title"));
search.addQuery(new FilterQuery("authorCount:[4 TO 4.1]"));
search.addQuery(new FilterQuery("rating:[2 TO 3]").exclude());
searchResult = zuliaWorkPool.search(search);
Assertions.assertEquals(repeatCount, searchResult.getTotalHits());
List<FacetStats> ratingByFacet = searchResult.getFacetFieldStat("rating", "pathFacet");
Assertions.assertEquals(1, ratingByFacet.size());
Assertions.assertEquals(0, ratingByFacet.get(0).getDocCount()); //No docs have values for rating that match even though there are facets for pathFacet
System.err.println(ratingByFacet.get(0).getMin().getDoubleValue());
System.err.println(ratingByFacet.get(0).getMax().getDoubleValue());
System.err.println(ratingByFacet.get(0).getSum().getDoubleValue());
Assertions.assertEquals(Double.POSITIVE_INFINITY, ratingByFacet.get(0).getMin().getDoubleValue(), 0.001); // Positive Infinity for Min when no values found
Assertions.assertEquals(Double.NEGATIVE_INFINITY, ratingByFacet.get(0).getMax().getDoubleValue(), 0.001); // Negative Infinity for Max when no values found
Assertions.assertEquals(0, ratingByFacet.get(0).getSum().getDoubleValue(), 0.001);
}

private void authorCountPathFacetTest(SearchResult searchResult) {
Expand Down

0 comments on commit 8b832b9

Please sign in to comment.