I am trying to update several document fields and return full document after update. I use elastic4s 1.3.4, elasticsearch 1.4.3 (as server).
Here is a code:
import scala.concurrent.ExecutionContext.Implicits.global
object ElasticsearchTester extends App {
private val settings: Settings = ImmutableSettings.settingsBuilder().put("cluster.name", "clustername").build()
private val client: ElasticClient = ElasticClient.remote(settings, ("localhost", 9300))
val initial = """
|{
| "name":"jojn",
| "surname":"olol"
|}
""".stripMargin
val updateString = """
|{
| "surname":"123",
| "global": {
| "new":"fiedl"
| }
|}
""".stripMargin
import com.sksamuel.elastic4s.ElasticDsl._
val future = client.execute {
create index "my_index"
}.flatMap { r=>
client.execute {
index into "my_index/user" doc StringDocumentSource(initial)
}.flatMap { re=>
println("Ololo indexed is: " + initial)
println("Ololo indexed id: " + re.getId)
client.execute {
update id re.getId in "my_index/user" doc StringDocumentSource(updateString) docAsUpsert true params ("fields" -> "_source")
}.map{res=>
println("Ololo result is: " + res.getGetResult.sourceAsString())
}
}
}
Await.result (future, 20.seconds)
println("Ololo ok")
}
Why I get NullPointerException
in line res.getGetResult.sourceAsString()
? It seems that update response do not contains a document after update operation. Is it possible to return document _source
from update response?
Elastic4s seem has no api in UpdateDefinition (for now 23.07.2015) to set fields. However it's builder support this operation, the code below is like dirty hack, but it works as exepcted, just set fields directly into _builder
:
val updateRequest = update id re.getId in "my_index/user" doc StringDocumentSource(updateString) docAsUpsert true
updateRequest._builder.setFields("_source")
client.execute {
updateRequest
}.map { res=>
println("Ololo result is: " + res.getGetResult.sourceAsString())
}
}
prints
Ololo indexed id: AU66n1yiYVxOgU2h4AoG
Ololo result is: {"name":"jojn","surname":"123","global":{"new":"fiedl"}}
Ololo ok
Note
Elasticsearch does support returning fields after update request.
Updated
Elastic4s after this commit support this via UpdateDsl.includeSource
or UpdateDsl.setFields
methods.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.