/** * Creates a criterion using a {@literal $regex} operator. * * @param re * @return * @see <a href="https://docs.mongodb.com/manual/reference/operator/query/regex/">MongoDB Query operator: $regex</a> */ public Criteria regex(String re) { return regex(re, null); }
/** * Creates a criterion using a {@literal $regex} and {@literal $options} operator. * * @param re * @param options * @return * @see <a href="https://docs.mongodb.com/manual/reference/operator/query/regex/">MongoDB Query operator: $regex</a> */ public Criteria regex(String re, @Nullable String options) { return regex(toPattern(re, options)); }
/** * Creates an appropriate like-regex and appends it to the given criteria. * * @param criteria * @param part * @param value * @return the criteria extended with the regex. */ private Criteria addAppropriateLikeRegexTo(Criteria criteria, Part part, Object value) { if (value == null) { throw new IllegalArgumentException(String.format( "Argument for creating $regex pattern for property '%s' must not be null!", part.getProperty().getSegment())); } return criteria.regex(toLikeRegex(value.toString(), part), toRegexOptions(part)); }
/** * Creates a criterion using a {@literal $regex} operator. * * @param re * @return * @see <a href="https://docs.mongodb.com/manual/reference/operator/query/regex/">MongoDB Query operator: $regex</a> */ public Criteria regex(String re) { return regex(re, null); }
/** * Creates a criterion using a {@literal $regex} and {@literal $options} operator. * * @param re * @param options * @return * @see <a href="https://docs.mongodb.com/manual/reference/operator/query/regex/">MongoDB Query operator: $regex</a> */ public Criteria regex(String re, @Nullable String options) { return regex(toPattern(re, options)); }
/** * Creates an appropriate like-regex and appends it to the given criteria. * * @param criteria * @param part * @param value * @return the criteria extended with the regex. */ private Criteria addAppropriateLikeRegexTo(Criteria criteria, Part part, Object value) { if (value == null) { throw new IllegalArgumentException(String.format( "Argument for creating $regex pattern for property '%s' must not be null!", part.getProperty().getSegment())); } return criteria.regex(toLikeRegex(value.toString(), part), toRegexOptions(part)); }
private Query buildSearchUserQuery(String term, Pageable pageable) { final String regex = "(?i).*" + Pattern.quote(term.toLowerCase()) + ".*"; Criteria login = where(LOGIN).regex(regex); Criteria fullName = where(FULLNAME_DB_FIELD).regex(regex); Criteria email = where(User.EMAIL).regex(regex); Criteria criteria = new Criteria().orOperator(email, login, fullName); return query(criteria).with(pageable); }
@Override public Iterable<RuleEntity> search(String search) { Collection<String> terms = new HashSet<>(Arrays.asList(search.split(" "))); Criteria[] criterias = terms .stream() .map(s -> Criteria.where("searchIndex").regex(s.toLowerCase(), "iu")) .toArray(Criteria[]::new); return mongoTemplate.find(Query.query(new Criteria().andOperator(criterias)).with(Sort.by("order")), RuleEntity.class); } }
/** * Finds a project by a given name * * @param name The name of the project that should be retrieved * @return Returns a project with the provided name * @see GraphQLProject */ @Override public GraphQLProject findGraphQLProjectWithName(String name) { Preconditions.checkNotNull(name, "Project name cannot be null"); Preconditions.checkArgument(!name.isEmpty(), "Project name cannot be empty"); Query exactNameIgnoreCaseQuery = query(where("name").regex("^" + name + "$", "i")); GraphQLProjectDocument project = mongoOperations.findOne(exactNameIgnoreCaseQuery, GraphQLProjectDocument.class); return project == null ? null : mapper.map(project, GraphQLProject.class); }
@Override public void addCondition(Criteria criteria, FilterCondition filter, CriteriaHolder criteriaHolder) { /* Validate only strings */ this.validate(criteriaHolder, filter.getValue(), filter.isNegative(), INCORRECT_FILTER_PARAMETERS); if (!ObjectId.isValid(filter.getValue())) { criteria.regex("(?i).*" + Pattern.quote(filter.getValue()) + ".*"); } else { criteria.regex(".*" + filter.getValue() + ".*"); } }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override public List<String> findDistinctValues(String launchId, String containsValue, String distinctBy) { Aggregation aggregation = newAggregation(match(where(LAUNCH_REFERENCE).is(launchId)), unwind(distinctBy), match(where(distinctBy).regex("(?i).*" + Pattern.quote(containsValue) + ".*")), group(distinctBy) ); AggregationResults<Map> result = mongoTemplate.aggregate(aggregation, TestItem.class, Map.class); return result.getMappedResults().stream().map(entry -> entry.get("_id").toString()).collect(toList()); }
/** * Case-insensitive contains search. * * @see com.castlemock.core.basis.model.SearchValidator#validate(String, String) */ protected Query getSearchQuery(String property, SearchQuery searchQuery) { return query(where(property).regex(".*" + searchQuery.getQuery() + ".*", "i")); }
@SuppressWarnings({ "rawtypes" }) @Override public List<String> findValuesWithMode(String projectName, String containsValue, String distinctBy, String mode) { Aggregation aggregation = newAggregation(match(where(PROJECT_ID_REFERENCE).is(projectName)), match(where(MODE).is(mode)), match(where(distinctBy).regex("(?i).*" + Pattern.quote(containsValue) + ".*")), group(distinctBy), limit(AUTOCOMPLETE_LIMITATION) ); AggregationResults<Map> result = mongoTemplate.aggregate(aggregation, Launch.class, Map.class); return result.getMappedResults().stream().map(entry -> entry.get("_id").toString()).collect(toList()); }
@Override public Page<T> findSharedEntitiesByName(String projectName, String term, Pageable pageable) { final String regex = "(?i).*" + Pattern.quote(term.toLowerCase()) + ".*"; Query query = createSharedEntityQuery(projectName).with(pageable); Criteria name = Criteria.where(ENTITY_NAME_FIELD).regex(regex); Criteria owner = Criteria.where(ENTITY_OWNER_FIELD).regex(regex); Criteria description = Criteria.where(ENTITY_DESCRIPTION_FIELD).regex(regex); query.addCriteria(new Criteria().orOperator(name, owner, description)); Class<T> entityType = getEntityInformation().getJavaType(); List<T> searchResults = getMongoOperations().find(query, entityType); return new PageImpl<>(searchResults, pageable, getMongoOperations().count(query, entityType)); }
/** * name模糊查找 */ @Override public List<User> findUserByLikeName(String name) { Query query = new Query(); query.addCriteria(Criteria.where("name").regex(".*" +name+ ".*")); return mongoTemplate.find(query, User.class); }
@Override public List<GridFSDBFile> findModifiedLaterAgo(Duration period, String project) { return gridFs.find(ModifiableQueryBuilder.findModifiedLaterThanPeriod(period, project) .addCriteria(where("filename").not().regex(PHOTO_PREFIX))); }
@Override public Optional<String> findPersonalProjectName(String user) { Query query = Query.query(userExists(user)) .addCriteria(Criteria.where(PROJECT_TYPE).is(EntryType.PERSONAL)) .addCriteria(Criteria.where(PROJECT_ID).regex("^" + user + PERSONAL_PROJECT_POSTFIX)); query.fields().include("name"); return Optional.ofNullable(mongoTemplate.findOne(query, Project.class)).map(Project::getName); }
@Override public List<TestItem> findInIssueTypeItems(String issueType, String launchId) { Query query = query(where(LAUNCH_REFERENCE).is(launchId)).addCriteria(where(ISSUE).exists(true)) .addCriteria(where(ISSUE_TYPE).regex(Pattern.quote(issueType))); return mongoTemplate.find(query, TestItem.class); }