Question: I'm trying to create a regex to find a string with a double-underscore `__`, multiple underscores `_` and then another double-underscore `__` and extract the part before the final `__<string>`  

The first delimiter should be `__` and then multiple `_`, and string ends with `__<String>`

The result should be such that string before the second \_\_

example 1- `UK__SATHISH_KUMAR__LONDON` should result to `UK__SATHISH_KUMAR`

example 2- `UK__SATHISH_KUMAR_MALE__LONDON` should result to `UK__SATHISH_KUMAR_MALE`

```
public static final String RULE_FILE_NAME_PATTERN =
    "(([a-zA-Z]+)__(([a-zA-Z]+_[a-zA-Z]+_[a-zA-Z]+_[a-zA-Z]+)|([a-zA-Z]+_[a-zA-Z]+_[a-zA-Z]+)|([a-zA-Z]+_[a-zA-Z]+)|([a-zA-Z]+)))(__[\\w]+)*";

```

This pattern works but fails sonarqube as it's long, can some one help to get a shorter regex?

Answer: 
The limit is called [max\_result\_window](https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html#index-max-result-window) and default value is 10k. Mathematically this is the max value `size+from` can take.

from:1, size:10000 will give error.

from:5, size:9996 will give error.

from:9999, size:2 will give error.

[Search after](https://www.elastic.co/guide/en/elasticsearch/reference/current/paginate-search-results.html#search-after) is the recommended alternative if you want deeper results.
You can update existing index settings with this query:

```
PUT myexistingindex/_settings
{
  "settings": {
    "max_result_window": 20000000
  }
}

```

If your are creating dynamic index, you can give max result window parameter in settings.

In Java like this:

```
private void createIndex(String indexName) throws IOException {
    Settings settings = Settings.builder()
            .put("number_of_shards", 1)
            .put("number_of_replicas", 0)
            .put("index.translog.durability", "async")
            .put("index.translog.sync_interval", "5s")
            .put("max_result_window", "20000000").build();

    CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName).settings(settings);
    restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
}

```

After these configurations, you can give "from" parameters up to 20 million.

But this way is not recommended.

You can review this document: [Scroll Api](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/paginate-search-results.html#scroll-search-results)