Search in sources :

Example 16 with Search

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

the class SortTest method multiIndexTest.

@Test
@Order(14)
public void multiIndexTest() throws Exception {
    ClientIndexConfig indexConfig = new ClientIndexConfig();
    indexConfig.addDefaultSearchField("magicNumber");
    indexConfig.addFieldConfig(FieldConfigBuilder.create("id", FieldType.NUMERIC_INT).sort());
    indexConfig.addFieldConfig(FieldConfigBuilder.create("magicNumber", FieldType.NUMERIC_INT).index().sort());
    indexConfig.setIndexName("anotherIndex");
    indexConfig.setNumberOfShards(1);
    // force some commits
    indexConfig.setShardCommitInterval(20);
    zuliaWorkPool.createIndex(indexConfig);
    indexConfig.setIndexName("anotherIndex2");
    zuliaWorkPool.createIndex(indexConfig);
    for (int id = 0; id < 200; id++) {
        int magicNumber = 7;
        if (id > 10) {
            magicNumber = -1;
        }
        Document mongoDocument = new Document().append("id", id).append("magicNumber", magicNumber);
        zuliaWorkPool.store(new Store(id + "", "anotherIndex", ResultDocBuilder.from(mongoDocument)));
    }
    for (int id = 0; id < 100; id++) {
        int magicNumber = (id % 10) + 5;
        Document mongoDocument = new Document().append("id", id).append("magicNumber", magicNumber);
        zuliaWorkPool.store(new Store(id + "", "anotherIndex2", ResultDocBuilder.from(mongoDocument)));
    }
    SearchResult searchResult;
    Search search = new Search("anotherIndex", "anotherIndex2").setAmount(1);
    search.addSort(new Sort("magicNumber"));
    searchResult = zuliaWorkPool.search(search);
    Assertions.assertEquals(-1, searchResult.getFirstDocument().get("magicNumber"));
    search.clearSort();
    search.addSort(new Sort("magicNumber").descending());
    searchResult = zuliaWorkPool.search(search);
    Assertions.assertEquals(14, searchResult.getFirstDocument().get("magicNumber"));
}
Also used : Search(io.zulia.client.command.builder.Search) ClientIndexConfig(io.zulia.client.config.ClientIndexConfig) Store(io.zulia.client.command.Store) Sort(io.zulia.client.command.builder.Sort) SearchResult(io.zulia.client.result.SearchResult) Document(org.bson.Document) Order(org.junit.jupiter.api.Order) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Test(org.junit.jupiter.api.Test)

Example 17 with Search

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

the class SortTest method titleSort.

@Test
@Order(2)
public void titleSort() throws Exception {
    SearchResult searchResult;
    Search search = new Search(INDEX_NAME).setAmount(10);
    // default ascending missing first
    search.addSort(new Sort("title"));
    searchResult = zuliaWorkPool.search(search);
    Assertions.assertNull(searchResult.getFirstDocument().get("title"));
    search.clearSort();
    // default missing first
    search.addSort(new Sort("title").ascending());
    searchResult = zuliaWorkPool.search(search);
    Assertions.assertNull(searchResult.getFirstDocument().get("title"));
    search.clearSort();
    search.addSort(new Sort("title").ascending().missingFirst());
    searchResult = zuliaWorkPool.search(search);
    Assertions.assertNull(searchResult.getFirstDocument().get("title"));
    search.clearSort();
    search.addSort(new Sort("title").ascending().missingLast());
    searchResult = zuliaWorkPool.search(search);
    Assertions.assertEquals("a special title", searchResult.getFirstDocument().get("title"));
    search.clearSort();
    // default missing first
    search.addSort(new Sort("title").descending());
    searchResult = zuliaWorkPool.search(search);
    Assertions.assertEquals("some title", searchResult.getFirstDocument().get("title"));
    search.clearSort();
    search.addSort(new Sort("title").descending().missingFirst());
    searchResult = zuliaWorkPool.search(search);
    Assertions.assertEquals("some title", searchResult.getFirstDocument().get("title"));
    search.clearSort();
    search.addSort(new Sort("title").descending().missingLast());
    searchResult = zuliaWorkPool.search(search);
    Assertions.assertNull(searchResult.getFirstDocument().get("title"));
}
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)

Example 18 with Search

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

the class SortTest method scoreSort.

@Test
@Order(9)
public void scoreSort() throws Exception {
    SearchResult searchResult;
    Search search = new Search(INDEX_NAME).setAmount(10).addQuery(new ScoredQuery("title:special OR title:secret"));
    searchResult = zuliaWorkPool.search(search);
    Assertions.assertEquals("40", searchResult.getFirstDocument().get("id"));
    search.clearSort();
    // default ascending
    search.addSort(new Sort(ZuliaConstants.SCORE_FIELD));
    searchResult = zuliaWorkPool.search(search);
    Assertions.assertEquals("30", searchResult.getFirstDocument().get("id"));
    search.clearSort();
    search.addSort(new Sort(ZuliaConstants.SCORE_FIELD).descending());
    searchResult = zuliaWorkPool.search(search);
    Assertions.assertEquals("40", searchResult.getFirstDocument().get("id"));
}
Also used : ScoredQuery(io.zulia.client.command.builder.ScoredQuery) 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)

Example 19 with Search

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

the class SortTest method ratingSort.

@Test
@Order(5)
public void ratingSort() throws Exception {
    SearchResult searchResult;
    Search search = new Search(INDEX_NAME).setAmount(10);
    String field = "rating";
    // 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);
    // mongodb bson does not support float so it comes back as a double, the search engine is indexing in float precision however
    Assertions.assertEquals(1.1f, (double) searchResult.getFirstDocument().get(field), 0.001f);
    search.clearSort();
    // default missing first
    search.addSort(new Sort(field).descending());
    searchResult = zuliaWorkPool.search(search);
    Assertions.assertEquals(5.0f, (double) searchResult.getFirstDocument().get(field), 0.001f);
    search.clearSort();
    search.addSort(new Sort(field).descending().missingFirst());
    searchResult = zuliaWorkPool.search(search);
    Assertions.assertEquals(5.0f, (double) searchResult.getFirstDocument().get(field), 0.001f);
    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)

Example 20 with Search

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

the class SortTest method reindexTest.

@Test
@Order(13)
public void reindexTest() throws Exception {
    ClientIndexConfig indexConfig = new ClientIndexConfig();
    indexConfig.addDefaultSearchField("title");
    indexConfig.addFieldConfig(FieldConfigBuilder.create("id", FieldType.STRING).indexAs(DefaultAnalyzers.LC_KEYWORD).sortAs(// change sort as to be theId instead of just id
    "theId"));
    indexConfig.addFieldConfig(FieldConfigBuilder.create("title", FieldType.STRING).indexAs(DefaultAnalyzers.STANDARD).sort());
    // no longer sortable
    indexConfig.addFieldConfig(FieldConfigBuilder.create("stars", FieldType.NUMERIC_INT).index());
    indexConfig.addFieldConfig(FieldConfigBuilder.create("starsLong", FieldType.NUMERIC_LONG).index().sort());
    // indexConfig.addFieldConfig(FieldConfigBuilder.create("rating", FieldType.NUMERIC_FLOAT).index().sort()); // no longer indexed or sortable
    indexConfig.addFieldConfig(FieldConfigBuilder.create("ratingDouble", FieldType.NUMERIC_FLOAT).index().sort());
    indexConfig.addFieldConfig(FieldConfigBuilder.create("special", FieldType.BOOL).index().sort());
    indexConfig.addFieldConfig(FieldConfigBuilder.create("added", FieldType.DATE).index().sort());
    // sort() adds standard string (case senstive sorting with a field name the same as the stored field
    // sortAs(LOWERCASE_FOLDING, "otherTitleFolding") add another sortable field with a lowercase and ascii folding filter applied to make case insensitive sort and fancy letter insensitive (gotta be a better term here)
    indexConfig.addFieldConfig(FieldConfigBuilder.create("otherTitle", FieldType.STRING).index().sort().sortAs(LOWERCASE_FOLDING, "otherTitleFolding"));
    indexConfig.setIndexName(INDEX_NAME);
    indexConfig.setNumberOfShards(1);
    // force some commits
    indexConfig.setShardCommitInterval(20);
    zuliaWorkPool.createIndex(indexConfig);
    zuliaWorkPool.reindex(new Reindex(INDEX_NAME));
    SearchResult searchResult;
    Search search = new Search(INDEX_NAME).setAmount(1);
    search.addSort(new Sort("otherTitle").ascending().missingLast());
    searchResult = zuliaWorkPool.search(search);
    Assertions.assertEquals("Blah", searchResult.getFirstDocument().get("otherTitle"));
    search.clearSort();
    search.addSort(new Sort("otherTitleFolding").ascending().missingLast());
    search.addSort(new Sort("starts").ascending().missingLast());
    searchResult = zuliaWorkPool.search(search);
    Assertions.assertEquals("blah", searchResult.getFirstDocument().get("otherTitle"));
    search.clearSort();
    search.addSort(new Sort("otherTitle").descending());
    searchResult = zuliaWorkPool.search(search);
    Assertions.assertEquals("still more blah", searchResult.getFirstDocument().get("otherTitle"));
    search.clearSort();
    // use the new sort as id field
    search.addSort(new Sort("theId").descending());
    searchResult = zuliaWorkPool.search(search);
    // sorting as string so this is 99 instead of 199
    Assertions.assertEquals("99", searchResult.getFirstDocument().get("id"));
}
Also used : Search(io.zulia.client.command.builder.Search) ClientIndexConfig(io.zulia.client.config.ClientIndexConfig) Sort(io.zulia.client.command.builder.Sort) SearchResult(io.zulia.client.result.SearchResult) Reindex(io.zulia.client.command.Reindex) 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