View Javadoc
1   /*
2    * Copyright 2012-2025 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.job;
17  
18  import org.apache.logging.log4j.LogManager;
19  import org.apache.logging.log4j.Logger;
20  import org.codelibs.fess.mylasta.direction.FessConfig;
21  import org.codelibs.fess.opensearch.client.SearchEngineClient;
22  import org.codelibs.fess.util.ComponentUtil;
23  import org.opensearch.index.query.QueryBuilder;
24  import org.opensearch.index.query.QueryBuilders;
25  
26  /**
27   * Job for purging expired documents from the search index.
28   * This job removes documents that have passed their expiration time based on the expires field.
29   * It helps maintain the search index by cleaning up outdated content automatically.
30   */
31  public class PurgeDocJob {
32  
33      /** Logger instance for this class */
34      private static final Logger logger = LogManager.getLogger(PurgeDocJob.class);
35  
36      /**
37       * Default constructor for PurgeDocJob.
38       * Creates a new instance of the document purging job with default settings.
39       */
40      public PurgeDocJob() {
41          // Default constructor
42      }
43  
44      /**
45       * Executes the document purging job.
46       * Removes all documents from the search index that have expired based on their expires field.
47       *
48       * @return a string containing the execution result and any error messages
49       */
50      public String execute() {
51          final SearchEngineClient searchEngineClient = ComponentUtil.getSearchEngineClient();
52          final FessConfig fessConfig = ComponentUtil.getFessConfig();
53  
54          final StringBuilder resultBuf = new StringBuilder();
55  
56          // clean up
57          final QueryBuilder queryBuilder = QueryBuilders.rangeQuery(fessConfig.getIndexFieldExpires()).to("now");
58          try {
59              searchEngineClient.deleteByQuery(fessConfig.getIndexDocumentUpdateIndex(), queryBuilder);
60  
61          } catch (final Exception e) {
62              logger.error("Could not delete expired documents: {}", queryBuilder, e);
63              resultBuf.append(e.getMessage()).append("\n");
64          }
65  
66          return resultBuf.toString();
67      }
68  
69  }