public static Query addTenantClause(List<String> tenantLinks) { Query groupClause = null; String propertyName = QueryTask.QuerySpecification .buildCollectionItemName(MultiTenantDocument.FIELD_NAME_TENANT_LINKS); // if a tenant is not specified, search global only if (tenantLinks == null || tenantLinks.isEmpty()) { groupClause = new Query() .setTermPropertyName(propertyName); groupClause.setTermMatchType(MatchType.WILDCARD) .setTermMatchValue(UriUtils.URI_WILDCARD_CHAR); groupClause.occurance = Occurance.MUST_NOT_OCCUR; } else { groupClause = addListValueClause(propertyName, tenantLinks.stream() .filter(tenantLink -> tenantLink.startsWith(MultiTenantDocument.TENANTS_PREFIX) || tenantLink.startsWith(MultiTenantDocument.PROJECTS_IDENTIFIER)) .filter(tenantLink -> !tenantLink.contains( MultiTenantDocument.GROUP_IDENTIFIER)) .collect(Collectors.toList()), MatchType.TERM); } return groupClause; }
public static Query addTenantAndGroupClause(List<String> tenantLinks) { Query groupClause = null; String propertyName = QueryTask.QuerySpecification .buildCollectionItemName(MultiTenantDocument.FIELD_NAME_TENANT_LINKS); // if a tenant is not specified, search global only if (tenantLinks == null || tenantLinks.isEmpty()) { groupClause = new Query() .setTermPropertyName(propertyName); groupClause.setTermMatchType(MatchType.WILDCARD) .setTermMatchValue(UriUtils.URI_WILDCARD_CHAR); groupClause.occurance = Occurance.MUST_NOT_OCCUR; } else { groupClause = addListValueClause(propertyName, tenantLinks.stream() .filter(tenantLink -> !tenantLink.startsWith(MultiTenantDocument.USERS_PREFIX)) .collect(Collectors.toList()), MatchType.TERM); } return groupClause; }
public static Query addTenantAndUserClause(List<String> tenantLinks) { Query groupClause = null; String propertyName = QueryTask.QuerySpecification .buildCollectionItemName(MultiTenantDocument.FIELD_NAME_TENANT_LINKS); // if a tenant is not specified, search global only if (tenantLinks == null || tenantLinks.isEmpty()) { groupClause = new Query() .setTermPropertyName(propertyName); groupClause.setTermMatchType(MatchType.WILDCARD) .setTermMatchValue(UriUtils.URI_WILDCARD_CHAR); groupClause.occurance = Occurance.MUST_NOT_OCCUR; } else { groupClause = addListValueClause(propertyName, tenantLinks.stream() .filter(tenantLink -> !tenantLink.contains( MultiTenantDocument.GROUP_IDENTIFIER)) .collect(Collectors.toList()), MatchType.TERM); } return groupClause; }
private QueryTask.Query createCompositeDescClause(String query, List<String> tenantLinks, boolean templatesParentOnly) { QueryTask.Query compositeDescClause = new QueryTask.Query(); compositeDescClause.addBooleanClause(createKindClause(CompositeDescription.class)); if (templatesParentOnly) { QueryTask.Query propClause = new QueryTask.Query() .setTermPropertyName(CompositeDescription.FIELD_NAME_PARENT_DESCRIPTION_LINK) .setTermMatchType(MatchType.WILDCARD) .setTermMatchValue("*"); propClause.occurance = Occurance.MUST_NOT_OCCUR; compositeDescClause.addBooleanClause(propClause); } compositeDescClause.addBooleanClause(createAnyPropertyClause(query, CompositeDescription.FIELD_NAME_NAME)); // if tenant is null, do a global search, if not search in tenant if (tenantLinks != null && !tenantLinks.isEmpty()) { compositeDescClause .addBooleanClause(QueryUtil.addTenantGroupAndUserClause(tenantLinks)); } return compositeDescClause; }
private QueryTask buildEventLogCountQuery(List<String> tenantLinks) { QueryTask qt = QueryUtil.buildQuery(EventLogState.class, true); if (!tenantLinks.isEmpty()) { qt.querySpec.query.addBooleanClause(QueryUtil.addTenantGroupAndUserClause(tenantLinks)); } long nMinutesAgo = Utils.fromNowMicrosUtc(-EVENTS_TIME_INTERVAL_MICROS); QueryTask.Query numOfInstancesClause = new QueryTask.Query() .setTermPropertyName(ServiceDocument.FIELD_NAME_UPDATE_TIME_MICROS) .setNumericRange(NumericRange.createLongRange(nMinutesAgo, Long.MAX_VALUE, true, false)) .setTermMatchType(MatchType.TERM); qt.querySpec.query.addBooleanClause(numOfInstancesClause); QueryTask.Query eventTypeClause = new QueryTask.Query() .setTermPropertyName(EventLogState.FIELD_NAME_EVENT_LOG_TYPE) .setTermMatchValue(EventLogState.EventLogType.INFO.toString()) .setTermMatchType(MatchType.TERM); eventTypeClause.occurance = Occurance.MUST_NOT_OCCUR; qt.querySpec.query.addBooleanClause(eventTypeClause); QueryUtil.addCountOption(qt); return qt; }
public void logAllDocuments(Set<URI> baseHostUris) { QueryTask task = new QueryTask(); task.setDirect(true); task.querySpec = new QuerySpecification(); task.querySpec.query.setTermPropertyName("documentSelfLink").setTermMatchValue("*"); task.querySpec.query.setTermMatchType(MatchType.WILDCARD); task.querySpec.options = EnumSet.of(QueryOption.EXPAND_CONTENT); List<Operation> ops = new ArrayList<>(); for (URI baseHost : baseHostUris) { Operation queryPost = Operation .createPost(UriUtils.buildUri(baseHost, ServiceUriPaths.CORE_QUERY_TASKS)) .setBody(task); ops.add(queryPost); } List<QueryTask> queryTasks = this.sender.sendAndWait(ops, QueryTask.class); for (QueryTask queryTask : queryTasks) { log(Utils.toJsonHtml(queryTask)); } }
public void logAllDocuments(Set<URI> baseHostUris) { QueryTask task = new QueryTask(); task.setDirect(true); task.querySpec = new QuerySpecification(); task.querySpec.query.setTermPropertyName("documentSelfLink").setTermMatchValue("*"); task.querySpec.query.setTermMatchType(MatchType.WILDCARD); task.querySpec.options = EnumSet.of(QueryOption.EXPAND_CONTENT); List<Operation> ops = new ArrayList<>(); for (URI baseHost : baseHostUris) { Operation queryPost = Operation .createPost(UriUtils.buildUri(baseHost, ServiceUriPaths.CORE_QUERY_TASKS)) .setBody(task); ops.add(queryPost); } List<QueryTask> queryTasks = this.sender.sendAndWait(ops, QueryTask.class); for (QueryTask queryTask : queryTasks) { log(Utils.toJsonHtml(queryTask)); } }
/** * Add a clause which matches a top level field name with the provided {@link MatchType} and * {@link Occurance}. * @param fieldName the top level field name. * @param fieldValue the field value to match, first converted so it can match in a case insensitive way * @param matchType the match type. * @param occurance the {@link Occurance} for this clause. * @return a reference to this object. */ public Builder addCaseInsensitiveFieldClause(String fieldName, String fieldValue, MatchType matchType, Occurance occurance) { Query clause = new Query() .setTermPropertyName(fieldName) .setCaseInsensitiveTermMatchValue(fieldValue) .setTermMatchType(matchType); clause.occurance = occurance; this.query.addBooleanClause(clause); return this; }
@Override public ServiceDocument getDocumentTemplate() { ServiceDocument td = super.getDocumentTemplate(); QueryTask template = (QueryTask) td; QuerySpecification q = new QueryTask.QuerySpecification(); QueryTask.Query kindClause = new QueryTask.Query().setTermPropertyName( ServiceDocument.FIELD_NAME_KIND).setTermMatchValue( Utils.buildKind(ExampleServiceState.class)); QueryTask.Query nameClause = new QueryTask.Query(); nameClause.setTermPropertyName("name") .setTermMatchValue("query-target") .setTermMatchType(MatchType.PHRASE); q.query.addBooleanClause(kindClause).addBooleanClause(nameClause); template.querySpec = q; QueryTask exampleTask = new QueryTask(); template.indexLink = exampleTask.indexLink; return template; } }
/** * Add a clause which matches a top level field name with the provided {@link MatchType} and * {@link Occurance}. * @param fieldName the top level field name. * @param fieldValue the field value to match. * @param matchType the match type. * @param occurance the {@link Occurance} for this clause. * @return a reference to this object. */ public Builder addFieldClause(String fieldName, String fieldValue, MatchType matchType, Occurance occurance) { Query clause = new Query() .setTermPropertyName(fieldName) .setTermMatchValue(fieldValue) .setTermMatchType(matchType); clause.occurance = occurance; this.query.addBooleanClause(clause); return this; }
public static Query addTenantGroupAndUserClause(Collection<String> tenantLinks) { Query groupClause = null; String propertyName = QueryTask.QuerySpecification .buildCollectionItemName(MultiTenantDocument.FIELD_NAME_TENANT_LINKS); // if a tenant is not specified, search global only if (tenantLinks == null || tenantLinks.isEmpty()) { groupClause = new Query() .setTermPropertyName(propertyName); groupClause.setTermMatchType(MatchType.WILDCARD) .setTermMatchValue(UriUtils.URI_WILDCARD_CHAR); groupClause.occurance = Occurance.MUST_NOT_OCCUR; } else { groupClause = addListValueClause(propertyName, tenantLinks, MatchType.TERM); } return groupClause; }
public static QueryTask.Query createAnyPropertyClause(String query, Occurance occurence, String... propertyNames) { QueryTask.Query anyPropertyClause = new QueryTask.Query(); for (String propertyName : propertyNames) { QueryTask.Query propClause = new QueryTask.Query() .setTermPropertyName(propertyName) .setTermMatchType(MatchType.WILDCARD) .setTermMatchValue(query); propClause.occurance = occurence; anyPropertyClause.addBooleanClause(propClause); } return anyPropertyClause; }
@Test public void testSimpleWildcardQuery() throws Throwable { Query expected = new Query().setTermPropertyName("name").setTermMatchValue("foo*"); expected.occurance = Query.Occurance.MUST_OCCUR; expected.setTermMatchType(QueryTask.QueryTerm.MatchType.WILDCARD); String odataFilter = String.format("%s eq %s", expected.term.propertyName, expected.term.matchValue); Query actual = toQuery(odataFilter); assertQueriesEqual(actual, expected); }
private Query createUpdatedSinceTimeRange(long timeInMicros) { long limitToNowInMicros = Utils.getNowMicrosUtc() + TimeUnit.SECONDS.toMicros(10); NumericRange<Long> range = NumericRange.createLongRange(timeInMicros, limitToNowInMicros, true, false); range.precisionStep = 64; // 4 and 8 doesn't work. 16 works but set 64 to be certain. QueryTask.Query latestSinceCondition = new QueryTask.Query() .setTermPropertyName(ServiceDocument.FIELD_NAME_UPDATE_TIME_MICROS) .setNumericRange(range) .setTermMatchType(MatchType.TERM); latestSinceCondition.occurance = Occurance.MUST_OCCUR; return latestSinceCondition; }
Query createTerm(String key, String value, Occurance occurance, MatchType matchType) { Query query = createTerm(key, value); query.setTermMatchType(matchType); query.occurance = occurance; return query; }
@Test public void testSimpleWildcardQuery() throws Throwable { Query expected = new Query().setTermPropertyName("name").setTermMatchValue("foo*"); expected.occurance = Query.Occurance.MUST_OCCUR; expected.setTermMatchType(QueryTask.QueryTerm.MatchType.WILDCARD); String odataFilter = String.format("%s eq %s", expected.term.propertyName, expected.term.matchValue); Query actual = toQuery(odataFilter); assertQueriesEqual(actual, expected); }
Query createTerm(String key, String value) { Query query = new Query(); query.setTermPropertyName(key); query.setTermMatchType(MatchType.TERM); query.setTermMatchValue(value); return query; }
Query createTerm(String key, String value, Occurance occurance, MatchType matchType) { Query query = createTerm(key, value); query.setTermMatchType(matchType); query.occurance = occurance; return query; }
private Query createUpdatedSinceTimeRange(long timeInMicros) { long limitToNowInMicros = Utils.fromNowMicrosUtc(TimeUnit.SECONDS.toMicros(10)); NumericRange<Long> range = NumericRange.createLongRange(timeInMicros, limitToNowInMicros, true, false); range.precisionStep = 64; // 4 and 8 doesn't work. 16 works but set 64 to be certain. QueryTask.Query latestSinceCondition = new QueryTask.Query() .setTermPropertyName(ServiceDocument.FIELD_NAME_UPDATE_TIME_MICROS) .setNumericRange(range) .setTermMatchType(MatchType.TERM); latestSinceCondition.occurance = Occurance.MUST_OCCUR; return latestSinceCondition; }
Query createTerm(String key, String value) { Query query = new Query(); query.setTermPropertyName(key); query.setTermMatchType(MatchType.TERM); query.setTermMatchValue(value); return query; }