嵌入式 ES 实例中的补全搜索

Completion search in Embedded ES instance

我正在尝试在嵌入式实例中完成搜索以进行单元测试。我正在使用 elastic4s 和 elasticsearch 2.0.x

我的工作代码如下:

val nIndex = create index "musicbrainz" shards 5 replicas 1 mappings(
  "artist_title" as(
    "artist" typed StringType,
    "artist_suggest" typed CompletionType analyzer "simple" searchAnalyzer "simple" payloads false preserveSeparators true preservePositionIncrements true maxInputLen 50,

    "title" typed StringType
  ) size true numericDetection true boostNullValue 1.2 boost "myboost"
)

client.execute(nIndex)

val bulkInsert = bulk(
  index into "artist_title"/"musicbrainz" id 1 fields("title" -> "Just the Way You Are", "artist" -> "Bruno Mars", "artist_suggest" -> Map("input" -> Seq("bruno", "mars"), "output" -> Seq("Bruno Mars"))) refresh true
)

client.execute(
  bulkInsert
).await

val res = client.execute {
  search in "artist_title"/"musicbrainz" limit 1 query bool {
    must (
      matchQuery("artist", "Bruno Mars")
    )
  }
}.await

val artistSugg = completionSuggestion.field("artist_suggest").text("b")
val resp = client.execute {
  search in "artist_title"/"musicbrainz" suggestions  {
    artistSugg
  }
}.await
val it = resp.suggestion(artistSugg).entry("bruno").options.map(x => x.text).distinct

点击最后一行时,代码将中断并显示消息 Field [artist_suggest] is not a completion suggest field。但是,如果我查看 nIndex._mappings(0)._fields,它包含一个 CompletionFieldDefinition。 MatchQuery returns 数据,所以我知道它已正确添加,我似乎无法弄清楚如何让完成建议器工作。从我在 scala 中得到的信息来看,它似乎与文档相符:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html

再次阅读文档,我发现我漏掉了一些东西。完成建议器搜索索引中的所有文档,所以当我将 search in "artist_title"/"musicbrainz" suggestions 更改为 search in "musicbrainz" suggestions 时它起作用了。