return elasticsearch 更新后如何获取文档?
How return document from elasticsearch after update?
我试图在更新后更新几个文档字段和 return 完整文档。
我使用 elastic4s 1.3.4, elasticsearch 1.4.3(作为服务器)。
这是一个代码:
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")
}
为什么我在行 res.getGetResult.sourceAsString()
中得到 NullPointerException
?似乎更新响应不包含更新操作后的文档。
是否可以从更新响应中 return 记录 _source
?
Elastic4s 似乎没有 api in UpdateDefinition(现在是 23.07.2015)来设置字段。然而它的构建器支持这个操作,下面的代码就像肮脏的黑客,但它按预期工作,只需将字段直接设置为 _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())
}
}
打印
Ololo indexed id: AU66n1yiYVxOgU2h4AoG
Ololo result is: {"name":"jojn","surname":"123","global":{"new":"fiedl"}}
Ololo ok
备注
Elasticsearch support 在更新请求后返回字段。
已更新
Elastic4s 在 this 提交后通过 UpdateDsl.includeSource
或 UpdateDsl.setFields
方法支持此功能。
我试图在更新后更新几个文档字段和 return 完整文档。 我使用 elastic4s 1.3.4, elasticsearch 1.4.3(作为服务器)。
这是一个代码:
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")
}
为什么我在行 res.getGetResult.sourceAsString()
中得到 NullPointerException
?似乎更新响应不包含更新操作后的文档。
是否可以从更新响应中 return 记录 _source
?
Elastic4s 似乎没有 api in UpdateDefinition(现在是 23.07.2015)来设置字段。然而它的构建器支持这个操作,下面的代码就像肮脏的黑客,但它按预期工作,只需将字段直接设置为 _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())
}
}
打印
Ololo indexed id: AU66n1yiYVxOgU2h4AoG
Ololo result is: {"name":"jojn","surname":"123","global":{"new":"fiedl"}}
Ololo ok
备注
Elasticsearch support 在更新请求后返回字段。
已更新
Elastic4s 在 this 提交后通过 UpdateDsl.includeSource
或 UpdateDsl.setFields
方法支持此功能。