package org.codelibs.elasticsearch.dynarank.filter;

import org.codelibs.elasticsearch.dynarank.ranker.DynamicRanker;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.action.search.SearchAction;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.ActionFilter;
import org.elasticsearch.action.support.ActionFilterChain;
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.script.ScriptService;

public class SearchActionFilter extends AbstractComponent implements
        ActionFilter {

    private int order;

    private DynamicRanker dynamicRanker;

    @Inject
    public SearchActionFilter(final Settings settings,
            final ClusterService clusterService,
            final ScriptService scriptService) {
        super(settings);
        dynamicRanker = new DynamicRanker(settings, clusterService,
                scriptService);

        order = settings.getAsInt("indices.dynarank.filter.order", 10);
    }

    @Override
    public int order() {
        return order;
    }

    @Override
    public void apply(final String action, final ActionRequest request,
            final ActionListener listener, final ActionFilterChain chain) {
        if (!SearchAction.INSTANCE.name().equals(action)) {
            chain.proceed(action, request, listener);
            return;
        }

        final SearchRequest searchRequest = (SearchRequest) request;
        final ActionListener<SearchResponse> wrappedListener = dynamicRanker
                .wrapActionListener(action, searchRequest, listener);
        chain.proceed(action, request, wrappedListener == null ? listener
                : wrappedListener);
    }

    @Override
    public void apply(final String action, final ActionResponse response,
            final ActionListener listener, final ActionFilterChain chain) {
        chain.proceed(action, response, listener);
    }

}

@@ -408,32 +408,83 @@
 SearchResponse;%0A
+import org.elasticsearch.action.search.SearchType;%0A
 import org.elast
@@ -517,24 +517,24 @@
 tionFilter;%0A
-
 import org.e
@@ -991,24 +991,102 @@
 micRanker;%0A%0A
+    private ThreadLocal%3CSearchType%3E currentSearchType = new ThreadLocal%3C%3E();%0A%0A
     @Inject%0A
@@ -1887,16 +1887,359 @@
 equest;%0A
+        final SearchType searchType = currentSearchType.get();%0A        if (searchType == null) %7B%0A            try %7B%0A                currentSearchType.set(searchRequest.searchType());%0A                chain.proceed(action, request, listener);%0A            %7D finally %7B%0A                currentSearchType.remove();%0A            %7D%0A        %7D else %7B%0A    
         
@@ -2323,16 +2323,20 @@
         
+    
 .wrapAct
@@ -2373,32 +2373,36 @@
 est, listener);%0A
+    
         chain.pr
@@ -2475,16 +2475,20 @@
         
+   
+ 
 : wrappe
@@ -2495,24 +2495,34 @@
 dListener);%0A
+        %7D%0A
     %7D%0A%0A    @

fix #2