Elasticsearch - 通过 java API 而不是手动创建索引失败

Elasticsearch - creating index failure when doing it through java API but not manually

我有一个带有 ngram 分析器的复杂索引。我希望能够通过 Java API 创建一个新的索引。我目前为此使用 Kotlin,但使用相同的框架。我已经为此索引创建了架构:

 {
  "settings": {
    "index": {
      "max_ngram_diff": 20,
      "search.idle.after": "10m"
    },
    "analysis": {
      "analyzer": {
        "ngram3_analyzer": {
          "tokenizer": "ngram3_tokenizer",
          "filter": [
            "lowercase"
          ]
        }
      },
      "tokenizer": {
        "ngram3_tokenizer": {
          "type": "ngram",
          "min_gram": 3,
          "max_gram": 20
        }
      }
    }
  },
  "mappings": {
    "dynamic": "strict",
    "_doc": {
      "properties": {
        "name": {
          "type": "keyword",
          "fields": {
            "partial": {
              "type": "text",
              "analyzer": "ngram3_analyzer",
              "search_analyzer": "keyword"
            },
            "text": {
              "type": "text"
            }
          }
        },
        "location": {
          "type": "geo_shape",
          "ignore_malformed": true
        },
        "type": {
          "type": "keyword"
        },
        "sort": {
          "type": "integer"
        }
      }
    }
  }
}

此 json 架构在通过休息客户端 PUT 调用手动传递时有效。

    {
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "new_index_created"
    }

使用以下 koltin 函数通过弹性 java API 传递相同的模式:

private fun createIndex(index: String, schema: String) {
    val createIndexRequest = CreateIndexRequest(index).mapping(schema, XContentType.JSON)
    getClient().indices().create(createIndexRequest, RequestOptions.DEFAULT)
}

我收到这样的回复:

Elasticsearch exception [type=mapper_parsing_exception, reason=Failed to parse mapping [_doc]: Root mapping definition has unsupported parameters:  [settings : {index={max_ngram_diff=20, search.idle.after=10m}, analysis={analyzer={ngram3_analyzer={filter=[lowercase], tokenizer=ngram3_tokenizer}}, tokenizer={ngram3_tokenizer={min_gram=3, type=ngram, max_gram=20}}}}] [mappings : {_doc={properties={name={type=keyword, fields={text={type=text}, partial={search_analyzer=keyword, analyzer=ngram3_analyzer, type=text}}}, location={ignore_malformed=true, type=geo_shape}, sort={type=integer}, type={type=keyword}}}, dynamic=strict}]]

任何关于这个问题的帮助都会很棒:)

您收到的错误是因为您将 mappingssettings 都传递给了 mapping(...) 调用。

您可以仅使用 mappings 部分调用 mapping(),使用 settings 部分调用 setting(),也可以这样调用 source()

val createIndexRequest = CreateIndexRequest(index).source(schema, XContentType.JSON)
                                                      ^
                                                      |
                                                  change this