使用 elastic4s 创建索引设置

creating settings of index using elastic4s

我有一些 class:

class SomeClass(val client: ElasticClient, val config: Config, val configName: String)(implicit val ec: ExecutionContext)
  extends ElasticSearchRepositoryWrapper[AnotherClass]{
    override def mapping: Option[MappingDefinition] = Some(
      properties(
        KeywordField("id"),
        TextField("name").fielddata(true).analyzer("ngram_analyzer"),
        KeywordField("lang"),
        BasicField("order", "long"),
        ...
       )
    )

我正在使用此映射 repository.createIndexIfNotExists() 创建索引。
现在我必须在我的索引设置中创建 ngram_analyzer

  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "ngram_analyzer": {
            "filter": [
              "lowercase"
            ],
            "tokenizer": "my_tokenizer"
          }
        },
        "tokenizer": {
          "my_tokenizer": {
            "token_chars": [
              "letter",
              "digit"
            ],
            "min_gram": "3",
            "type": "ngram",
            "max_gram": "3"
          }
        }
      }
    }

如何使用 elastic4s 做到这一点?

好的。 createIndexIfNotExists() 的许多功能已被弃用。所以,我在放置分析器的地方使用了 CreateIndexRequest

CreateIndexRequest(repository.indexName, analysis = Option(ngramAnalyzer), mapping = repository.mapping)
      .shards(repository.shards)
      .replicas(repository.replicas)

然后我像这样初始化了我的分析器:

val ngramAnalyzer = Analysis(
    List(CustomAnalyzer(
      name = "ngram_analyzer",
      tokenizer = "ngram",
      charFilters = Nil,
      tokenFilters = List("lowercase")
    ))
  )