creating settings of index using elastic4s

杀马特。学长 韩版系。学妹 提交于 2020-02-06 08:01:11

问题


I have some 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"),
        ...
       )
    )

I'm creating an index with repository.createIndexIfNotExists() using this mapping.
Now I must create ngram_analyzer in my index settings:

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

How can I do that using elastic4s?


回答1:


OK. A lot of functions of createIndexIfNotExists() were deprecated. So, I used CreateIndexRequest where I put my analyzer:

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

And I initialized my analyzer like this:

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


来源:https://stackoverflow.com/questions/59984368/creating-settings-of-index-using-elastic4s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!