1

I've been using elastic4s for my data retrieval and have begun looking into testing for my current code. I am trying to create a local client that I can then use (and pass to my actors) that will be more manageable for testing than having to deploy an elastic cluster on a build server and keep it maintained.

Below is the code I've been trying so far. This should start a simple instance, create an index, add a video into the "videos" index, and then retrieve it.

val essettings = ImmutableSettings
.settingsBuilder().put("cluster.name", conf.getString("elasticsearch.cluster-name")).build()

ElasticsearchConnection.setInstance(ElasticClient.local(essettings))

val client = ElasticsearchConnection.getInstance()

client.execute( create index "videos" mappings(
 "video" as (
   "artist" typed StringType,
   "title" typed StringType
  )
 )
)

client.execute(
 bulk(
  index into "videos" id 1 fields ("title" -> "Worth It", "artist" -> "Fifth Harmony")
 )
).await

val resp = client.execute(
 search in "videos" types "video" limit 1 query bool {
  must (
    matchQuery("artist", "Fifth Harmony")
  )
 }
).await

However it is not working as I'd expect it to. It runs through without breaking on any of the lines, but no data is found in resp. I use the same type of queries with an external ES cluster, so I think there's something wrong with how I'm setting it up.

rebnat
  • 121
  • 9

1 Answers1

1

There are a number of helpers you can use in elastic4s that will allow you to create embedded instances - take a look at any of the units tests. But lets assume you want to do it all manually.

You don't say what version of elasticsearch but I'll assume 1.7.

The simple answer is you're not setting a type when you index. You have to specify one, even if its empty string, so when you do index into "videos" you're saying use videos index but empty string type.

So changing your index request to: index into "video" / "videos" id 1 fields ("title" -> "Worth It", "artist" -> "Fifth Harmony" fixes it.

If you're not familiar with indexes and types, this should help: https://www.elastic.co/guide/en/elasticsearch/guide/current/mapping.html

sksamuel
  • 16,154
  • 8
  • 60
  • 108