rebnat
rebnat

Reputation: 131

Completion search in Embedded ES instance

I am trying to get completion searching working in an embedded instance for unit testing. I am using elastic4s and elasticsearch 2.0.x

My working code is as follows:

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

On hitting the last line, the code will break with the message Field [artist_suggest] is not a completion suggest field. However if I look at nIndex._mappings(0)._fields it contains a CompletionFieldDefinition. The MatchQuery returns the piece of data, so I know it is getting added correctly, I just can't seem to figure out how to get the completion suggester working. From what I have in the scala it seems like it matches up to the doc: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html

Upvotes: 1

Views: 109

Answers (1)

rebnat
rebnat

Reputation: 131

Reading through the docs again, I realized I missed something. The completion suggester searches all documents in an index, so when I changed search in "artist_title"/"musicbrainz" suggestions to search in "musicbrainz" suggestions it works.

Upvotes: 2

Related Questions