MIkCode
MIkCode

Reputation: 2845

elastic4s and play framework 2.2.1

i'm trying to index some data to elastic search by using the elastic4s API and play framework

I'm basically calling this method from the controller

 def test(){
 val client = ElasticClient.local
 client.execute { create index "bands" }
 client execute { index into "bands/singers" fields "name"->"chris martin" }
 client.close()

 }

I didn't get any error on play or in the elastic search log,

Then i checked with the Sense plugin if the data is indexed and i got

  {
   "error": "IndexMissingException[[bands] missing]",
  "status": 404
   }

It looks like the query didn't go to the server?? ...

Upvotes: 2

Views: 1083

Answers (3)

sksamuel
sksamuel

Reputation: 16387

It's because the create index is not synchronous so you're trying to index before the create index has completed.

The easiest way is to make the create index synchronous by calling

client.sync.execute { create index "bands" }

Which will block until the index is created, which should be < 1 second. Or you can work off the future returned.

Edit: In elastic4s 1.3, sync has been replaced with a .await helper on futures.

client.execute( create index "bands" ).await

Upvotes: 6

Ashalynd
Ashalynd

Reputation: 12573

If using Java API isn't a requirement, you can also try the REST Scala client for ElasticSearch: https://github.com/gphat/wabisabi. It is more transparent, but you'll have to add up some harness to create Json requests. Play Json library + a couple of simple helpers is in principle all that is needed there.

Upvotes: 0

dan.rising
dan.rising

Reputation: 51

It's hard to tell for sure, but it could be that the execute calls actually are throwing exceptions that you aren't seeing because the Client.execute method returns a Future[Res] instead of blocking and returning a Res directly.

And forgive me if I'm wrong about this, but since it sounds like you're just familiarizing yourself with how this lib works, I'd suggest using an onComplete callback (or some other strategy on the Scala futures page) to see what's going on when the Future is actually completed.

Upvotes: 1

Related Questions