Elastic4s 5.x中的多字段

时间:2017-03-02 17:21:31

标签: scala elasticsearch elastic4s

我目前正在使用Elastic4s v5.0,它仍然具有多字段类型,用于以多种方式索引字段。

elasticClient.execute(createIndex("foo") mappings (
  mapping("bar").as(
    multiField("baz").as(
      textField("baz") analyzer myAnalyzer,
      textField("original") index NotAnalyzed
    )
  )
)

但是,我收到以下错误:

No handler for type [multi_field] declared on field []

答案ElasticSearch 5: MapperParserException with multi_field和此处的文档https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-fields.html表示使用“字段”代替,但我找不到如何在elastic4s中执行此操作。

1 个答案:

答案 0 :(得分:2)

In Elasticsearch any multifield has a primary field which is kind of like a parent field and then it has secondary fields. The primary field (primary and secondary is my terminology by the way), is accessed with a and the secondary fields are accessed as a.b, a.c and so on.

This might not be how you would first imagine a multi field to be, because you might just think that there's a, b, c as siblings like a kind of sequence. So its worth understanding this.

In elastic4s, you can just use .fields on any field you want, and then those fields will be combined with the parent to become a multi field. So your example re-written would be.

  client.execute {
    createIndex("foo").mappings(
      mapping("bar").fields(
        textField("baz").fields(
          textField("inner1") analyzer PatternAnalyzer,
          textField("inner2") index NotAnalyzed
        )
      )
    )
  }

Note that as is an alias for fields and I think fields is more readable so I used it here.