# Query index from within an AnalysisProvider?

**URL:** https://discuss.elastic.co/t/query-index-from-within-an-analysisprovider/327191
**Category:** Elasticsearch
**Created:** [March 7, 2023, 2:26pm UTC](https://discuss.elastic.co/t/query-index-from-within-an-analysisprovider/327191 "2023-03-07T14:26:51Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![jnioche](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jnioche/32/13090_2.png) [@jnioche](https://discuss.elastic.co/u/jnioche)
#### Post date: [March 7, 2023, 2:26pm UTC](https://discuss.elastic.co/t/query-index-from-within-an-analysisprovider/327191/1 "2023-03-07T14:26:51Z")

</div>

Hi,

Here is the context of my question: **[Synonym graph token filter backed by Elastic index](https://discuss.elastic.co/t/synonym-graph-token-filter-backed-by-elastic-index/326616)**

I am writing a custom _AnalysisPlugin_ to generate a list of synonyms from an Elastic index instead of using a static file.

A naive approach would be to use the REST client during its instantiation and point it to localhost then query in the usual way to populate the synonym map but I was wondering whether there would be a more efficient / cleaner way of doing e.g. I have a TokenFilterFactory with the following constructor _(IndexSettings indexSettings, Environment env, String name, Settings settings)_, can I retrieve the port number from there? Is there a better way of doing?

Thanks

---

<div class="post-metadata">

### Author: ![jnioche](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jnioche/32/13090_2.png) [@jnioche](https://discuss.elastic.co/u/jnioche)
#### Post date: [March 8, 2023, 11:47am UTC](https://discuss.elastic.co/t/query-index-from-within-an-analysisprovider/327191/2 "2023-03-08T11:47:31Z")

</div>

Answering my own question: yes, it is possible to retrieve the host and port of Elasticsearch from the settings of the Environment object

```auto
this.port = env.settings().getAsInt(http.port, 9200);
this.host = env.settings().get(network.host,localhost);

```

this way my plugin doesn't need the users to set the values themselves, which is great.

Using this info with the Java client within the plugin code, I am getting

```auto
"caused_by" : {
          "type" : "access_control_exception",
          "reason" : "access denied (\"java.net.SocketPermission\" \"0.0.0.0:9200\" \"connect,resolve\")"
        }

```

when querying the index with Elastic running as a docker container.

Is there a way I can either

1. configure ES to accept incoming traffic from a plugin it is runs?
2. use [Java security permissions in the plugin](https://www.elastic.co/guide/en/elasticsearch/plugins/8.6/plugin-authors.html#plugin-authors-jsm)?

Going back to my original question, is there an alternative to using the Java Ciient within the plugin to query Elastic?

Any help and pointers would be greatly appreciated

---

<div class="post-metadata">

### Author: ![jnioche](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jnioche/32/13090_2.png) [@jnioche](https://discuss.elastic.co/u/jnioche)
#### Post date: [March 8, 2023, 12:38pm UTC](https://discuss.elastic.co/t/query-index-from-within-an-analysisprovider/327191/3 "2023-03-08T12:38:38Z")

</div>

Changing the security permissions for the plugin did the trick.

Creating a file with

```auto
grant {
  permission java.net.SocketPermission "localhost", "accept,connect,listen,resolve";
  permission java.net.SocketPermission "0.0.0.0", "accept,connect,listen,resolve";
};

```

allows the code in my plugin to connect to ES

---

<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: [April 5, 2023, 12:38pm UTC](https://discuss.elastic.co/t/query-index-from-within-an-analysisprovider/327191/4 "2023-04-05T12:38:55Z")

</div>

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