Search in sources :

Example 1 with ScoredQuery

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

the class StartStopTest method sortScoreBuilder.

@Test
@Order(3)
public void sortScoreBuilder() throws Exception {
    Search search = new Search(FACET_TEST_INDEX).setAmount(10);
    search.addQuery(new ScoredQuery("issn:\"1234-1234\" OR country:US"));
    search.addSort(new Sort(ZuliaConstants.SCORE_FIELD).ascending());
    SearchResult searchResult = zuliaWorkPool.search(search);
    double lowScore = -1;
    double highScore = -1;
    for (ZuliaQuery.ScoredResult result : searchResult.getResults()) {
        Assertions.assertTrue(result.getScore() > 0);
        if (lowScore < 0 || result.getScore() < lowScore) {
            lowScore = result.getScore();
        }
    }
    search.clearSort();
    search.addSort(new Sort(ZuliaConstants.SCORE_FIELD).descending());
    searchResult = zuliaWorkPool.search(search);
    for (ZuliaQuery.ScoredResult result : searchResult.getResults()) {
        Assertions.assertTrue(result.getScore() > 0);
        if (highScore < 0 || result.getScore() > highScore) {
            highScore = result.getScore();
        }
    }
    Assertions.assertTrue(highScore > lowScore);
}
Also used : ScoredQuery(io.zulia.client.command.builder.ScoredQuery) Search(io.zulia.client.command.builder.Search) ZuliaQuery(io.zulia.message.ZuliaQuery) 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 2 with ScoredQuery

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

the class AliasTest method aliasSearchTest.

@Test
@Order(3)
public void aliasSearchTest() throws Exception {
    Search search = new Search(ALIAS_TEST_INDEX);
    SearchResult searchResult = zuliaWorkPool.search(search);
    Assertions.assertEquals(repeatCount * 2 * uniqueDocs, searchResult.getTotalHits());
    search = new Search("someAlias");
    searchResult = zuliaWorkPool.search(search);
    Assertions.assertEquals(repeatCount * 2 * uniqueDocs, searchResult.getTotalHits());
    search = new Search(ALIAS_TEST_INDEX);
    search.addQuery(new ScoredQuery("rating:[4.0 TO *]"));
    searchResult = zuliaWorkPool.search(search);
    Assertions.assertEquals(repeatCount * 2 * 3, searchResult.getTotalHits());
    search = new Search("someAlias");
    search.addQuery(new ScoredQuery("rating:[4.0 TO *]"));
    searchResult = zuliaWorkPool.search(search);
    Assertions.assertEquals(repeatCount * 2 * 3, searchResult.getTotalHits());
    GetNumberOfDocsResult numberOfDocs = zuliaWorkPool.getNumberOfDocs("someAlias");
    Assertions.assertEquals(repeatCount * 2 * uniqueDocs, numberOfDocs.getNumberOfDocs());
    GetFieldsResult getFields = zuliaWorkPool.getFields("someAlias");
    Assertions.assertEquals(3, getFields.getFieldNames().size());
    GetTermsResult getTermsResult = zuliaWorkPool.execute(new GetTerms("someAlias", "title"));
    Assertions.assertEquals(6, getTermsResult.getTerms().size());
    GetIndexesResult indexes = zuliaWorkPool.getIndexes();
    Assertions.assertEquals(1, indexes.getIndexNames().size());
    GetNodesResult nodes = zuliaWorkPool.getNodes();
    Assertions.assertEquals(1, nodes.getIndexAliases().size());
    FetchResult fetch = zuliaWorkPool.fetch(new Fetch("1", "someAlias"));
    Assertions.assertEquals("1", fetch.getDocument().getString("id"));
    GetIndexConfigResult indexConfig = zuliaWorkPool.getIndexConfig("someAlias");
    Assertions.assertEquals(indexConfig.getIndexConfig().getIndexName(), ALIAS_TEST_INDEX);
}
Also used : Fetch(io.zulia.client.command.Fetch) ScoredQuery(io.zulia.client.command.builder.ScoredQuery) GetNodesResult(io.zulia.client.result.GetNodesResult) FetchResult(io.zulia.client.result.FetchResult) GetIndexConfigResult(io.zulia.client.result.GetIndexConfigResult) Search(io.zulia.client.command.builder.Search) GetIndexesResult(io.zulia.client.result.GetIndexesResult) GetNumberOfDocsResult(io.zulia.client.result.GetNumberOfDocsResult) SearchResult(io.zulia.client.result.SearchResult) GetFieldsResult(io.zulia.client.result.GetFieldsResult) GetTerms(io.zulia.client.command.GetTerms) GetTermsResult(io.zulia.client.result.GetTermsResult) Order(org.junit.jupiter.api.Order) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Test(org.junit.jupiter.api.Test)

Example 3 with ScoredQuery

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

the class Zulia method main.

public static void main(String[] args) {
    LogUtil.init();
    ZuliaBaseArgs zuliaArgs = new ZuliaBaseArgs();
    GetIndexesCmd getIndexesCmd = new GetIndexesCmd();
    VersionCmd version = new VersionCmd();
    ClearCmd clear = new ClearCmd();
    OptimizeCmd optimize = new OptimizeCmd();
    GetCountCmd getCount = new GetCountCmd();
    GetCurrentNodesCmd getCurrentNodes = new GetCurrentNodesCmd();
    GetFieldsCmd getFields = new GetFieldsCmd();
    DeleteCmd delete = new DeleteCmd();
    ReindexCmd reindex = new ReindexCmd();
    QueryCmd query = new QueryCmd();
    JCommander jCommander = JCommander.newBuilder().addObject(zuliaArgs).addCommand(version).addCommand(getIndexesCmd).addCommand(query).addCommand(clear).addCommand(getCount).addCommand(getCurrentNodes).addCommand(getFields).addCommand(delete).addCommand(reindex).addCommand(optimize).build();
    try {
        jCommander.parse(args);
        if (jCommander.getParsedCommand() == null) {
            jCommander.usage();
            System.exit(2);
        }
        ZuliaPoolConfig config = new ZuliaPoolConfig().addNode(zuliaArgs.address, zuliaArgs.port);
        ZuliaWorkPool workPool = new ZuliaWorkPool(config);
        if ("--version".equalsIgnoreCase(jCommander.getParsedCommand())) {
            System.out.println(ZuliaVersion.getVersion());
            System.exit(0);
        }
        if ("getIndexes".equalsIgnoreCase(jCommander.getParsedCommand())) {
            GetIndexes getIndexes = new GetIndexes();
            GetIndexesResult execute = workPool.execute(getIndexes);
            System.out.println(execute.getIndexNames());
            System.exit(0);
        }
        String index = zuliaArgs.index;
        if (index == null) {
            System.err.println("Please pass in an index.");
            jCommander.usage();
            System.exit(2);
        }
        if ("query".equals(jCommander.getParsedCommand())) {
            Search search;
            if (query.indexes != null) {
                search = new Search(query.indexes);
            } else {
                search = new Search(index);
            }
            search.setAmount(query.rows);
            ScoredQuery scoredQuery = new ScoredQuery(query.q);
            if (query.qf != null) {
                query.qf.forEach(scoredQuery::addQueryField);
            }
            if (query.minimumNumberShouldMatch != null) {
                scoredQuery.setMinShouldMatch(query.minimumNumberShouldMatch);
            }
            search.addQuery(scoredQuery);
            search.setStart(query.start);
            if (query.fetch.equalsIgnoreCase("full")) {
                search.setResultFetchType(ZuliaQuery.FetchType.FULL);
            }
            if (query.facets != null) {
                for (String facet : query.facets) {
                    search.addCountFacet(new CountFacet(facet).setTopN(query.facetCount).setTopNShard(query.facetShardCount));
                }
            }
            if (query.sortFields != null) {
                for (String sortField : query.sortFields) {
                    search.addSort(new Sort(sortField));
                }
                ;
            }
            if (query.sortDescFields != null) {
                for (String sortDesc : query.sortDescFields) {
                    search.addSort(new Sort(sortDesc).descending());
                }
            }
            if (query.fq != null) {
                for (String filterQuery : query.fq) {
                    search.addQuery(new FilterQuery(filterQuery));
                }
            }
            if (query.fl != null) {
                query.fl.forEach(search::addDocumentField);
            }
            if (query.flMask != null) {
                query.flMask.forEach(search::addDocumentMaskedField);
            }
            SearchResult searchResult = workPool.search(search);
            List<ZuliaQuery.ScoredResult> srList = searchResult.getResults();
            System.out.println("QueryTime: " + (searchResult.getCommandTimeMs()) + "ms");
            System.out.println("TotalResults: " + searchResult.getTotalHits());
            System.out.println("Results:");
            System.out.print("UniqueId");
            System.out.print("\t");
            System.out.print("Score");
            System.out.print("\t");
            System.out.print("Index");
            System.out.print("\t");
            System.out.print("Shard");
            System.out.print("\t");
            System.out.print("LuceneShardId");
            System.out.print("\t");
            System.out.print("Sort");
            System.out.print("\t");
            if (query.fetch.equalsIgnoreCase("full")) {
                System.out.print("Document");
            }
            System.out.println();
            for (ZuliaQuery.ScoredResult sr : srList) {
                System.out.print(sr.getUniqueId());
                System.out.print("\t");
                System.out.print(df.format(sr.getScore()));
                System.out.print("\t");
                System.out.print(sr.getIndexName());
                System.out.print("\t");
                System.out.print(sr.getShard());
                System.out.print("\t");
                System.out.print(sr.getLuceneShardId());
                System.out.print("\t");
                StringBuffer sb = new StringBuffer();
                if (sr.hasSortValues()) {
                    for (ZuliaQuery.SortValue sortValue : sr.getSortValues().getSortValueList()) {
                        if (sb.length() != 0) {
                            sb.append(",");
                        }
                        if (sortValue.getExists()) {
                            if (sortValue.getDateValue() != 0) {
                                sb.append(new Date(sortValue.getDateValue()));
                            } else if (sortValue.getDoubleValue() != 0) {
                                sb.append(sortValue.getDoubleValue());
                            } else if (sortValue.getFloatValue() != 0) {
                                sb.append(sortValue.getFloatValue());
                            } else if (sortValue.getIntegerValue() != 0) {
                                sb.append(sortValue.getIntegerValue());
                            } else if (sortValue.getLongValue() != 0) {
                                sb.append(sortValue.getLongValue());
                            } else if (sortValue.getStringValue() != null) {
                                sb.append(sortValue.getStringValue());
                            }
                        } else {
                            sb.append("!NULL!");
                        }
                    }
                }
                if (sb.length() != 0) {
                    System.out.print(sb);
                } else {
                    System.out.print("--");
                }
                if (query.fetch != null && query.fetch.equalsIgnoreCase("full")) {
                    System.out.print("\t");
                    if (sr.hasResultDocument()) {
                        ZuliaBase.ResultDocument resultDocument = sr.getResultDocument();
                        if (resultDocument.getDocument() != null) {
                            Document mongoDocument = new Document();
                            mongoDocument.putAll(ZuliaUtil.byteArrayToMongoDocument(resultDocument.getDocument().toByteArray()));
                            System.out.println(mongoDocument.toJson());
                        }
                    }
                }
                System.out.println();
            }
            if (!searchResult.getFacetGroups().isEmpty()) {
                System.out.println("Facets:");
                for (ZuliaQuery.FacetGroup fg : searchResult.getFacetGroups()) {
                    System.out.println();
                    System.out.println("--Facet on " + fg.getCountRequest().getFacetField().getLabel() + "--");
                    for (ZuliaQuery.FacetCount fc : fg.getFacetCountList()) {
                        System.out.print(fc.getFacet());
                        System.out.print("\t");
                        System.out.print(fc.getCount());
                        System.out.print("\t");
                        System.out.print("+" + fc.getMaxError());
                        System.out.println();
                    }
                    if (fg.getPossibleMissing()) {
                        System.out.println("Possible facets missing from top results for <" + fg.getCountRequest().getFacetField().getLabel() + "> with max count <" + fg.getMaxValuePossibleMissing() + ">");
                    }
                }
            }
        } else if ("clear".equals(jCommander.getParsedCommand())) {
            System.out.println("Clearing index: " + index);
            ClearIndexResult response = workPool.execute(new ClearIndex(index));
            System.out.println("Cleared index: " + index);
        } else if ("getCount".equals(jCommander.getParsedCommand())) {
            GetNumberOfDocsResult response = workPool.execute(new GetNumberOfDocs(index));
            System.out.println("Shards: " + response.getShardCountResponseCount());
            System.out.println("Count: " + response.getNumberOfDocs());
            for (ZuliaBase.ShardCountResponse scr : response.getShardCountResponses()) {
                System.out.println("Shard [" + scr.getShardNumber() + "] Count:\n" + scr.getNumberOfDocs());
            }
        } else if ("getCurrentNodes".equals(jCommander.getParsedCommand())) {
            GetNodesResult response = workPool.execute(new GetNodes());
            System.out.println("serverAddress\tservicePort\theartBeat\trestPort\tversion");
            for (ZuliaBase.Node val : response.getNodes()) {
                String nodeVersion = val.getVersion();
                if (nodeVersion == null || nodeVersion.isEmpty()) {
                    nodeVersion = "< " + ZuliaVersion.getVersionAdded();
                }
                System.out.println(val.getServerAddress() + "\t" + val.getServicePort() + "\t" + val.getHeartbeat() + "\t" + val.getRestPort() + "\t" + nodeVersion);
            }
        } else if ("getFields".equals(jCommander.getParsedCommand())) {
            GetFieldsResult response = workPool.execute(new io.zulia.client.command.GetFields(index));
            response.getFieldNames().forEach(System.out::println);
        } else if ("delete".equals(jCommander.getParsedCommand())) {
            System.out.println("Deleting index: " + index);
            DeleteIndexResult response = workPool.execute(new DeleteIndex(index));
            System.out.println("Deleted index: " + index);
        } else if ("reindex".equals(jCommander.getParsedCommand())) {
            if (index.contains("*")) {
                GetIndexesResult indexesResult = workPool.getIndexes();
                for (String ind : indexesResult.getIndexNames()) {
                    if (ind.startsWith(index.replace("*", ""))) {
                        System.out.println("Reindexing index: " + ind);
                        ReindexResult response = workPool.execute(new Reindex(ind));
                        System.out.println("Reindexed index: " + ind);
                    }
                }
            } else {
                System.out.println("Reindexing index: " + index);
                ReindexResult response = workPool.execute(new Reindex(index));
                System.out.println("Reindexed index: " + index);
            }
        } else if ("optimize".equals(jCommander.getParsedCommand())) {
            System.out.println("Optimizing index: " + index);
            OptimizeIndexResult response = workPool.execute(new OptimizeIndex(index));
            System.out.println("Optimized index: " + index);
        }
    } catch (Exception e) {
        if (e instanceof ParameterException) {
            System.err.println(e.getMessage());
            jCommander.usage();
            System.exit(2);
        } else {
            e.printStackTrace();
        }
    }
}
Also used : ZuliaWorkPool(io.zulia.client.pool.ZuliaWorkPool) ZuliaBase(io.zulia.message.ZuliaBase) FilterQuery(io.zulia.client.command.builder.FilterQuery) GetFieldsResult(io.zulia.client.result.GetFieldsResult) Document(org.bson.Document) ScoredQuery(io.zulia.client.command.builder.ScoredQuery) DeleteIndex(io.zulia.client.command.DeleteIndex) Search(io.zulia.client.command.builder.Search) GetIndexes(io.zulia.client.command.GetIndexes) Sort(io.zulia.client.command.builder.Sort) ParameterException(com.beust.jcommander.ParameterException) GetNodes(io.zulia.client.command.GetNodes) GetIndexesResult(io.zulia.client.result.GetIndexesResult) CountFacet(io.zulia.client.command.builder.CountFacet) ReindexResult(io.zulia.client.result.ReindexResult) OptimizeIndexResult(io.zulia.client.result.OptimizeIndexResult) ZuliaQuery(io.zulia.message.ZuliaQuery) Reindex(io.zulia.client.command.Reindex) ClearIndex(io.zulia.client.command.ClearIndex) JCommander(com.beust.jcommander.JCommander) ClearIndexResult(io.zulia.client.result.ClearIndexResult) OptimizeIndex(io.zulia.client.command.OptimizeIndex) SearchResult(io.zulia.client.result.SearchResult) DeleteIndexResult(io.zulia.client.result.DeleteIndexResult) Date(java.util.Date) ParameterException(com.beust.jcommander.ParameterException) GetNumberOfDocs(io.zulia.client.command.GetNumberOfDocs) GetNodesResult(io.zulia.client.result.GetNodesResult) ZuliaPoolConfig(io.zulia.client.config.ZuliaPoolConfig) GetNumberOfDocsResult(io.zulia.client.result.GetNumberOfDocsResult)

Example 4 with ScoredQuery

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

the class ZuliaCmdUtil method writeOutput.

public static void writeOutput(String recordsFilename, String index, String q, int rows, ZuliaWorkPool workPool, AtomicInteger count, String idField, Set<String> uniqueIds, boolean sortById) throws Exception {
    try (FileWriter fileWriter = new FileWriter(recordsFilename, Charsets.UTF_8)) {
        Search zuliaQuery = new Search(index).addQuery(new ScoredQuery(q)).setAmount(rows);
        if (sortById) {
            zuliaQuery.addSort(new Sort(idField));
        }
        try {
            workPool.searchAll(zuliaQuery, queryResult -> {
                long totalHits = queryResult.getTotalHits();
                LOG.info("Found <" + totalHits + "> for index <" + index + ">");
                queryResult.getDocuments().forEach(doc -> {
                    try {
                        if (uniqueIds != null) {
                            uniqueIds.add(doc.getString(idField));
                        }
                        fileWriter.write(doc.toJson());
                        fileWriter.write(System.lineSeparator());
                        if (count.incrementAndGet() % 1000 == 0) {
                            LOG.info("So far written <" + count + "> of <" + totalHits + ">");
                        }
                    } catch (IOException e) {
                        LOG.log(Level.SEVERE, "Could not write record <" + doc + "> for index <" + index + ">", e);
                    } catch (Throwable e) {
                        LOG.log(Level.SEVERE, "Could not write output for index <" + index + ">", e);
                    }
                });
            });
        } catch (Throwable t) {
            LOG.log(Level.SEVERE, "Query failed for index <" + index + ">", t);
        }
    } catch (Throwable e) {
        LOG.log(Level.SEVERE, "Could not write output for index <" + index + ">", e);
        throw e;
    }
}
Also used : ScoredQuery(io.zulia.client.command.builder.ScoredQuery) FileWriter(java.io.FileWriter) Search(io.zulia.client.command.builder.Search) Sort(io.zulia.client.command.builder.Sort) IOException(java.io.IOException)

Example 5 with ScoredQuery

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

Aggregations

ScoredQuery (io.zulia.client.command.builder.ScoredQuery)5 Search (io.zulia.client.command.builder.Search)5 Sort (io.zulia.client.command.builder.Sort)4 SearchResult (io.zulia.client.result.SearchResult)4 Order (org.junit.jupiter.api.Order)3 Test (org.junit.jupiter.api.Test)3 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)3 GetFieldsResult (io.zulia.client.result.GetFieldsResult)2 GetIndexesResult (io.zulia.client.result.GetIndexesResult)2 GetNodesResult (io.zulia.client.result.GetNodesResult)2 GetNumberOfDocsResult (io.zulia.client.result.GetNumberOfDocsResult)2 ZuliaQuery (io.zulia.message.ZuliaQuery)2 JCommander (com.beust.jcommander.JCommander)1 ParameterException (com.beust.jcommander.ParameterException)1 ClearIndex (io.zulia.client.command.ClearIndex)1 DeleteIndex (io.zulia.client.command.DeleteIndex)1 Fetch (io.zulia.client.command.Fetch)1 GetIndexes (io.zulia.client.command.GetIndexes)1 GetNodes (io.zulia.client.command.GetNodes)1 GetNumberOfDocs (io.zulia.client.command.GetNumberOfDocs)1