gpt4 book ai didi

elasticsearch - 在 ElasticSearch 上使用 elastic4s 查询嵌套字段

转载 作者:行者123 更新时间:2023-11-29 02:55:15 24 4
gpt4 key购买 nike

我想查询在 ES 中索引的嵌套文档。

例如,嵌套字段是user,它包含两个字段idname。我想查询名称与字段 user.name 完全匹配的所有文档。

无法弄清楚如何为此使用 elastic4s DSL。

最佳答案

这是在 elastic4s 中进行嵌套查询的方式:

首先,设置索引,这样你就有了一个嵌套类型:

  client.execute {
create index "nested" mappings {
"show" as {
"actor" typed NestedType
}
}
}

然后是一些示例数据

  client.execute(
index into "nested/show" fields(
"name" -> "game of thrones",
"actor" -> Seq(
Map("name" -> "peter dinklage", "birthplace" -> "Morristown"),
Map("name" -> "pedro pascal", "birthplace" -> "Santiago")
)
)
)

接下来是关键部分。要进行搜索,您可以使用 nested(或在 elastic4s 1.4 beta 中,nestedQuery)“放入”嵌套范围,您可以在其中使用任何标准查询类型进行搜索。在这里,我只是使用了一个简单的 termQuery 搜索。

client.execute {
search in "nested/show" query nested("actor").query(termQuery("actor.name" -> "dinklage"))
}

关于elasticsearch - 在 ElasticSearch 上使用 elastic4s 查询嵌套字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26620788/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com