2

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?? ...

Jack Bonneman
  • 1,821
  • 18
  • 24
MIkCode
  • 2,655
  • 5
  • 28
  • 46
  • can u please suggest me some good tutorial on elastic search using scala client with play framework ....please it will be very help full for me – swaheed Nov 29 '14 at 10:50

3 Answers3

6

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

sksamuel
  • 16,154
  • 8
  • 60
  • 108
1

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.

dan.rising
  • 51
  • 2
  • My answer is actually more likely to be correct. The create and index will execute immediately in his code sample, whereas the create will take around 50ms to take effect in the database. – sksamuel Apr 01 '14 at 18:58
0

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.

Ashalynd
  • 12,363
  • 2
  • 34
  • 37