# How to intercept search request (in a pluign) without creating separate RestAction

**URL:** <https://discuss.elastic.co/t/how-to-intercept-search-request-in-a-pluign-without-creating-separate-restaction/19714>\
**Category:** Elasticsearch\
**Created:** [September 10, 2014, 1:17pm UTC](https://discuss.elastic.co/t/how-to-intercept-search-request-in-a-pluign-without-creating-separate-restaction/19714 "2014-09-10T13:17:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Piotr\_Majewski](https://avatars.discourse-cdn.com/v4/letter/p/4da419/32.png) [@Piotr\_Majewski](https://discuss.elastic.co/u/Piotr_Majewski)\
**Post date:** [September 10, 2014, 1:17pm UTC](https://discuss.elastic.co/t/how-to-intercept-search-request-in-a-pluign-without-creating-separate-restaction/19714/1 "2014-09-10T13:17:41Z")

</div>

Is it possible to intercept the request (and save the payload in ES) in a  
plugin without creating a separate RestAction ?

1. I want to avoid having to add separate url patterns  
(like [https://github.com/jprante/elasticsearch-arrayformat](https://github.com/jprante/elasticsearch-arrayformat))
2. I want to return the same response ES would return without the plugin.

What would be the best possible to log search request into elasticsearch ?  
I know I can create a nginx proxy that would do that but I wanted to have a  
solution built in ES.

I was thinking of a service that would schedule puting the payload in ES  
so the plugin wouldn't block the response?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/505701c3-f8a0-4a1b-ae58-d6815567a1dc%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/505701c3-f8a0-4a1b-ae58-d6815567a1dc%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![android.kc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/android.kc/32/58806_2.png) [@android.kc](https://discuss.elastic.co/u/android.kc)\
**Post date:** [January 21, 2017, 6:49am UTC](https://discuss.elastic.co/t/how-to-intercept-search-request-in-a-pluign-without-creating-separate-restaction/19714/2 "2017-01-21T06:49:34Z")

</div>

Hi @Piotr_Majewski,

Did you end up with some solution? I have a ES API extension plugin to intercept all requests sent to ES. you may try the code below. use ES 5.1.x

```
public class RestRequestInterceptorAction extends BaseRestHandler {

    private static final Logger logger = Loggers.getLogger(RestRequestInterceptorAction.class);
    
    @Inject
    public RestRequestInterceptorAction(Settings settings, RestController controller) {
        super(settings);
        controller.registerFilter(new RestFilter() {
            @Override
            public void process(RestRequest request, RestChannel channel, NodeClient client, RestFilterChain filterChain)
                    throws Exception {
                
                RestRequest newRequest = new RestRequest(request.params(), request.path()) {
                    @Override
                    public String uri() {
// logger.info("add customize uri");
                        return request.uri();
                    }

                    @Override
                    public Method method() {
// logger.info("method shall be the same");
                        return request.method();
                    }

                    @Override
                    public Iterable<Entry<String, String>> headers() {
// logger.info("add customized header to request");
                        return request.headers();
                    }

                    @Override
                    public String header(String name) {
                        return request.header(name);
                    }

                    @Override
                    public boolean hasContent() {
// logger.info("has Contect?:" + request.hasContent());
                        return request.hasContent();
                    }

                    @Override
                    public BytesReference content() {
// logger.info("customized content is called");
                        return request.content();
                    }
                };
                filterChain.continueProcessing(newRequest, channel, client);
            }});
    }

}
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 10:03pm UTC](https://discuss.elastic.co/t/how-to-intercept-search-request-in-a-pluign-without-creating-separate-restaction/19714/3 "2017-07-05T22:03:59Z")

</div>


