OpenSearch -使用Java客户端创建索引时出错

2ledvvac  于 2023-05-12  发布在  Java
关注(0)|答案(1)|浏览(206)

我正在尝试使用json中的给定设置创建索引。

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

创建index的代码是:

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());
    }
  }
}

当试图从上面的代码创建索引时,我得到了未知的设置错误:

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]

但是当在opensearch的控制面板上创建索引时,使用相同的设置,它工作得很好。

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"
}

我已经通过了给定的链接,但还没有找到解决方案。
Elasticsearch error while mapping - unknown setting
Creating Index with OpenSearch Java Client
谁能帮我找到解决方案?

ia2d9nvy

ia2d9nvy1#

您需要删除testindex-index-settings.json文件中的settings

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

相关问题