Browse Source

#2025 clear search context

Shinsuke Sugaya 6 years ago
parent
commit
400f565638

+ 10 - 3
src/main/java/org/codelibs/fess/es/client/FessEsClient.java

@@ -729,25 +729,28 @@ public class FessEsClient implements Client {
         if (condition.build(searchRequestBuilder)) {
             final FessConfig fessConfig = ComponentUtil.getFessConfig();
 
+            String scrollId = null;
             try {
                 if (logger.isDebugEnabled()) {
                     logger.debug("Query DSL:\n" + searchRequestBuilder.toString());
                 }
                 SearchResponse response = searchRequestBuilder.execute().actionGet(ComponentUtil.getFessConfig().getIndexSearchTimeout());
 
-                String scrollId = response.getScrollId();
+                scrollId = response.getScrollId();
                 while (scrollId != null) {
                     final SearchHits searchHits = response.getHits();
                     final SearchHit[] hits = searchHits.getHits();
                     if (hits.length == 0) {
-                        scrollId = null;
                         break;
                     }
 
                     for (final SearchHit hit : hits) {
                         count++;
                         if (!cursor.apply(creator.build(response, hit))) {
-                            scrollId = null;
+                            if (scrollId != null) {
+                                client.prepareClearScroll().addScrollId(scrollId)
+                                        .execute(ActionListener.wrap(res -> {}, e1 -> logger.warn("Failed to clear scrollId.", e1)));
+                            }
                             break;
                         }
                     }
@@ -758,6 +761,10 @@ public class FessEsClient implements Client {
                     scrollId = response.getScrollId();
                 }
             } catch (final SearchPhaseExecutionException e) {
+                if (scrollId != null) {
+                    client.prepareClearScroll().addScrollId(scrollId)
+                            .execute(ActionListener.wrap(res -> {}, e1 -> logger.warn("Failed to clear scrollId.", e1)));
+                }
                 throw new InvalidQueryException(messages -> messages.addErrorsInvalidQueryParseError(UserMessages.GLOBAL_PROPERTY_KEY),
                         "Invalid query: " + searchRequestBuilder, e);
             }

+ 7 - 0
src/main/java/org/codelibs/fess/es/config/allcommon/EsAbstractBehavior.java

@@ -41,6 +41,7 @@ import org.dbflute.cbean.result.ListResultBean;
 import org.dbflute.exception.FetchingOverSafetySizeException;
 import org.dbflute.exception.IllegalBehaviorStateException;
 import org.dbflute.util.DfTypeUtil;
+import org.elasticsearch.action.ActionListener;
 import org.elasticsearch.action.DocWriteResponse.Result;
 import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
 import org.elasticsearch.action.bulk.BulkItemResponse;
@@ -247,6 +248,9 @@ public abstract class EsAbstractBehavior<ENTITY extends Entity, CB extends Condi
             }
 
             if (!handler.apply(searchHits)) {
+                if (response.getScrollId() != null) {
+                    client.prepareClearScroll().addScrollId(response.getScrollId()).execute(ActionListener.wrap(() -> {}));
+                }
                 break;
             }
         }
@@ -380,6 +384,9 @@ public abstract class EsAbstractBehavior<ENTITY extends Entity, CB extends Condi
             count += hits.length;
             final BulkResponse bulkResponse = bulkRequest.execute().actionGet(bulkTimeout);
             if (bulkResponse.hasFailures()) {
+                if (response.getScrollId() != null) {
+                    client.prepareClearScroll().addScrollId(response.getScrollId()).execute(ActionListener.wrap(() -> {}));
+                }
                 throw new IllegalBehaviorStateException(bulkResponse.buildFailureMessage());
             }
         }

+ 7 - 0
src/main/java/org/codelibs/fess/es/log/allcommon/EsAbstractBehavior.java

@@ -41,6 +41,7 @@ import org.dbflute.cbean.result.ListResultBean;
 import org.dbflute.exception.FetchingOverSafetySizeException;
 import org.dbflute.exception.IllegalBehaviorStateException;
 import org.dbflute.util.DfTypeUtil;
+import org.elasticsearch.action.ActionListener;
 import org.elasticsearch.action.DocWriteResponse.Result;
 import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
 import org.elasticsearch.action.bulk.BulkItemResponse;
@@ -247,6 +248,9 @@ public abstract class EsAbstractBehavior<ENTITY extends Entity, CB extends Condi
             }
 
             if (!handler.apply(searchHits)) {
+                if (response.getScrollId() != null) {
+                    client.prepareClearScroll().addScrollId(response.getScrollId()).execute(ActionListener.wrap(() -> {}));
+                }
                 break;
             }
         }
@@ -380,6 +384,9 @@ public abstract class EsAbstractBehavior<ENTITY extends Entity, CB extends Condi
             count += hits.length;
             final BulkResponse bulkResponse = bulkRequest.execute().actionGet(bulkTimeout);
             if (bulkResponse.hasFailures()) {
+                if (response.getScrollId() != null) {
+                    client.prepareClearScroll().addScrollId(response.getScrollId()).execute(ActionListener.wrap(() -> {}));
+                }
                 throw new IllegalBehaviorStateException(bulkResponse.buildFailureMessage());
             }
         }

+ 7 - 0
src/main/java/org/codelibs/fess/es/user/allcommon/EsAbstractBehavior.java

@@ -41,6 +41,7 @@ import org.dbflute.cbean.result.ListResultBean;
 import org.dbflute.exception.FetchingOverSafetySizeException;
 import org.dbflute.exception.IllegalBehaviorStateException;
 import org.dbflute.util.DfTypeUtil;
+import org.elasticsearch.action.ActionListener;
 import org.elasticsearch.action.DocWriteResponse.Result;
 import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
 import org.elasticsearch.action.bulk.BulkItemResponse;
@@ -247,6 +248,9 @@ public abstract class EsAbstractBehavior<ENTITY extends Entity, CB extends Condi
             }
 
             if (!handler.apply(searchHits)) {
+                if (response.getScrollId() != null) {
+                    client.prepareClearScroll().addScrollId(response.getScrollId()).execute(ActionListener.wrap(() -> {}));
+                }
                 break;
             }
         }
@@ -380,6 +384,9 @@ public abstract class EsAbstractBehavior<ENTITY extends Entity, CB extends Condi
             count += hits.length;
             final BulkResponse bulkResponse = bulkRequest.execute().actionGet(bulkTimeout);
             if (bulkResponse.hasFailures()) {
+                if (response.getScrollId() != null) {
+                    client.prepareClearScroll().addScrollId(response.getScrollId()).execute(ActionListener.wrap(() -> {}));
+                }
                 throw new IllegalBehaviorStateException(bulkResponse.buildFailureMessage());
             }
         }