Search in sources :

Example 1 with StatFacet

use of io.zulia.client.command.builder.StatFacet in project zuliasearch by zuliaio.

the class StatTest method statTest.

@Test
@Order(3)
public void statTest() throws Exception {
    Search search = new Search(STAT_TEST_INDEX);
    search.addStat(new NumericStat("rating"));
    search.addQuery(new FilterQuery("title:boring").exclude());
    search.addQuery(new MatchAllQuery());
    SearchResult searchResult = zuliaWorkPool.search(search);
    System.out.println("Hits: " + searchResult.getTotalHits());
    FacetStats ratingStat = searchResult.getNumericFieldStat("rating");
    Assertions.assertEquals(0.5, ratingStat.getMin().getDoubleValue(), 0.001);
    Assertions.assertEquals(3.5, ratingStat.getMax().getDoubleValue(), 0.001);
    Assertions.assertEquals(10.5 * repeatCount, ratingStat.getSum().getDoubleValue(), 0.001);
    Assertions.assertEquals(4L * repeatCount, ratingStat.getDocCount());
    Assertions.assertEquals(6L * repeatCount, ratingStat.getAllDocCount());
    Assertions.assertEquals(5L * repeatCount, ratingStat.getValueCount());
    search.clearStat();
    search.addStat(new StatFacet("rating", "normalFacet"));
    searchResult = zuliaWorkPool.search(search);
    List<FacetStats> ratingByFacet = searchResult.getFacetFieldStat("rating", "normalFacet");
    for (FacetStats facetStats : ratingByFacet) {
        if (facetStats.getFacet().equals("foo")) {
            Assertions.assertEquals(1, facetStats.getMin().getDoubleValue(), 0.001);
            Assertions.assertEquals(3.5, facetStats.getMax().getDoubleValue(), 0.001);
            Assertions.assertEquals(7L * repeatCount, facetStats.getSum().getDoubleValue(), 0.001);
            Assertions.assertEquals(2L * repeatCount, facetStats.getDocCount());
            Assertions.assertEquals(3L * repeatCount, facetStats.getAllDocCount());
            Assertions.assertEquals(3L * repeatCount, facetStats.getValueCount());
        } else if (facetStats.getFacet().equals("bar")) {
            Assertions.assertEquals(0.5, facetStats.getMin().getDoubleValue(), 0.001);
            Assertions.assertEquals(3.0, facetStats.getMax().getDoubleValue(), 0.001);
            Assertions.assertEquals(3.5 * repeatCount, facetStats.getSum().getDoubleValue(), 0.001);
            Assertions.assertEquals(2L * repeatCount, facetStats.getDocCount());
            Assertions.assertEquals(2L * repeatCount, facetStats.getAllDocCount());
            Assertions.assertEquals(2L * repeatCount, facetStats.getValueCount());
        } else {
            throw new AssertionFailedError("Unexpect facet <" + facetStats.getFacet() + ">");
        }
    }
    search.clearStat();
    search.addStat(new StatFacet("rating", "pathFacet"));
    searchResult = zuliaWorkPool.search(search);
    List<FacetStats> ratingByPathFacet = searchResult.getFacetFieldStat("rating", "pathFacet");
    for (FacetStats facetStats : ratingByPathFacet) {
        if (facetStats.getFacet().equals("top1")) {
            Assertions.assertEquals(1, facetStats.getMin().getDoubleValue(), 0.001);
            Assertions.assertEquals(3.5, facetStats.getMax().getDoubleValue(), 0.001);
            Assertions.assertEquals(7L * repeatCount, facetStats.getSum().getDoubleValue(), 0.001);
            Assertions.assertEquals(2L * repeatCount, facetStats.getDocCount());
            Assertions.assertEquals(3L * repeatCount, facetStats.getAllDocCount());
            Assertions.assertEquals(3L * repeatCount, facetStats.getValueCount());
        } else if (facetStats.getFacet().equals("top2")) {
            Assertions.assertEquals(0.5, facetStats.getMin().getDoubleValue(), 0.001);
            Assertions.assertEquals(0.5, facetStats.getMax().getDoubleValue(), 0.001);
            Assertions.assertEquals(0.5 * repeatCount, facetStats.getSum().getDoubleValue(), 0.001);
            Assertions.assertEquals(repeatCount, facetStats.getDocCount());
            Assertions.assertEquals(repeatCount, facetStats.getAllDocCount());
            Assertions.assertEquals(repeatCount, facetStats.getValueCount());
        } else if (facetStats.getFacet().equals("top3")) {
            Assertions.assertEquals(3.0, facetStats.getMin().getDoubleValue(), 0.001);
            Assertions.assertEquals(3.0, facetStats.getMax().getDoubleValue(), 0.001);
            Assertions.assertEquals(3.0 * repeatCount, facetStats.getSum().getDoubleValue(), 0.001);
            Assertions.assertEquals(repeatCount, facetStats.getDocCount());
            Assertions.assertEquals(repeatCount, facetStats.getValueCount());
        } else {
            throw new AssertionFailedError("Unexpect facet <" + facetStats.getFacet() + ">");
        }
    }
    search = new Search(STAT_TEST_INDEX);
    search.addStat(new StatFacet("authorCount", "pathFacet"));
    Search finalSearch = search;
    Assertions.assertThrows(Exception.class, () -> zuliaWorkPool.search(finalSearch), "Expecting: Search: Numeric field <authorCount> must be indexed as a SORTABLE numeric field");
}
Also used : Search(io.zulia.client.command.builder.Search) NumericStat(io.zulia.client.command.builder.NumericStat) FilterQuery(io.zulia.client.command.builder.FilterQuery) SearchResult(io.zulia.client.result.SearchResult) AssertionFailedError(org.opentest4j.AssertionFailedError) StatFacet(io.zulia.client.command.builder.StatFacet) MatchAllQuery(io.zulia.client.command.builder.MatchAllQuery) FacetStats(io.zulia.message.ZuliaQuery.FacetStats) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Order(org.junit.jupiter.api.Order) Test(org.junit.jupiter.api.Test)

Example 2 with StatFacet

use of io.zulia.client.command.builder.StatFacet in project zuliasearch by zuliaio.

the class StatTest method confirm.

@Test
@Order(6)
public void confirm() throws Exception {
    Search search = new Search(STAT_TEST_INDEX);
    search.addQuery(new FilterQuery("title:boring").exclude());
    search.addQuery(new MatchAllQuery());
    search.addStat(new NumericStat("authorCount"));
    SearchResult searchResult = zuliaWorkPool.search(search);
    FacetStats authorCountStats = searchResult.getNumericFieldStat("authorCount");
    Assertions.assertEquals(2, authorCountStats.getMin().getLongValue());
    Assertions.assertEquals(5, authorCountStats.getMax().getLongValue());
    Assertions.assertEquals(20L * repeatCount, authorCountStats.getSum().getLongValue());
    Assertions.assertEquals(6L * repeatCount, authorCountStats.getDocCount());
    Assertions.assertEquals(6L * repeatCount, authorCountStats.getValueCount());
    search.clearStat();
    search.addStat(new StatFacet("authorCount", "normalFacet"));
    searchResult = zuliaWorkPool.search(search);
    List<FacetStats> authorCountByNormalFacet = searchResult.getFacetFieldStat("authorCount", "normalFacet");
    for (FacetStats facetStats : authorCountByNormalFacet) {
        if (facetStats.getFacet().equals("foo")) {
            Assertions.assertEquals(2L, facetStats.getMin().getLongValue());
            Assertions.assertEquals(4L, facetStats.getMax().getLongValue());
            Assertions.assertEquals(9L * repeatCount, facetStats.getSum().getLongValue());
            Assertions.assertEquals(3L * repeatCount, facetStats.getDocCount());
            Assertions.assertEquals(3L * repeatCount, facetStats.getAllDocCount());
            Assertions.assertEquals(3L * repeatCount, facetStats.getValueCount());
        } else if (facetStats.getFacet().equals("bar")) {
            Assertions.assertEquals(2L, facetStats.getMin().getLongValue());
            Assertions.assertEquals(5L, facetStats.getMax().getLongValue());
            Assertions.assertEquals(7L * repeatCount, facetStats.getSum().getLongValue());
            Assertions.assertEquals(2L * repeatCount, facetStats.getDocCount());
            Assertions.assertEquals(2L * repeatCount, facetStats.getAllDocCount());
            Assertions.assertEquals(2L * repeatCount, facetStats.getValueCount());
        } else {
            throw new AssertionFailedError("Unexpected facet <" + facetStats.getFacet() + ">");
        }
    }
    search.clearStat();
    search.addStat(new StatFacet("authorCount", "pathFacet"));
    searchResult = zuliaWorkPool.search(search);
    List<FacetStats> ratingByPathFacet = searchResult.getFacetFieldStat("authorCount", "pathFacet");
    for (FacetStats facetStats : ratingByPathFacet) {
        if (facetStats.getFacet().equals("top1")) {
            Assertions.assertEquals(2L, facetStats.getMin().getLongValue());
            Assertions.assertEquals(4L, facetStats.getMax().getLongValue());
            Assertions.assertEquals(9L * repeatCount, facetStats.getSum().getLongValue());
            Assertions.assertEquals(3L * repeatCount, facetStats.getDocCount());
            Assertions.assertEquals(3L * repeatCount, facetStats.getAllDocCount());
            Assertions.assertEquals(3L * repeatCount, facetStats.getValueCount());
        } else if (facetStats.getFacet().equals("top2")) {
            Assertions.assertEquals(2L, facetStats.getMin().getLongValue());
            Assertions.assertEquals(2L, facetStats.getMax().getLongValue());
            Assertions.assertEquals(2L * repeatCount, facetStats.getSum().getLongValue());
            Assertions.assertEquals(repeatCount, facetStats.getDocCount());
            Assertions.assertEquals(repeatCount, facetStats.getAllDocCount());
            Assertions.assertEquals(repeatCount, facetStats.getValueCount());
        } else if (facetStats.getFacet().equals("top3")) {
            Assertions.assertEquals(4L, facetStats.getMin().getLongValue());
            Assertions.assertEquals(5L, facetStats.getMax().getLongValue());
            Assertions.assertEquals(9L * repeatCount, facetStats.getSum().getLongValue());
            Assertions.assertEquals(2L * repeatCount, facetStats.getDocCount());
            Assertions.assertEquals(2L * repeatCount, facetStats.getAllDocCount());
            Assertions.assertEquals(2L * repeatCount, facetStats.getValueCount());
        } else {
            throw new AssertionFailedError("Unexpected facet <" + facetStats.getFacet() + ">");
        }
    }
}
Also used : Search(io.zulia.client.command.builder.Search) NumericStat(io.zulia.client.command.builder.NumericStat) FilterQuery(io.zulia.client.command.builder.FilterQuery) SearchResult(io.zulia.client.result.SearchResult) AssertionFailedError(org.opentest4j.AssertionFailedError) StatFacet(io.zulia.client.command.builder.StatFacet) MatchAllQuery(io.zulia.client.command.builder.MatchAllQuery) FacetStats(io.zulia.message.ZuliaQuery.FacetStats) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Order(org.junit.jupiter.api.Order) Test(org.junit.jupiter.api.Test)

Aggregations

FilterQuery (io.zulia.client.command.builder.FilterQuery)2 MatchAllQuery (io.zulia.client.command.builder.MatchAllQuery)2 NumericStat (io.zulia.client.command.builder.NumericStat)2 Search (io.zulia.client.command.builder.Search)2 StatFacet (io.zulia.client.command.builder.StatFacet)2 SearchResult (io.zulia.client.result.SearchResult)2 FacetStats (io.zulia.message.ZuliaQuery.FacetStats)2 Order (org.junit.jupiter.api.Order)2 Test (org.junit.jupiter.api.Test)2 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)2 AssertionFailedError (org.opentest4j.AssertionFailedError)2