use of io.zulia.message.ZuliaQuery.FacetCount in project zuliasearch by zuliaio.
the class FacetCombiner method getCombinedFacetGroup.
public FacetGroup getCombinedFacetGroup() {
if (facetGroups.size() == 1) {
return facetGroups.get(0).getFacetGroup();
} else {
Map<String, AtomicLong> facetCounts = new HashMap<>();
Map<String, FixedBitSet> shardsReturned = new HashMap<>();
FixedBitSet fullResults = new FixedBitSet(shardReponses);
long[] minForShard = new long[shardReponses];
for (FacetGroupWithShardIndex facetGroupWithShardIndex : facetGroups) {
FacetGroup fg = facetGroupWithShardIndex.getFacetGroup();
int shardIndex = facetGroupWithShardIndex.getShardIndex();
for (FacetCount fc : fg.getFacetCountList()) {
String facet = fc.getFacet();
AtomicLong facetSum = facetCounts.get(facet);
FixedBitSet shardSet = shardsReturned.get(facet);
if (facetSum == null) {
facetSum = new AtomicLong();
facetCounts.put(facet, facetSum);
shardSet = new FixedBitSet(shardReponses);
shardsReturned.put(facet, shardSet);
}
long count = fc.getCount();
facetSum.addAndGet(count);
shardSet.set(shardIndex);
minForShard[shardIndex] = count;
}
int shardFacets = countRequest.getShardFacets();
int facetCountCount = fg.getFacetCountCount();
if (facetCountCount < shardFacets || (shardFacets == -1)) {
fullResults.set(shardIndex);
minForShard[shardIndex] = 0;
}
}
FacetGroup.Builder fg = FacetGroup.newBuilder();
fg.setCountRequest(countRequest);
int numberOfShards = shardIndexes.length;
long maxValuePossibleMissing = 0;
for (int i = 0; i < numberOfShards; i++) {
maxValuePossibleMissing += minForShard[i];
}
boolean computeError = countRequest.getMaxFacets() > 0 && countRequest.getShardFacets() > 0 && numberOfShards > 1;
boolean computePossibleMissing = computeError && (maxValuePossibleMissing != 0);
SortedSet<FacetCountResult> sortedFacetResults = facetCounts.keySet().stream().map(facet -> new FacetCountResult(facet, facetCounts.get(facet).get())).collect(Collectors.toCollection(TreeSet::new));
int maxCount = countRequest.getMaxFacets();
long minCountReturned = 0;
int count = 0;
for (FacetCountResult facet : sortedFacetResults) {
FixedBitSet shardCount = shardsReturned.get(facet.getFacet());
shardCount.or(fullResults);
FacetCount.Builder facetCountBuilder = FacetCount.newBuilder().setFacet(facet.getFacet()).setCount(facet.getCount());
long maxWithError = 0;
if (computeError) {
long maxError = 0;
if (shardCount.cardinality() < numberOfShards) {
for (int i = 0; i < numberOfShards; i++) {
if (!shardCount.get(i)) {
maxError += minForShard[i];
}
}
}
facetCountBuilder.setMaxError(maxError);
maxWithError = maxError + facet.getCount();
}
count++;
if (maxCount > 0 && count > maxCount) {
if (computePossibleMissing) {
if (maxWithError > maxValuePossibleMissing) {
maxValuePossibleMissing = maxWithError;
}
} else {
break;
}
} else {
fg.addFacetCount(facetCountBuilder);
minCountReturned = facet.getCount();
}
}
if (!sortedFacetResults.isEmpty()) {
if (maxValuePossibleMissing > minCountReturned) {
fg.setPossibleMissing(true);
fg.setMaxValuePossibleMissing(maxValuePossibleMissing);
}
}
return fg.build();
}
}
use of io.zulia.message.ZuliaQuery.FacetCount in project zuliasearch by zuliaio.
the class StartStopTest method boolTestBuilder.
@Test
@Order(3)
public void boolTestBuilder() throws Exception {
Search s = new Search(FACET_TEST_INDEX).addQuery(new FilterQuery("testBool:true"));
SearchResult searchResult = zuliaWorkPool.search(s);
Assertions.assertEquals(6, searchResult.getTotalHits());
s = new Search(FACET_TEST_INDEX).addQuery(new FilterQuery("testBool:1"));
searchResult = zuliaWorkPool.search(s);
Assertions.assertEquals(6, searchResult.getTotalHits());
s = new Search(FACET_TEST_INDEX).addQuery(new FilterQuery("testBool:True"));
searchResult = zuliaWorkPool.search(s);
Assertions.assertEquals(6, searchResult.getTotalHits());
s = new Search(FACET_TEST_INDEX).addQuery(new FilterQuery("testBool:y"));
searchResult = zuliaWorkPool.search(s);
Assertions.assertEquals(6, searchResult.getTotalHits());
s = new Search(FACET_TEST_INDEX).addQuery(new FilterQuery("testBool:yes"));
searchResult = zuliaWorkPool.search(s);
Assertions.assertEquals(6, searchResult.getTotalHits());
s = new Search(FACET_TEST_INDEX).addQuery(new FilterQuery("testBool:false"));
searchResult = zuliaWorkPool.search(s);
Assertions.assertEquals(4, searchResult.getTotalHits());
s = new Search(FACET_TEST_INDEX).addQuery(new FilterQuery("testBool:0"));
searchResult = zuliaWorkPool.search(s);
Assertions.assertEquals(4, searchResult.getTotalHits());
s = new Search(FACET_TEST_INDEX).addQuery(new FilterQuery("testBool:no"));
searchResult = zuliaWorkPool.search(s);
Assertions.assertEquals(4, searchResult.getTotalHits());
s = new Search(FACET_TEST_INDEX).addQuery(new FilterQuery("testBool:f"));
searchResult = zuliaWorkPool.search(s);
Assertions.assertEquals(4, searchResult.getTotalHits());
s = new Search(FACET_TEST_INDEX).addQuery(new FilterQuery("testBool:fake"));
searchResult = zuliaWorkPool.search(s);
Assertions.assertEquals(0, searchResult.getTotalHits());
s = new Search(FACET_TEST_INDEX).addQuery(new FilterQuery("testBool:*"));
searchResult = zuliaWorkPool.search(s);
Assertions.assertEquals(10, searchResult.getTotalHits());
s = new Search(FACET_TEST_INDEX).addCountFacet(new CountFacet("testBool"));
searchResult = zuliaWorkPool.search(s);
List<FacetCount> facetCounts = searchResult.getFacetCounts("testBool");
Assertions.assertEquals(2, facetCounts.size());
Assertions.assertEquals(6, facetCounts.get(0).getCount());
Assertions.assertEquals("True", facetCounts.get(0).getFacet());
Assertions.assertEquals(4, facetCounts.get(1).getCount());
Assertions.assertEquals("False", facetCounts.get(1).getFacet());
}
use of io.zulia.message.ZuliaQuery.FacetCount in project zuliasearch by zuliaio.
the class StartStopTest method reindex.
@Test
@Order(4)
public void reindex() throws Exception {
ClientIndexConfig indexConfig = new ClientIndexConfig();
indexConfig.addDefaultSearchField("title");
indexConfig.addFieldConfig(FieldConfigBuilder.create("id", FieldType.STRING).indexAs(DefaultAnalyzers.LC_KEYWORD).sort());
indexConfig.addFieldConfig(FieldConfigBuilder.create("title", FieldType.STRING).indexAs(DefaultAnalyzers.STANDARD).sort());
indexConfig.addFieldConfig(FieldConfigBuilder.create("issn", FieldType.STRING).indexAs(DefaultAnalyzers.LC_KEYWORD).facet().sort());
indexConfig.addFieldConfig(FieldConfigBuilder.create("eissn", FieldType.STRING).indexAs(DefaultAnalyzers.LC_KEYWORD).facet());
indexConfig.addFieldConfig(FieldConfigBuilder.create("uid", FieldType.STRING).indexAs(DefaultAnalyzers.LC_KEYWORD));
indexConfig.addFieldConfig(FieldConfigBuilder.create("an", FieldType.NUMERIC_INT).index().displayName("Accession Number").sort());
indexConfig.addFieldConfig(FieldConfigBuilder.create("country", FieldType.STRING).indexAs(DefaultAnalyzers.LC_KEYWORD).facet().sort());
indexConfig.addFieldConfig(FieldConfigBuilder.create("date", FieldType.DATE).index().facetAs(DateHandling.DATE_YYYY_MM_DD).description("The very special data").sort());
indexConfig.addFieldConfig(FieldConfigBuilder.create("testList", FieldType.STRING).index());
indexConfig.setIndexName(FACET_TEST_INDEX);
indexConfig.setNumberOfShards(1);
zuliaWorkPool.createIndex(indexConfig);
zuliaWorkPool.reindex(new Reindex(FACET_TEST_INDEX));
Search search = new Search(FACET_TEST_INDEX).addCountFacet(new CountFacet("eissn"));
SearchResult searchResult = zuliaWorkPool.search(search);
List<FacetCount> eissnCounts = searchResult.getFacetCounts("eissn");
Assertions.assertEquals(eissns.length, eissnCounts.size());
for (FacetCount eissnCount : eissnCounts) {
Assertions.assertEquals(COUNT_PER_ISSN, eissnCount.getCount());
}
}
use of io.zulia.message.ZuliaQuery.FacetCount in project zuliasearch by zuliaio.
the class HierarchicalFacetTest method facetTest.
@Test
@Order(3)
public void facetTest() throws Exception {
Search search = new Search(FACET_TEST_INDEX);
search.addCountFacet(new CountFacet("path"));
search.addCountFacet(new CountFacet("date"));
SearchResult queryResult = zuliaWorkPool.search(search);
List<FacetCount> paths = queryResult.getFacetCounts("path");
for (FacetCount path : paths) {
if (path.getFacet().equals("1")) {
Assertions.assertEquals(30, path.getCount());
} else if (path.getFacet().equals("a")) {
Assertions.assertEquals(20, path.getCount());
} else if (path.getFacet().equals("2")) {
Assertions.assertEquals(10, path.getCount());
} else if (path.getFacet().equals("3")) {
Assertions.assertEquals(10, path.getCount());
} else if (path.getFacet().equals("4")) {
Assertions.assertEquals(10, path.getCount());
} else if (path.getFacet().equals("one")) {
Assertions.assertEquals(10, path.getCount());
} else {
throw new AssertionFailedError("Unexpect path <" + path.getFacet() + ">");
}
}
paths = queryResult.getFacetCounts("date");
for (FacetCount path : paths) {
if (path.getFacet().equals("2012")) {
Assertions.assertEquals(45, path.getCount());
} else if (path.getFacet().equals("2013")) {
Assertions.assertEquals(36, path.getCount());
} else if (path.getFacet().equals("2014")) {
Assertions.assertEquals(9, path.getCount());
} else {
throw new AssertionFailedError("Unexpect path <" + path.getFacet() + ">");
}
}
search = new Search(FACET_TEST_INDEX);
search.addCountFacet(new CountFacet("path", "1"));
search.addCountFacet(new CountFacet("path", "2"));
queryResult = zuliaWorkPool.search(search);
paths = queryResult.getFacetCountsForPath("path", "1");
for (FacetCount path : paths) {
if (path.getFacet().equals("2")) {
Assertions.assertEquals(10, path.getCount());
} else if (path.getFacet().equals("3")) {
Assertions.assertEquals(10, path.getCount());
} else {
throw new AssertionFailedError("Unexpect path <" + path.getFacet() + ">");
}
}
paths = queryResult.getFacetCountsForPath("path", "2");
for (FacetCount path : paths) {
if (path.getFacet().equals("3")) {
Assertions.assertEquals(10, path.getCount());
} else {
throw new AssertionFailedError("Unexpect path <" + path.getFacet() + ">");
}
}
search = new Search(FACET_TEST_INDEX);
search.addCountFacet(new CountFacet("path", "one", "two"));
queryResult = zuliaWorkPool.search(search);
paths = queryResult.getFacetCountsForPath("path", "one", "two");
for (FacetCount path : paths) {
if (path.getFacet().equals("three")) {
Assertions.assertEquals(10, path.getCount());
} else {
throw new AssertionFailedError("Unexpect path <" + path.getFacet() + ">");
}
}
search = new Search(FACET_TEST_INDEX);
search.addCountFacet(new CountFacet("path2"));
Search finalSearch = search;
Assertions.assertThrows(Exception.class, () -> zuliaWorkPool.search(finalSearch), "path2 is not defined as a facetable field");
}
use of io.zulia.message.ZuliaQuery.FacetCount in project zuliasearch by zuliaio.
the class HierarchicalFacetTest method confirm.
@Test
@Order(6)
public void confirm() throws Exception {
Search search = new Search(FACET_TEST_INDEX);
String pathField = "path2";
search.addCountFacet(new CountFacet(pathField));
search.addCountFacet(new CountFacet("date"));
SearchResult queryResult = zuliaWorkPool.search(search);
List<FacetCount> paths = queryResult.getFacetCounts(pathField);
for (FacetCount path : paths) {
if (path.getFacet().equals("1")) {
Assertions.assertEquals(30, path.getCount());
} else if (path.getFacet().equals("a")) {
Assertions.assertEquals(20, path.getCount());
} else if (path.getFacet().equals("2")) {
Assertions.assertEquals(10, path.getCount());
} else if (path.getFacet().equals("3")) {
Assertions.assertEquals(10, path.getCount());
} else if (path.getFacet().equals("4")) {
Assertions.assertEquals(10, path.getCount());
} else if (path.getFacet().equals("one")) {
Assertions.assertEquals(10, path.getCount());
} else {
throw new AssertionFailedError("Unexpect path <" + path.getFacet() + ">");
}
}
paths = queryResult.getFacetCounts("date");
for (FacetCount path : paths) {
if (path.getFacet().equals("2012")) {
Assertions.assertEquals(45, path.getCount());
} else if (path.getFacet().equals("2013")) {
Assertions.assertEquals(36, path.getCount());
} else if (path.getFacet().equals("2014")) {
Assertions.assertEquals(9, path.getCount());
} else {
throw new AssertionFailedError("Unexpect path <" + path.getFacet() + ">");
}
}
search = new Search(FACET_TEST_INDEX);
search.addCountFacet(new CountFacet(pathField, "1"));
queryResult = zuliaWorkPool.search(search);
paths = queryResult.getFacetCountsForPath(pathField, "1");
for (FacetCount path : paths) {
if (path.getFacet().equals("2")) {
Assertions.assertEquals(10, path.getCount());
} else if (path.getFacet().equals("3")) {
Assertions.assertEquals(10, path.getCount());
} else {
throw new AssertionFailedError("Unexpect path <" + path.getFacet() + ">");
}
}
search = new Search(FACET_TEST_INDEX);
search.addCountFacet(new CountFacet(pathField, "one", "two"));
queryResult = zuliaWorkPool.search(search);
paths = queryResult.getFacetCountsForPath(pathField, "one", "two");
for (FacetCount path : paths) {
if (path.getFacet().equals("three")) {
Assertions.assertEquals(10, path.getCount());
} else {
throw new AssertionFailedError("Unexpect path <" + path.getFacet() + ">");
}
}
}
Aggregations