View Javadoc
1   /*
2    * Copyright 2012-2019 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 java.io.IOException;
19  import java.util.Map;
20  import java.util.Set;
21  
22  import org.codelibs.core.lang.StringUtil;
23  import org.codelibs.fess.es.client.FessEsClient;
24  import org.codelibs.fess.helper.LabelTypeHelper;
25  import org.codelibs.fess.mylasta.direction.FessConfig;
26  import org.codelibs.fess.util.ComponentUtil;
27  import org.codelibs.fess.util.DocumentUtil;
28  import org.elasticsearch.common.xcontent.XContentFactory;
29  import org.elasticsearch.index.query.QueryBuilder;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  public class UpdateLabelJob {
34  
35      private static final Logger logger = LoggerFactory.getLogger(UpdateLabelJob.class);
36  
37      protected QueryBuilder queryBuilder = null;
38  
39      public String execute() {
40          final FessEsClient fessEsClient = ComponentUtil.getFessEsClient();
41          final FessConfig fessConfig = ComponentUtil.getFessConfig();
42          final LabelTypeHelper labelTypeHelper = ComponentUtil.getLabelTypeHelper();
43  
44          final StringBuilder resultBuf = new StringBuilder();
45  
46          try {
47              final long count =
48                      fessEsClient.updateByQuery(
49                              fessConfig.getIndexDocumentUpdateIndex(),
50                              option -> {
51                                  if (queryBuilder != null) {
52                                      option.setQuery(queryBuilder);
53                                  }
54                                  return option.setFetchSource(new String[] { fessConfig.getIndexFieldUrl() }, null);
55                              },
56                              (builder, hit) -> {
57                                  try {
58                                      final Map<String, Object> doc = hit.getSourceAsMap();
59                                      final String url = DocumentUtil.getValue(doc, fessConfig.getIndexFieldUrl(), String.class);
60                                      if (StringUtil.isNotBlank(url)) {
61                                          final Set<String> labelSet = labelTypeHelper.getMatchedLabelValueSet(url);
62                                          return builder.setDoc(XContentFactory.jsonBuilder().startObject()
63                                                  .field(fessConfig.getIndexFieldLabel(), labelSet.toArray(n -> new String[n])).endObject());
64                                      }
65                                  } catch (IOException e) {
66                                      logger.warn("Failed to process " + hit, e);
67                                  }
68                                  return null;
69                              });
70              resultBuf.append(count).append(" docs").append("\n");
71          } catch (final Exception e) {
72              logger.error("Could not update labels.", e);
73              resultBuf.append(e.getMessage()).append("\n");
74          }
75  
76          return resultBuf.toString();
77      }
78  
79      public UpdateLabelJob query(final QueryBuilder queryBuilder) {
80          this.queryBuilder = queryBuilder;
81          return this;
82      }
83  }