use of io.zulia.client.command.builder.FilterQuery in project zuliasearch by zuliaio.
the class StartStopTest method termTestBuilder.
@Test
@Order(3)
public void termTestBuilder() throws Exception {
Search s = new Search(FACET_TEST_INDEX);
s.addQuery(new TermQuery("id").addTerms("1", "2", "3", "4"));
SearchResult searchResult = zuliaWorkPool.search(s);
Assertions.assertEquals(4, searchResult.getTotalHits());
s = new Search(FACET_TEST_INDEX);
s.addQuery(new TermQuery("id").addTerms(Arrays.asList("1", "2", "3", "4")));
s.addQuery(new FilterQuery("testBool:true"));
searchResult = zuliaWorkPool.search(s);
Assertions.assertEquals(4, searchResult.getTotalHits());
s = new Search(FACET_TEST_INDEX);
s.addQuery(new TermQuery("id").addTerm("1").addTerm("2").addTerm("3").addTerm("4"));
s.addQuery(new FilterQuery("country:US"));
searchResult = zuliaWorkPool.search(s);
Assertions.assertEquals(2, searchResult.getTotalHits());
s = new Search(FACET_TEST_INDEX);
s.addQuery(new TermQuery("id").addTerm("1").addTerm("2").addTerm("3").addTerm("4").exclude());
s.addQuery(new FilterQuery("country:US"));
searchResult = zuliaWorkPool.search(s);
Assertions.assertEquals(28, searchResult.getTotalHits());
s = new Search(FACET_TEST_INDEX);
s.addQuery(new TermQuery("id").addTerm("1").addTerm("2").addTerm("3").addTerm("4").exclude());
s.addQuery(new FilterQuery("country:US").exclude());
// need to use match all because all other queries are negated
s.addQuery(new MatchAllQuery());
searchResult = zuliaWorkPool.search(s);
Assertions.assertEquals(28, searchResult.getTotalHits());
}
use of io.zulia.client.command.builder.FilterQuery 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.client.command.builder.FilterQuery in project zuliasearch by zuliaio.
the class StartStopTest method lengthTestBuilder.
@Test
@Order(3)
public void lengthTestBuilder() throws Exception {
Search s = new Search(FACET_TEST_INDEX);
SearchResult searchResult = zuliaWorkPool.search(s);
long total = searchResult.getTotalHits();
s = new Search(FACET_TEST_INDEX).addQuery(new FilterQuery("|country|:2"));
searchResult = zuliaWorkPool.search(s);
Assertions.assertEquals(total / 2, searchResult.getTotalHits());
s = new Search(FACET_TEST_INDEX).addQuery(new FilterQuery("|country|:[0 TO 1]"));
searchResult = zuliaWorkPool.search(s);
Assertions.assertEquals(0, searchResult.getTotalHits());
s = new Search(FACET_TEST_INDEX).addQuery(new FilterQuery("|||testList|||:3"));
searchResult = zuliaWorkPool.search(s);
Assertions.assertEquals(total / 2, searchResult.getTotalHits());
s = new Search(FACET_TEST_INDEX).addQuery(new FilterQuery("|||testList|||:[2 TO 3]"));
searchResult = zuliaWorkPool.search(s);
Assertions.assertEquals(total, searchResult.getTotalHits());
s = new Search(FACET_TEST_INDEX).setAmount((int) total / 2).addSort(new Sort("|country|"));
searchResult = zuliaWorkPool.search(s);
for (Document document : searchResult.getDocuments()) {
Assertions.assertEquals(document.getString("country"), "US");
}
s = new Search(FACET_TEST_INDEX).setAmount((int) total / 2).addSort(new Sort("|country|").descending());
searchResult = zuliaWorkPool.search(s);
for (Document document : searchResult.getDocuments()) {
Assertions.assertEquals(document.getString("country"), "France");
}
}
use of io.zulia.client.command.builder.FilterQuery 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");
}
use of io.zulia.client.command.builder.FilterQuery 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();
}
}
}
Aggregations