Example usage for com.mongodb QueryBuilder text

List of usage examples for com.mongodb QueryBuilder text

Introduction

In this page you can find the example usage for com.mongodb QueryBuilder text.

Prototype

public QueryBuilder text(final String search) 

Source Link

Document

Equivalent to a $text operand.

Usage

From source file:net.atos.entng.calendar.event.CalendarSearchingEvents.java

License:Open Source License

private void searchEvent(int page, int limit, List<String> searchWords, final Map<String, String> mapIdTitle,
        Handler<Either<String, JsonArray>> handler) {
    final int skip = (0 == page) ? -1 : page * limit;

    final List<String> returnFields = new ArrayList<String>();
    returnFields.add("title");
    returnFields.add("calendar");
    returnFields.add("description");
    returnFields.add("location");
    returnFields.add("modified");
    returnFields.add("startMoment");
    returnFields.add("endMoment");
    returnFields.add("owner.userId");
    returnFields.add("owner.displayName");

    final QueryBuilder worldsQuery = new QueryBuilder();
    worldsQuery.text(MongoDbSearchService.textSearchedComposition(searchWords));

    final QueryBuilder calendarQuery = new QueryBuilder().start("calendar").in(mapIdTitle.keySet());
    final QueryBuilder query = new QueryBuilder().and(worldsQuery.get(), calendarQuery.get());

    JsonObject sort = new JsonObject().putNumber("modified", -1);
    final JsonObject projection = new JsonObject();
    for (String field : returnFields) {
        projection.putNumber(field, 1);/*ww  w. j av  a2  s.  c  om*/
    }

    mongo.find(Calendar.CALENDAR_EVENT_COLLECTION, MongoQueryBuilder.build(query), sort, projection, skip,
            limit, Integer.MAX_VALUE, validResultsHandler(handler));
}

From source file:net.atos.entng.forum.events.ForumSearchingEvents.java

License:Open Source License

private void searchSubject(int page, int limit, List<String> searchWords, final Map<String, String> mapIdName,
        Handler<Either<String, JsonArray>> handler) {
    final int skip = (0 == page) ? -1 : page * limit;

    final QueryBuilder worldsQuery = new QueryBuilder();
    worldsQuery.text(MongoDbSearchService.textSearchedComposition(searchWords));

    final QueryBuilder categoryQuery = new QueryBuilder().start("category").in(mapIdName.keySet());

    final QueryBuilder query = new QueryBuilder().and(worldsQuery.get(), categoryQuery.get());

    JsonObject sort = new JsonObject().putNumber("modified", -1);
    final JsonObject projection = new JsonObject();
    projection.putNumber("title", 1);
    projection.putNumber("messages", 1);
    projection.putNumber("category", 1);
    projection.putNumber("modified", 1);
    projection.putNumber("owner.userId", 1);
    projection.putNumber("owner.displayName", 1);

    mongo.find(Forum.SUBJECT_COLLECTION, MongoQueryBuilder.build(query), sort, projection, skip, limit,
            Integer.MAX_VALUE, validResultsHandler(handler));
}

From source file:org.entcore.blog.events.BlogSearchingEvents.java

License:Open Source License

private void searchPosts(int page, int limit, List<String> searchWords, final Set<String> setIds,
        Handler<Either<String, JsonArray>> handler) {
    final int skip = (0 == page) ? -1 : page * limit;

    final QueryBuilder worldsQuery = new QueryBuilder();
    worldsQuery.text(MongoDbSearchService.textSearchedComposition(searchWords));

    final QueryBuilder blogQuery = new QueryBuilder().start("blog.$id").in(setIds);
    final QueryBuilder publishedQuery = new QueryBuilder().start("state").is(PUBLISHED_STATE);

    final QueryBuilder query = new QueryBuilder().and(worldsQuery.get(), blogQuery.get(), publishedQuery.get());

    JsonObject sort = new JsonObject().put("modified", -1);
    final JsonObject projection = new JsonObject();
    projection.put("title", 1);
    projection.put("content", 1);
    projection.put("blog.$id", 1);
    projection.put("modified", 1);
    projection.put("author.userId", 1);
    projection.put("author.username", 1);

    mongo.find(Blog.POSTS_COLLECTION, MongoQueryBuilder.build(query), sort, projection, skip, limit,
            Integer.MAX_VALUE, validResultsHandler(handler));
}

From source file:org.entcore.common.service.impl.MongoDbSearchService.java

License:Open Source License

@Override
public void search(String userId, List<String> groupIds, List<String> returnFields, List<String> searchWords,
        Integer page, Integer limit, Handler<Either<String, JsonArray>> handler) {
    final int skip = (0 == page) ? -1 : page * limit;

    final List<DBObject> groups = new ArrayList<DBObject>();
    groups.add(QueryBuilder.start("userId").is(userId).get());
    for (String gpId : groupIds) {
        groups.add(QueryBuilder.start("groupId").is(gpId).get());
    }//from   ww  w  .  ja  v  a  2 s  .  c o m

    final QueryBuilder worldsQuery = new QueryBuilder();
    //no stemming (in fact, stemming works only with words and for a given language) and no list of stop words
    worldsQuery.text(textSearchedComposition(searchWords));

    final QueryBuilder rightsOrQuery = new QueryBuilder().or(
            QueryBuilder.start("visibility").is(VisibilityFilter.PUBLIC.name()).get(),
            QueryBuilder.start("visibility").is(VisibilityFilter.PROTECTED.name()).get(),
            QueryBuilder.start(this.ownerUserId).is(userId).get(), QueryBuilder.start("shared")
                    .elemMatch(new QueryBuilder().or(groups.toArray(new DBObject[groups.size()])).get()).get());

    final QueryBuilder query = new QueryBuilder().and(worldsQuery.get(), rightsOrQuery.get());

    JsonObject sort = new JsonObject().put("modified", -1);
    final JsonObject projection = new JsonObject();
    for (String field : returnFields) {
        projection.put(field, 1);
    }

    mongo.find(collection, MongoQueryBuilder.build(query), sort, projection, skip, limit, Integer.MAX_VALUE,
            validResultsHandler(handler));
}

From source file:org.entcore.workspace.service.impl.WorkspaceSearchingEvents.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public void searchResource(List<String> appFilters, final String userId, JsonArray groupIds,
        JsonArray searchWords, Integer page, Integer limit, final JsonArray columnsHeader, final String locale,
        final Handler<Either<String, JsonArray>> handler) {
    if (appFilters.contains(WorkspaceSearchingEvents.class.getSimpleName())) {

        final List<String> searchWordsLst = searchWords.getList();

        final List<String> groupIdsLst = groupIds.getList();
        final List<DBObject> groups = new ArrayList<>();
        groups.add(QueryBuilder.start("userId").is(userId).get());
        for (String gpId : groupIdsLst) {
            groups.add(QueryBuilder.start("groupId").is(gpId).get());
        }//from ww w  . ja v a 2s .  co m

        final QueryBuilder rightsQuery = new QueryBuilder().or(
                QueryBuilder.start("visibility").is(VisibilityFilter.PUBLIC.name()).get(),
                QueryBuilder.start("visibility").is(VisibilityFilter.PROTECTED.name()).get(),
                QueryBuilder.start("visibility").is(VisibilityFilter.PROTECTED.name()).get(),
                QueryBuilder.start("owner").is(userId).get(),
                QueryBuilder.start("shared")
                        .elemMatch(new QueryBuilder().or(groups.toArray(new DBObject[groups.size()])).get())
                        .get());

        final QueryBuilder worldsQuery = new QueryBuilder();
        worldsQuery.text(MongoDbSearchService.textSearchedComposition(searchWordsLst));

        //searching only the file entry (not folder), if folder match and is returned, the paginate system is impacted
        final QueryBuilder fileQuery = QueryBuilder.start("file").exists(true);

        final QueryBuilder query = new QueryBuilder().and(fileQuery.get(), rightsQuery.get(),
                worldsQuery.get());

        JsonObject sort = new JsonObject().put("modified", -1);
        final JsonObject projection = new JsonObject();
        projection.put("name", 1);
        projection.put("modified", 1);
        projection.put("folder", 1);
        projection.put("owner", 1);
        projection.put("ownerName", 1);
        projection.put("comments", 1);

        final int skip = (0 == page) ? -1 : page * limit;

        //main search on file
        mongo.find(this.collection, MongoQueryBuilder.build(query), sort, projection, skip, limit,
                Integer.MAX_VALUE, validResultsHandler(new Handler<Either<String, JsonArray>>() {
                    @Override
                    public void handle(Either<String, JsonArray> event) {

                        if (event.isRight()) {
                            final JsonArray globalJa = event.right().getValue();
                            //different owner can have the same folder name
                            //if the folder exists, we must find id of folder according to the directory name and the owner,
                            //it's necessary to use the front route for the resource link
                            final QueryBuilder foldersOwnersOrQuery = new QueryBuilder();
                            Boolean isFolderProcessing = false;

                            for (int i = 0; i < globalJa.size(); i++) {
                                final JsonObject j = globalJa.getJsonObject(i);

                                // processing only files that have a folder
                                if (j != null && !StringUtils.isEmpty(j.getString("folder"))) {
                                    isFolderProcessing = true;
                                    final QueryBuilder folderOwnerQuery = new QueryBuilder();
                                    folderOwnerQuery.and(
                                            QueryBuilder.start("folder").is(j.getString("folder")).get(),
                                            QueryBuilder.start("owner").is(j.getString("owner")).get());
                                    foldersOwnersOrQuery.or(folderOwnerQuery.get());
                                }
                            }

                            final Map<String, Map<String, String>> mapOwnerMapNameFolderId = new HashMap<>();
                            //finding ids of folder found.
                            if (isFolderProcessing) {
                                //find only authorized folder entry and not file
                                final QueryBuilder queryFindFolderIds = new QueryBuilder().and(
                                        QueryBuilder.start("file").exists(false).get(), rightsQuery.get(),
                                        foldersOwnersOrQuery.get());
                                //search all folder of main result set and format the search result
                                findFoldersIdAndFormatResult(globalJa, queryFindFolderIds, columnsHeader,
                                        searchWordsLst, locale, userId, handler);
                            } else {
                                //all files without folder
                                final JsonArray res = formatSearchResult(globalJa, columnsHeader,
                                        searchWordsLst, locale, userId, mapOwnerMapNameFolderId);
                                handler.handle(new Either.Right<String, JsonArray>(res));
                            }
                        } else {
                            handler.handle(new Either.Left<String, JsonArray>(event.left().getValue()));
                        }
                        if (log.isDebugEnabled()) {
                            log.debug(
                                    "[WorkspaceSearchingEvents][searchResource] The resources searched by user are finded");
                        }
                    }
                }));
    } else {
        handler.handle(new Either.Right<String, JsonArray>(new fr.wseduc.webutils.collections.JsonArray()));
    }
}