【发布时间】:2019-11-22 01:15:51
【问题描述】:
我使用的是 Scala 2.12,而我们使用的是 Elasticsearch 5.2.2。 我的要求是仅根据条件进行获取/搜索。搜索将一次性返回 10,000 多个文档或消息。所以我不能使用常规搜索。 数据(每个文档/消息)是一个复杂的 JSON,我可以稍后对其进行解析。 因此,我需要获取所有这些消息并将其存储在 Json 或任何内容的单个列表中。 我对 Scala 不是很流利。我可以在 Scala 中使用 Elastic4s 进行搜索。我看到它有滚动和扫描选项,但没有找到任何完整的工作示例。所以寻求一些帮助。
我看到一些示例代码如下,但需要更多帮助来获取所有内容并将所有内容放在上面:
client.execute {
search in "index" / "type" query <yourquery> scroll "1m"
}
client.execute {
search scroll <id>
}
但是如何获取滚动 id 以及如何继续获取所有数据?
更新:
上面提到了scala版本和ES版本。
我正在使用以下示例:
SBT:
libraryDependencies += "com.sksamuel.elastic4s" %% "elastic4s-core" % "7.0.2"
libraryDependencies += "com.sksamuel.elastic4s" %% "elastic4s-http" % "5.5.10"
libraryDependencies += "com.sksamuel.elastic4s" %% "elastic4s-http-streams" % "6.5.1"
libraryDependencies += "org.elasticsearch" % "elasticsearch" % "5.6.0"
代码:
import com.sksamuel.elastic4s.ElasticsearchClientUri
import com.sksamuel.elastic4s.requests.common.RefreshPolicy
import com.sksamuel.elastic4s.http.{ElasticClient, ElasticProperties}
import com.sksamuel.elastic4s.http.Response
import com.sksamuel.elastic4s.http.search.SearchResponse
import com.sksamuel.elastic4s.HttpClient
import com.sksamuel.elastic4s.http.ElasticDsl._
val client = HttpClient(ElasticsearchClientUri("host", 9200))
val resp1 = client.execute {
search("index")
.matchQuery("key", "value")
.scroll("1m")
.limit(500)
}.await.result
val resp2 = client.execute {
searchScroll(resp1.scrollId.get).keepAlive(1.minute)
}.await
我认为我没有为 elastic4s 模块使用正确的版本。
问题:
import com.sksamuel.elastic4s.HttpClient:无法识别 HttpClient 类。当我尝试初始化“客户端”变量时,它显示错误 HttpClient not found。
接下来,在我的 resp2 中,当我试图获取“scrollId”时,它无法识别。如何从 resp1 中获取 scrollId?
基本上,这里缺少什么?
更新 2:
我根据 github 上的示例(示例)更改了以下依赖项的版本
libraryDependencies += "com.sksamuel.elastic4s" %% "elastic4s-http" % "6.3.3"
代码:
val client = ElasticClient(ElasticProperties("http://host:9200"))
现在,我收到以下错误;
错误:
Symbol 'type <none>.term.BuildableTermsQuery' is missing from the classpath.
[error] This symbol is required by 'method com.sksamuel.elastic4s.http.search.SearchHandlers.BuildableTermsNoOp'.
[error] Make sure that type BuildableTermsQuery is in your classpath and check for conflicting dependencies with `-Ylog-classpath`.
[error] A full rebuild may help if 'SearchHandlers.class' was compiled against an incompatible version of <none>.term.
[error] val client = ElasticClient(ElasticProperties("host:9200"))
[error] ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
【问题讨论】:
-
你看过 elastic4s 中的scroll tests 吗?另一种解决方案是使用search_after,它不需要使用特定的端点并保留滚动ID。
-
我正在更新我的问题,提供更多细节。
标签: scala elasticsearch elastic4s