org.springframework.data.mongodb.core.aggregation.MatchOperation Java Examples

The following examples show how to use org.springframework.data.mongodb.core.aggregation.MatchOperation. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UserInfoServiceImpl.java    From ExecDashboard with Apache License 2.0 5 votes vote down vote up
@Override
public List<String> getFrequentExecutives() {
	List<String> executives = new LinkedList<>();
	try {
		Map<String, Integer> resultsMap = new HashMap<>();
		Map<String, Integer> resultsMapSorted = new LinkedHashMap<>();
		List<String> views = new ArrayList<>();
		views.add(PORTFOLIO);
		views.add(PORTFOLIOMETRIC);
		GroupOperation groupByExecutiveViewId = Aggregation.group(EXECUTIVEVIEWID).count().as(TOTAL);
		MatchOperation filter = Aggregation.match(new Criteria(VIEW).in(views).andOperator(
				Criteria.where(TIMESTAMP).gte(getTimeStamp(30)), Criteria.where(EXECUTIVEVIEWID).ne(null)));
		Aggregation aggregation = Aggregation.newAggregation(filter, groupByExecutiveViewId);
		AggregationResults<DBObject> temp = mongoTemplate.aggregate(aggregation, TRACKVIEWS, DBObject.class);
		if (temp != null) {
			List<DBObject> results = temp.getMappedResults();
			if (results != null && !results.isEmpty()) {
				for (DBObject object : results) {
					if (object.get(FIRSTORDER) != null) {
						String id = object.get(FIRSTORDER).toString();
						Integer total = (Integer) object.get(TOTAL);
						resultsMap.put(id, total);
					}
				}
				resultsMap.entrySet().stream().sorted(Map.Entry.<String, Integer> comparingByValue().reversed())
						.forEachOrdered(x -> resultsMapSorted.put(x.getKey(), x.getValue()));
				resultsMapSorted.forEach((eid, v) -> {
					ExecutiveSummaryList executive = executiveSummaryListRepository.findByEid(eid);
					if (executive != null) {
						executives.add(executive.getFirstName() + ", " + executive.getLastName());
					}
				});
			}
		}
	} catch (Exception e) {
		LOG.error("User Tracking, getFrequentExecutives :: " + e);
	}
	return executives;
}
 
Example #2
Source File: UserInfoServiceImpl.java    From ExecDashboard with Apache License 2.0 5 votes vote down vote up
@Override
public List<String> getFrequentApplications() {
	List<String> applications = new LinkedList<>();
	try {
		Map<String, Integer> resultsMap = new HashMap<>();
		Map<String, Integer> resultsMapSorted = new LinkedHashMap<>();
		List<String> views = new ArrayList<>();
		views.add(PRODUCTMETRIC);
		views.add(PRODUCT);
		GroupOperation groupByApplicationViewId = Aggregation.group(APPLICATIONVIEWID).count().as(TOTAL);
		MatchOperation filter = Aggregation.match(new Criteria(VIEW).in(views).andOperator(
				Criteria.where(TIMESTAMP).gte(getTimeStamp(30)), Criteria.where(APPLICATIONVIEWID).ne(null)));
		Aggregation aggregation = Aggregation.newAggregation(filter, groupByApplicationViewId);
		AggregationResults<DBObject> temp = mongoTemplate.aggregate(aggregation, TRACKVIEWS, DBObject.class);
		if (temp != null) {
			List<DBObject> results = temp.getMappedResults();
			if (results != null && !results.isEmpty()) {
				for (DBObject object : results) {
					if (object.get(FIRSTORDER) != null) {
						String id = object.get(FIRSTORDER).toString();
						Integer total = (Integer) object.get(TOTAL);
						resultsMap.put(id, total);
					}
				}
				resultsMap.entrySet().stream().sorted(Map.Entry.<String, Integer> comparingByValue().reversed())
						.forEachOrdered(x -> resultsMapSorted.put(x.getKey(), x.getValue()));
				resultsMapSorted.forEach((appId, v) -> {
					ApplicationDetails app = applicationDetailsRepository.findByAppId(appId);
					if (app != null) {
						applications.add(app.getAppName() + " - " + app.getLob());
					}
				});
			}
		}
	} catch (Exception e) {
		LOG.error("User Tracking, getFrequentApplications :: " + e);
	}
	return applications;
}
 
Example #3
Source File: UserInfoServiceImpl.java    From ExecDashboard with Apache License 2.0 5 votes vote down vote up
@Override
public List<String> getFrequentCards() {
	List<String> metrics = new LinkedList<>();
	try {
		Map<String, Integer> resultsMap = new HashMap<>();
		Map<String, Integer> resultsMapSorted = new LinkedHashMap<>();
		List<String> views = new ArrayList<>();
		views.add(PRODUCTMETRIC);
		views.add(PORTFOLIOMETRIC);
		GroupOperation groupByApplicationViewId = Aggregation.group(METRICSNAME).count().as(TOTAL);
		MatchOperation filter = Aggregation.match(new Criteria(VIEW).in(views).andOperator(
				Criteria.where(TIMESTAMP).gte(getTimeStamp(30)), Criteria.where(METRICSNAME).ne(null)));
		Aggregation aggregation = Aggregation.newAggregation(filter, groupByApplicationViewId);
		AggregationResults<DBObject> temp = mongoTemplate.aggregate(aggregation, TRACKVIEWS, DBObject.class);
		if (temp != null) {
			List<DBObject> results = temp.getMappedResults();
			if (results != null && !results.isEmpty()) {
				for (DBObject object : results) {
					if (object.get(ID) != null) {
						String id = object.get(ID).toString();
						Integer total = (Integer) object.get(TOTAL);
						resultsMap.put(id, total);
					}
				}
				resultsMap.entrySet().stream().sorted(Map.Entry.<String, Integer> comparingByValue().reversed())
						.forEachOrdered(x -> resultsMapSorted.put(x.getKey(), x.getValue()));
				resultsMapSorted.forEach((k, v) -> metrics.add(k));
			}
		}
	} catch (Exception e) {
		LOG.error("User Tracking, getFrequentCards :: " + e);
	}
	return metrics;
}
 
Example #4
Source File: AuthorServiceImpl.java    From biliob_backend with MIT License 5 votes vote down vote up
private MatchOperation getAggregateMatch(int days, Long mid) {
    if (days == -1) {
        return Aggregation.match(Criteria.where("mid").is(mid));
    } else {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -days);
        return Aggregation.match(Criteria.where("mid").is(mid).and("datetime").gt(c.getTime()));
    }
}
 
Example #5
Source File: AuthorGroupServiceImpl.java    From biliob_backend with MIT License 5 votes vote down vote up
public List<AuthorGroup> listAuthorList(MatchOperation match, Long page, Integer pageSize) {

        List<AuthorGroup> a = mongoTemplate.aggregate(
                Aggregation.newAggregation(
                        match,
                        Aggregation.sort(Sort.by("stars").descending()),
                        Aggregation.skip((page - 1) * pageSize),
                        Aggregation.limit(pageSize),
                        Aggregation.lookup("user", "creator._id", "_id", "creator"),
                        Aggregation.unwind("creator"),
                        Aggregation.lookup("user", "maintainer._id", "_id", "maintainer"),
                        Aggregation.unwind("maintainer"),
                        Aggregation.lookup("user_star_author_group", "_id", "groupId", "starList"),
                        Aggregation.lookup("author_group_item", "_id", "gid", "midList"),
                        Aggregation.lookup("author", "midList.mid", "mid", "authorList"),
                        Aggregation.project("starList", "tagList", "name", "desc")
                                .andExpression("{ userId: 1}").as("starList")
                                .andExpression("{ face: 1, mid: 1}").as("authorList")
                                .andExpression("{ nickName: 1, _id: 1 }").as("creator")
                                .andExpression("{ nickName: 1, _id: 1 }").as("maintainer")
                ), AuthorGroup.class, AuthorGroup.class
        ).getMappedResults();
        ObjectId userId = UserUtils.getUserId();
        a.forEach(authorGroup -> {
            authorGroup.setStars(authorGroup.getStarList().size());
            authorGroup.setAuthors(authorGroup.getAuthorList().size());
            authorGroup.setAuthorList(authorGroup.getAuthorList().subList(0, authorGroup.getAuthors() > 5 ? 5 : authorGroup.getAuthors()));
            if (userId == null) {
                return;
            }
            setIsStared(userId, authorGroup);
            authorGroup.setStarList(null);
        });
        return a;
    }
 
Example #6
Source File: AuthorGroupServiceImpl.java    From biliob_backend with MIT License 5 votes vote down vote up
@Override
public List<AuthorGroup> listAuthorList(String keyword, Long page, Integer pageSize) {
    MatchOperation match;
    if (!"".equals(keyword)) {
        match = Aggregation.match(new Criteria().orOperator(Criteria.where("name").regex(keyword, "i"), Criteria.where("desc").regex(keyword, "i"), Criteria.where("tagList").is(keyword)));
    } else {
        match = Aggregation.match(new Criteria());
    }
    logger.info("{} {}", keyword, page);
    return this.listAuthorList(match, page, pageSize);
}
 
Example #7
Source File: AuthorGroupServiceImpl.java    From biliob_backend with MIT License 5 votes vote down vote up
public AuthorGroup getAuthorList(MatchOperation match) {
    AuthorGroup a = mongoTemplate.aggregate(
            Aggregation.newAggregation(
                    match,
                    Aggregation.lookup("user", "creator._id", "_id", "creator"),
                    Aggregation.unwind("creator"),
                    Aggregation.lookup("user", "maintainer._id", "_id", "maintainer"),
                    Aggregation.unwind("maintainer"),
                    Aggregation.lookup("user_star_author_group", "_id", "groupId", "starList"),
                    Aggregation.lookup("author_group_item", "_id", "gid", "midList"),
                    Aggregation.lookup("author", "midList.mid", "mid", "authorList"),
                    Aggregation.project("starList", "tagList", "name", "desc", "authorList")
                            .andExpression("{ userId: 1}").as("starList")
                            .andExpression("{ nickName: 1, _id: 1 }").as("creator")
                            .andExpression("{ nickName: 1, _id: 1 }").as("maintainer"),
                    Aggregation.project().andExpression("{data: 0, keyword: 0}").as("authorList")
            ), AuthorGroup.class, AuthorGroup.class
    ).getUniqueMappedResult();
    if (a != null) {
        a.setStars(a.getStarList().size());
        setIsStared(UserUtils.getUserId(), a);
        a.setStarList(null);
        a.setAuthors(a.getAuthorList().size());
        authorUtil.getInterval(a.getAuthorList());
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -1);
        a.getAuthorList().stream().filter(author -> author.getRank() == null || author.getRank().getUpdateTime() == null || author.getRank().getUpdateTime().before(c.getTime())).forEach(authorService::getRankData);
    }
    return a;
}
 
Example #8
Source File: ZipsAggregationLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenStatesHavePopGrtrThan10MillionAndSorted_thenSuccess() {

    GroupOperation groupByStateAndSumPop = group("state").sum("pop").as("statePop");
    MatchOperation filterStates = match(new Criteria("statePop").gt(10000000));
    SortOperation sortByPopDesc = sort(new Sort(Direction.DESC, "statePop"));

    Aggregation aggregation = newAggregation(groupByStateAndSumPop, filterStates, sortByPopDesc);
    AggregationResults<StatePopulation> result = mongoTemplate.aggregate(aggregation, "zips", StatePopulation.class);

    /*
     * Assert that all states have population
     * greater than 10000000
     */
    result.forEach(statePop -> {
        assertTrue(statePop.getStatePop() > 10000000);
    });

    /*
     * Assert that states fetched are in sorted by
     * decreasing population
     */
    List<StatePopulation> actualList = StreamSupport.stream(result.spliterator(), false)
      .collect(Collectors.toList());

    List<StatePopulation> expectedList = new ArrayList<>(actualList);
    Collections.sort(expectedList, (sp1, sp2) -> sp2.getStatePop() - sp1.getStatePop());

    assertEquals(expectedList, actualList);

}
 
Example #9
Source File: AuthorServiceImpl.java    From biliob_backend with MIT License 4 votes vote down vote up
private Author getAggregatedData(Long mid, int days) {
    Calendar timer = Calendar.getInstance();
    MatchOperation match = getAggregateMatch(days, mid);
    Aggregation a = Aggregation.newAggregation(
            match,
            Aggregation.project("fans", "archiveView", "articleView", "like", "attention", "datetime", "mid").and("datetime").dateAsFormattedString("%Y-%m-%d").as("date"),
            Aggregation.group("date")
                    .first("datetime").as("datetime")
                    .first("fans").as("fans")
                    .first("archiveView").as("archiveView")
                    .first("articleView").as("articleView")
                    .first("like").as("like")
                    .first("attention").as("attention")
                    .first("mid").as("mid"),
            Aggregation.sort(Sort.Direction.DESC, "datetime"),
            Aggregation.group().push(
                    new BasicDBObject("datetime", "$datetime")
                            .append("fans", "$fans")
                            .append("archiveView", "$archiveView")
                            .append("articleView", "$articleView")
                            .append("archive", "$archive")
                            .append("article", "$article")
                            .append("like", "$like")
            ).as("data").first("mid").as("mid"),
            Aggregation.lookup("author", "mid", "mid", "author"),
            Aggregation.unwind("author"),
            (aoc) -> new Document("$addFields", new Document("author.data", "$data")),
            Aggregation.replaceRoot("author"),
            Aggregation.lookup("author_interval", "mid", "mid", "interval"),
            (aoc) -> new Document("$addFields", new Document("obInterval", new Document("$arrayElemAt", Arrays.asList("$interval.interval", 0)))),
            Aggregation.lookup("author_achievement", "mid", "author.mid", "achievements"),
            Aggregation.project().andExpression("{ mid: 0}").as("data")
    );
    Author data = mongoTemplate.aggregate(a, Author.Data.class, Author.class).getUniqueMappedResult();
    long deltaTime = Calendar.getInstance().getTimeInMillis() - timer.getTimeInMillis();
    // 太慢,则精简数据
    logger.info(deltaTime);
    if (deltaTime > 7000) {
        adminService.reduceByMid(mid);
    }
    return data;
}