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.ds.callback;
17  
18  import java.util.HashSet;
19  import java.util.Map;
20  import java.util.Set;
21  import java.util.concurrent.atomic.AtomicLong;
22  
23  import javax.annotation.PostConstruct;
24  
25  import org.codelibs.core.stream.StreamUtil;
26  import org.codelibs.fess.es.client.FessEsClient;
27  import org.codelibs.fess.exception.DataStoreException;
28  import org.codelibs.fess.helper.CrawlingInfoHelper;
29  import org.codelibs.fess.helper.IndexingHelper;
30  import org.codelibs.fess.helper.SearchLogHelper;
31  import org.codelibs.fess.helper.SystemHelper;
32  import org.codelibs.fess.mylasta.direction.FessConfig;
33  import org.codelibs.fess.util.ComponentUtil;
34  import org.codelibs.fess.util.DocList;
35  import org.codelibs.fess.util.DocumentUtil;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  public class IndexUpdateCallbackImpl implements IndexUpdateCallback {
40      private static final Logger logger = LoggerFactory.getLogger(IndexUpdateCallbackImpl.class);
41  
42      protected AtomicLong documentSize = new AtomicLong(0);
43  
44      protected volatile long executeTime = 0;
45  
46      protected final DocListist.html#DocList">DocList docList = new DocList();
47  
48      protected long maxDocumentRequestSize;
49  
50      @PostConstruct
51      public void init() {
52          maxDocumentRequestSize = ComponentUtil.getFessConfig().getIndexerDataMaxDocumentRequestSizeAsInteger().longValue();
53      }
54  
55      /* (non-Javadoc)
56       * @see org.codelibs.fess.ds.callback.IndexUpdateCallback#store(java.util.Map)
57       */
58      @Override
59      public void store(final Map<String, String> paramMap, final Map<String, Object> dataMap) {
60          final long startTime = System.currentTimeMillis();
61          final FessConfig fessConfig = ComponentUtil.getFessConfig();
62          final FessEsClient fessEsClient = ComponentUtil.getFessEsClient();
63  
64          if (logger.isDebugEnabled()) {
65              logger.debug("Adding " + dataMap);
66          }
67  
68          //   required check
69          final Object urlObj = dataMap.get(fessConfig.getIndexFieldUrl());
70          if (urlObj == null) {
71              throw new DataStoreException("url is null. dataMap=" + dataMap);
72          }
73  
74          final IndexingHelper indexingHelper = ComponentUtil.getIndexingHelper();
75          final CrawlingInfoHelper crawlingInfoHelper = ComponentUtil.getCrawlingInfoHelper();
76          dataMap.put(fessConfig.getIndexFieldId(), crawlingInfoHelper.generateId(dataMap));
77  
78          final String url = dataMap.get(fessConfig.getIndexFieldUrl()).toString();
79  
80          if (fessConfig.getIndexerClickCountEnabledAsBoolean()) {
81              addClickCountField(dataMap, url, fessConfig.getIndexFieldClickCount());
82          }
83  
84          if (fessConfig.getIndexerFavoriteCountEnabledAsBoolean()) {
85              addFavoriteCountField(dataMap, url, fessConfig.getIndexFieldFavoriteCount());
86          }
87  
88          final Set<String> matchedLabelSet = ComponentUtil.getLabelTypeHelper().getMatchedLabelValueSet(url);
89          if (!matchedLabelSet.isEmpty()) {
90              final Set<String> newLabelSet = new HashSet<>();
91              final String[] oldLabels = DocumentUtil.getValue(dataMap, fessConfig.getIndexFieldLabel(), String[].class);
92              StreamUtil.stream(oldLabels).of(stream -> stream.forEach(newLabelSet::add));
93              matchedLabelSet.stream().forEach(newLabelSet::add);
94              dataMap.put(fessConfig.getIndexFieldLabel(), newLabelSet.toArray(new String[newLabelSet.size()]));
95          }
96  
97          if (!dataMap.containsKey(fessConfig.getIndexFieldDocId())) {
98              final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
99              dataMap.put(fessConfig.getIndexFieldDocId(), systemHelper.generateDocId(dataMap));
100         }
101 
102         ComponentUtil.getLanguageHelper().updateDocument(dataMap);
103 
104         synchronized (docList) {
105             docList.add(dataMap);
106             if (logger.isDebugEnabled()) {
107                 logger.debug("Added the document. " + "The number of a document cache is " + docList.size() + ".");
108             }
109 
110             final Long contentLength = DocumentUtil.getValue(dataMap, fessConfig.getIndexFieldContentLength(), Long.class);
111             if (contentLength != null) {
112                 docList.addContentSize(contentLength.longValue());
113                 if (docList.getContentSize() >= maxDocumentRequestSize) {
114                     indexingHelper.sendDocuments(fessEsClient, docList);
115                 }
116             } else if (docList.size() >= fessConfig.getIndexerDataMaxDocumentCacheSizeAsInteger().intValue()) {
117                 indexingHelper.sendDocuments(fessEsClient, docList);
118             }
119             executeTime += System.currentTimeMillis() - startTime;
120         }
121 
122         documentSize.getAndIncrement();
123 
124         if (logger.isDebugEnabled()) {
125             logger.debug("The number of an added document is " + documentSize.get() + ".");
126         }
127 
128     }
129 
130     @Override
131     public void commit() {
132         synchronized (docList) {
133             if (!docList.isEmpty()) {
134                 final IndexingHelper indexingHelper = ComponentUtil.getIndexingHelper();
135                 final FessEsClient fessEsClient = ComponentUtil.getFessEsClient();
136                 indexingHelper.sendDocuments(fessEsClient, docList);
137             }
138         }
139     }
140 
141     protected void addClickCountField(final Map<String, Object> doc, final String url, final String clickCountField) {
142         final SearchLogHelper searchLogHelper = ComponentUtil.getSearchLogHelper();
143         final int count = searchLogHelper.getClickCount(url);
144         doc.put(clickCountField, count);
145         if (logger.isDebugEnabled()) {
146             logger.debug("Click Count: " + count + ", url: " + url);
147         }
148     }
149 
150     protected void addFavoriteCountField(final Map<String, Object> doc, final String url, final String favoriteCountField) {
151         final SearchLogHelper searchLogHelper = ComponentUtil.getSearchLogHelper();
152         final long count = searchLogHelper.getFavoriteCount(url);
153         doc.put(favoriteCountField, count);
154         if (logger.isDebugEnabled()) {
155             logger.debug("Favorite Count: " + count + ", url: " + url);
156         }
157     }
158 
159     @Override
160     public long getDocumentSize() {
161         return documentSize.get();
162     }
163 
164     @Override
165     public long getExecuteTime() {
166         return executeTime;
167     }
168 
169 }