javaspring-bootelasticsearchopensearchamazon-opensearch

OpenSearch - Error when creating index using Java client


I'm trying to create index with given settings from json.

{
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "my_synonym_analyzer": {
            "type": "custom",
            "tokenizer": "whitespace",
            "filter": [
              "my_synonym_filter"
            ]
          }
        },
        "filter": {
          "my_synonym_filter": {
            "type": "synonym_graph",
            "synonyms": []
          }
        }
      }
    }
  }
}

My code to create index is :


import jakarta.json.stream.JsonParser;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.opensearch.OpenSearchClient;
import org.opensearch.client.opensearch._types.mapping.TypeMapping;
import org.opensearch.client.opensearch.indices.CreateIndexRequest;
import org.opensearch.client.opensearch.indices.CreateIndexResponse;
import org.opensearch.client.opensearch.indices.IndexSettings;
import org.opensearch.common.io.Streams;
import org.springframework.beans.factory.annotation.Autowired;

public class IndexManager {

  @Autowired private OpenSearchClient searchClient;

  protected void create() throws IOException {

    if (this.indexExists()) {
      this.delete();
    }

    JsonpMapper mapper = searchClient._transport().jsonpMapper();
    try (Reader readerSettings =
            new InputStreamReader(
                Objects.requireNonNull(
                    this.getClass()
                        .getResourceAsStream("testindex-index-settings.json")));
        Reader readerMappings =
            new InputStreamReader(
                Objects.requireNonNull(
                    this.getClass()
                        .getResourceAsStream("testindex-index-mappings.json")));
        JsonParser settingsParser =
            mapper
                .jsonProvider()
                .createParser(new StringReader(Streams.copyToString(readerSettings)));
        JsonParser mappingsParser =
            mapper
                .jsonProvider()
                .createParser(new StringReader(Streams.copyToString(readerMappings))); ) {

      String indexName = "testindex";

      IndexSettings indexSettings = IndexSettings._DESERIALIZER.deserialize(settingsParser, mapper);

      TypeMapping typeMapping = TypeMapping._DESERIALIZER.deserialize(mappingsParser, mapper);

      CreateIndexRequest indexRequest =
          new CreateIndexRequest.Builder()
              .index(indexName)
              .settings(indexSettings)
              .mappings(typeMapping)
              .build();

      CreateIndexResponse indexResponse = searchClient.indices().create(indexRequest); // line 93

      log.info("Index {} created with response [{}]", indexName, indexResponse.toString());
    }
  }
}

When trying to create index from above code, I'm getting unknown setting error:

org.opensearch.client.opensearch._types.OpenSearchException: Request failed: [illegal_argument_exception] unknown setting [index.settings.analysis.analyzer.my_synonym_analyzer.filter] please check that any required plugins are installed, or check the breaking changes documentation for removed settings
    at org.opensearch.client.transport.rest_client.RestClientTransport.getHighLevelResponse(RestClientTransport.java:273) ~[opensearch-java-2.4.0.jar:na]
    at org.opensearch.client.transport.rest_client.RestClientTransport.performRequest(RestClientTransport.java:143) ~[opensearch-java-2.4.0.jar:na]
    at org.opensearch.client.opensearch.indices.OpenSearchIndicesClient.create(OpenSearchIndicesClient.java:248) ~[opensearch-java-2.4.0.jar:na]
    at com.joinhobnob.crm.service.search.command.IndexManager.create(IndexManager.java:93) ~[main/:na]

But while creating the index from opensearch's dashboard console, with same settings its working fine.

curl -XPUT "http://opensearch:9200/test_index?pretty" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "my_synonym_analyzer": {
            "type": "custom",
            "tokenizer": "whitespace",
            "filter": [
              "my_synonym_filter"
            ]
          }
        },
        "filter": {
          "my_synonym_filter": {
            "type": "synonym_graph",
            "synonyms": []
          }
        }
      }
    }
  }
}'

Response: 
{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "test_index"
}

I've gone through the given links, but have not found the solution yet.

Elasticsearch error while mapping - unknown setting

Creating Index with OpenSearch Java Client

Can anyone help me find the solution?


Solution

  • You need to remove the settings section in your testindex-index-settings.json file

      {
        "index": {
          "analysis": {
            "analyzer": {
              "my_synonym_analyzer": {
                "type": "custom",
                "tokenizer": "whitespace",
                "filter": [
                  "my_synonym_filter"
                ]
              }
            },
            "filter": {
              "my_synonym_filter": {
                "type": "synonym_graph",
                "synonyms": []
              }
            }
          }
        }
      }