Search in sources :

Example 21 with Search

use of io.zulia.client.command.builder.Search 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());
    }
}
Also used : Search(io.zulia.client.command.builder.Search) ClientIndexConfig(io.zulia.client.config.ClientIndexConfig) SearchResult(io.zulia.client.result.SearchResult) FacetCount(io.zulia.message.ZuliaQuery.FacetCount) Reindex(io.zulia.client.command.Reindex) CountFacet(io.zulia.client.command.builder.CountFacet) Order(org.junit.jupiter.api.Order) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Test(org.junit.jupiter.api.Test)

Example 22 with Search

use of io.zulia.client.command.builder.Search 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)

Example 23 with Search

use of io.zulia.client.command.builder.Search 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");
}
Also used : Search(io.zulia.client.command.builder.Search) SearchResult(io.zulia.client.result.SearchResult) FacetCount(io.zulia.message.ZuliaQuery.FacetCount) AssertionFailedError(org.opentest4j.AssertionFailedError) CountFacet(io.zulia.client.command.builder.CountFacet) Order(org.junit.jupiter.api.Order) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Test(org.junit.jupiter.api.Test)

Example 24 with Search

use of io.zulia.client.command.builder.Search 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() + ">");
        }
    }
}
Also used : Search(io.zulia.client.command.builder.Search) SearchResult(io.zulia.client.result.SearchResult) FacetCount(io.zulia.message.ZuliaQuery.FacetCount) AssertionFailedError(org.opentest4j.AssertionFailedError) CountFacet(io.zulia.client.command.builder.CountFacet) Order(org.junit.jupiter.api.Order) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Test(org.junit.jupiter.api.Test)

Example 25 with Search

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

the class SortTest method dateSort.

@Test
@Order(8)
public void dateSort() throws Exception {
    SearchResult searchResult;
    Search search = new Search(INDEX_NAME).setAmount(10);
    String field = "added";
    // default ascending missing first
    search.addSort(new Sort(field));
    searchResult = zuliaWorkPool.search(search);
    Assertions.assertNull(searchResult.getFirstDocument().get(field));
    search.clearSort();
    // default missing first
    search.addSort(new Sort(field).ascending());
    searchResult = zuliaWorkPool.search(search);
    Assertions.assertNull(searchResult.getFirstDocument().get(field));
    search.clearSort();
    search.addSort(new Sort(field).ascending().missingFirst());
    searchResult = zuliaWorkPool.search(search);
    Assertions.assertNull(searchResult.getFirstDocument().get(field));
    search.clearSort();
    search.addSort(new Sort(field).ascending().missingLast());
    searchResult = zuliaWorkPool.search(search);
    Assertions.assertEquals(Date.from(LocalDate.of(1951, Month.DECEMBER, 20).atStartOfDay(ZoneId.of("UTC")).toInstant()), searchResult.getFirstDocument().get(field));
    search.clearSort();
    // default missing first
    search.addSort(new Sort(field).descending());
    searchResult = zuliaWorkPool.search(search);
    Assertions.assertEquals(Date.from(LocalDate.of(2020, Month.JANUARY, 31).atStartOfDay(ZoneId.of("UTC")).toInstant()), searchResult.getFirstDocument().get(field));
    search.clearSort();
    search.addSort(new Sort(field).descending().missingFirst());
    searchResult = zuliaWorkPool.search(search);
    Assertions.assertEquals(Date.from(LocalDate.of(2020, Month.JANUARY, 31).atStartOfDay(ZoneId.of("UTC")).toInstant()), searchResult.getFirstDocument().get(field));
    search.clearSort();
    search.addSort(new Sort(field).descending().missingLast());
    searchResult = zuliaWorkPool.search(search);
    Assertions.assertNull(searchResult.getFirstDocument().get(field));
}
Also used : Search(io.zulia.client.command.builder.Search) Sort(io.zulia.client.command.builder.Sort) SearchResult(io.zulia.client.result.SearchResult) Order(org.junit.jupiter.api.Order) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Test(org.junit.jupiter.api.Test)

Aggregations

Search (io.zulia.client.command.builder.Search)25 SearchResult (io.zulia.client.result.SearchResult)24 Order (org.junit.jupiter.api.Order)22 Test (org.junit.jupiter.api.Test)22 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)22 Sort (io.zulia.client.command.builder.Sort)15 FilterQuery (io.zulia.client.command.builder.FilterQuery)6 CountFacet (io.zulia.client.command.builder.CountFacet)5 ScoredQuery (io.zulia.client.command.builder.ScoredQuery)5 Reindex (io.zulia.client.command.Reindex)3 MatchAllQuery (io.zulia.client.command.builder.MatchAllQuery)3 ClientIndexConfig (io.zulia.client.config.ClientIndexConfig)3 FacetCount (io.zulia.message.ZuliaQuery.FacetCount)3 Document (org.bson.Document)3 AssertionFailedError (org.opentest4j.AssertionFailedError)3 NumericStat (io.zulia.client.command.builder.NumericStat)2 StatFacet (io.zulia.client.command.builder.StatFacet)2 GetFieldsResult (io.zulia.client.result.GetFieldsResult)2 GetIndexesResult (io.zulia.client.result.GetIndexesResult)2 GetNodesResult (io.zulia.client.result.GetNodesResult)2