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

4

3 回答 3

6

这是因为创建索引不是同步的,所以您尝试在创建索引完成之前进行索引。

最简单的方法是通过调用使创建索引同步

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

它将阻塞直到创建索引,这应该是 < 1 秒。或者你可以在未来返回。

编辑:在 elastic4s 1.3 中,同步已被替换为期货上的 .await 助手。

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

于 2014-02-20T22:04:34.510 回答
1

很难确定,但可能是执行调用实际上引发了您没有看到的异常,因为Client .execute 方法返回 Future[Res] 而不是阻塞并直接返回 Res。

如果我对此有误,请原谅我,但因为听起来你只是熟悉这个库的工作原理,我建议使用 onComplete 回调(或Scala 期货页面上的其他策略)来看看是什么在 Future 实际完成时继续。

于 2014-02-16T02:51:20.853 回答
0

如果不需要使用 Java API,您还可以尝试 ElasticSearch 的 REST Scala 客户端:https ://github.com/gphat/wabisabi 。它更加透明,但是您必须添加一些工具来创建 Json 请求。Play Json 库 + 几个简单的助手原则上就是那里所需要的。

于 2014-02-17T22:48:06.537 回答