Shinsuke Sugaya 10 years ago
parent
commit
c17398f523

+ 16 - 9
src/main/java/org/codelibs/fess/client/SearchClient.java

@@ -28,11 +28,11 @@ import org.codelibs.fess.util.ComponentUtil;
 import org.codelibs.fess.util.QueryResponseList;
 import org.elasticsearch.ElasticsearchException;
 import org.elasticsearch.action.ActionListener;
+import org.elasticsearch.action.ShardOperationFailedException;
 import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
 import org.elasticsearch.action.admin.indices.flush.FlushResponse;
 import org.elasticsearch.action.admin.indices.optimize.OptimizeResponse;
 import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
-import org.elasticsearch.action.deletebyquery.DeleteByQueryResponse;
 import org.elasticsearch.action.search.SearchRequestBuilder;
 import org.elasticsearch.action.search.SearchResponse;
 import org.elasticsearch.action.update.UpdateResponse;
@@ -60,7 +60,6 @@ import org.slf4j.LoggerFactory;
 
 import com.google.common.io.BaseEncoding;
 
-// TODO rename to SearchClient
 public class SearchClient {
     private static final Logger logger = LoggerFactory.getLogger(SearchClient.class);
 
@@ -108,13 +107,21 @@ public class SearchClient {
     }
 
     public void deleteByQuery(QueryBuilder queryBuilder) {
-        DeleteByQueryResponse response = client.prepareDeleteByQuery(index).setQuery(queryBuilder).execute().actionGet();
-        // TODO
-    }
-
-    public void deleteByQuery(String field, String value) {
-        // TODO Auto-generated method stub
-
+        try {
+            client.prepareDeleteByQuery(index).setQuery(queryBuilder).execute().actionGet().forEach(res -> {
+                ShardOperationFailedException[] failures = res.getFailures();
+                if (failures.length > 0) {
+                    StringBuilder buf = new StringBuilder(200);
+                    buf.append("Failed to delete documents in some shards.");
+                    for (ShardOperationFailedException failure : failures) {
+                        buf.append('\n').append(failure.toString());
+                    }
+                    throw new SearchException(buf.toString());
+                }
+            });
+        } catch (ElasticsearchException e) {
+            throw new SearchException("Failed to delete documents.", e);
+        }
     }
 
     public Map<String, Object> getDocument(final String query) {

+ 17 - 0
src/main/java/org/codelibs/fess/client/SearchException.java

@@ -0,0 +1,17 @@
+package org.codelibs.fess.client;
+
+import org.codelibs.fess.FessSystemException;
+
+public class SearchException extends FessSystemException {
+
+    private static final long serialVersionUID = 1L;
+
+    public SearchException(String message, Throwable cause) {
+        super(message, cause);
+    }
+
+    public SearchException(String message) {
+        super(message);
+    }
+
+}

+ 4 - 1
src/main/java/org/codelibs/fess/web/admin/SearchListAction.java

@@ -37,6 +37,8 @@ import org.codelibs.fess.helper.SystemHelper;
 import org.codelibs.fess.util.QueryResponseList;
 import org.codelibs.sastruts.core.annotation.Token;
 import org.codelibs.sastruts.core.exception.SSCActionMessagesException;
+import org.elasticsearch.index.query.QueryBuilder;
+import org.elasticsearch.index.query.QueryBuilders;
 import org.seasar.framework.beans.util.Beans;
 import org.seasar.struts.annotation.ActionForm;
 import org.seasar.struts.annotation.Execute;
@@ -252,7 +254,8 @@ public class SearchListAction implements Serializable {
                 if (!jobHelper.isCrawlProcessRunning()) {
                     final long time = System.currentTimeMillis();
                     try {
-                        searchClient.deleteByQuery(fieldHelper.docIdField, docId);
+                        QueryBuilder query = QueryBuilders.termQuery(fieldHelper.docIdField, docId);
+                        searchClient.deleteByQuery(query);
                         if (logger.isInfoEnabled()) {
                             logger.info("[EXEC TIME] index cleanup time: " + (System.currentTimeMillis() - time) + "ms");
                         }