添加帖子是否展示的功能(四)algolia 、es、百度推送处理

es 和 algolia 重建索引, 百度推送

"/admin/search/index" 管理员重建索引

AdminProcessor.java
索引中只有普通和广播两种帖子

@RequestProcessing(value = "/admin/search/index", method = HttpMethod.POST)
    @Before({StopwatchStartAdvice.class, PermissionCheck.class})
    @After(StopwatchEndAdvice.class)
    public void rebuildArticleSearchIndex(final RequestContext context) {
......
                    final List<JSONObject> articles = articleQueryService.getValidArticles(pageNum, 50, Article.ARTICLE_TYPE_C_NORMAL, Article.ARTICLE_TYPE_C_CITY_BROADCAST);
......
    }

ArticleQueryService.java

    public List<JSONObject> getValidArticles(final int currentPageNum, final int pageSize, final int... types) throws ServiceException {
        try {
            final Query query = new Query().addSort(Keys.OBJECT_ID, SortDirection.DESCENDING).setPageCount(1).
                    setPage(currentPageNum, pageSize);

            if (null != types && types.length > 0) {
                final List<Filter> typeFilters = new ArrayList<>();
                for (int i = 0; i < types.length; i++) {
                    final int type = types[i];

                    typeFilters.add(new PropertyFilter(Article.ARTICLE_TYPE, FilterOperator.EQUAL, type));
                }

                final CompositeFilter typeFilter = new CompositeFilter(CompositeFilterOperator.OR, typeFilters);
                final List<Filter> filters = new ArrayList<>();
                filters.add(typeFilter);
                filters.add(new PropertyFilter(Article.ARTICLE_STATUS, FilterOperator.NOT_EQUAL, Article.ARTICLE_STATUS_C_INVALID));
                /**
                 * create by: qiankunpingtai
                 * create time: 2019/5/14 22:45
                 * website:https://qiankunpingtai.cn
                 * description:
                 * 过滤不展示的帖子
                 */
                filters.add( new PropertyFilter(Article.ARTICLE_DISPLAYABLE, FilterOperator.NOT_EQUAL, Article.ARTICLE_DISPLAYABLE_NOT));

                query.setFilter(new CompositeFilter(CompositeFilterOperator.AND, filters));
            } else {
                query.setFilter(new PropertyFilter(Article.ARTICLE_STATUS, FilterOperator.NOT_EQUAL, Article.ARTICLE_STATUS_C_INVALID));
            }

            final JSONObject result = articleRepository.get(query);

            return CollectionUtils.jsonArrayToList(result.optJSONArray(Keys.RESULTS));
        } catch (final RepositoryException e) {
            LOGGER.log(Level.ERROR, "Gets articles failed", e);

            throw new ServiceException(e);
        }
    }

重建指定帖子索引,百度推送

/admin/search-index-article

AdminProcessor.java

    @RequestProcessing(value = "/admin/search-index-article", method = HttpMethod.POST)
    @Before({StopwatchStartAdvice.class, PermissionCheck.class})
    @After(StopwatchEndAdvice.class)
    public void rebuildOneArticleSearchIndex(final RequestContext context) {
        final String articleId = context.getRequest().getParameter(Article.ARTICLE_T_ID);
        final JSONObject article = articleQueryService.getArticle(articleId);
        /**
         * create by: qiankunpingtai
         * create time: 2019/5/15 10:45
         * website:https://qiankunpingtai.cn
         * description:
         * 如果是不在列表中显示的帖子,直接跳过
         */
        final Integer articleDisplayable = article.optInt(Article.ARTICLE_DISPLAYABLE);
        if(!Article.ARTICLE_DISPLAYABLE_YES.equals(articleDisplayable)){
            return;
        }
        updateArticleSearchIndex(article);
        operationMgmtService.addOperation(Operation.newOperation(context.getRequest(), Operation.OPERATION_CODE_C_REBUILD_ARTICLE_SEARCH, articleId));

        context.sendRedirect(Latkes.getServePath() + "/admin/articles");
    }

上一篇 添加帖子是否展示的功能(三)查询优选贴、关注贴、城市贴