Enrich all Elasticsearch REST requests by adding filters from plugin

Hi,

would it be possible to catch all Elasticsearch requests, from a plugin, in order to enrich them by adding some filters without changing the query client-side?
E.g. user calls GET /_search and I will add:

{
  "query": { 
    "bool": { 
      "must": [
        { "match": { "title":   "Search"        }}, 
        { "match": { "content": "Elasticsearch" }}  
      ],
      "filter": [ 
        { "term":  { "status": "published" }}, 
        { "range": { "publish_date": { "gte": "2015-01-01" }}} 
      ]
    }
  }
}

in the plugin. somewhere.
Is it possible to do that? If so, what methods / classes do I need to extend / override to add my logic?

Thanks a lot!

Why not using an alias? An alias can have filters. See https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html#filtered

Thanks for the answer @dadoonet, unfortunately I cannot use aliases because I need to add those filters in a transparent way so that user still keeps doing the same requests in the same way.

That does not change the request. Just the index name.

@dadoonet, ok but when user make a normal and usual call (in the example GET /_search) how can I catch that call in the plugin to block it and replace it with another call to the previously created alias?

also, this would required to first create aliases for each filter I want to use in my Elasticsearch and I would like to avoid it.
The alias feature could be useful in my case, but just in case I will be able to replace the normal request a user is making with a custom one so that I could dynamically create an alias with a specific filter and then make the filtered request instead of the original one.

Dynamically based on what?

Dynamically based on what?

Based on the filters I want to add for the specific request.
Anyway, so there is no way to catch requests from an Elasticsearch plugin in order to replace them somehow?

Based on the filters I want to add for the specific request.

So I don't understand... Can't you just do that by yourself in your application?

I mean that yes you can probably do that within a plugin but this is looking over engineering to me. Unless I'm missing something. Writing and more than that, maintaining a plugin, is not something that trivial IMHO.

That's why I'm asking and trying to check if you really need that.

I mean that yes you can probably do that within a plugin but this is looking over engineering to me. Unless I'm missing something. Writing and more than that, maintaining a plugin, is not something that trivial IMHO.
That's why I'm asking and trying to check if you really need that.

yes you are right and thank you for that.
The problem is that I am really unable to touch the application making requests, besides the plugin will be used on generic Elasticsearch instances that are called by different and generic applications.

Do have any idea about where I can start to try something from the plugin in order to do what I need?
Any specific class I need to extend or methods to override in order to catch the request?

I don't understand.

If you can not change the application, how do you know what filter you need to apply when someone just call GET /_search? There's no parameter to decide... I think I'm lost.

Anyway, I wrote a long time ago how to write a plugin which adds a REST endpoint. That does not intercept your requests and add some filters though.

May be this can be a source of inspiration though: https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/SecurityRestFilter.java (this code is not under Apache2 License though but that could help).

I get those information about filters from an external service I call where the source application put specific filters I will use in the plugin.
I know it sound strange but there is reason for logic.

yes I know about it and also found an official example about how to create a custom REST endpoint, but unfortunately it's not helpful for my needs.

thank you, I'll try to look at it and will report here in case of progress.

Regards

Hi,

what about org.elasticsearch.action.support.ActionFilter (https://github.com/elastic/elasticsearch/blob/master/server/src/main/java/org/elasticsearch/action/support/ActionFilter.java)?
Do you think it can be useful for my needs?

Thanks

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.