Search code examples
javakotlinelasticsearchelasticsearch-api

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


I have a complex index with a ngram analyzer. I want to be able to create a new index through the Java API. I am currently using Kotlin for this but using the same framework. I have created the schema for this index as so:

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

This json schema works when manually passing it via a rest client PUT call.

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

Passing the same schema via elastic java API using the following koltin function:

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

I get this response:

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

any help on this issue would be great :)


Solution

  • The error you get is because you're passing both mappings and settings into the mapping(...) call.

    You can either call mapping() with only the mappings section and setting() with the settings section, or you can call source() like this:

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