我正在为 elasticsearch 使用和学习 scala 和 elastic4s。
我有一个使用官方 elasticsearch 模块的 python 脚本。我在 python 中的代码如下所示:
res=helpers.scan(es, query={"_source": ["http_uri", "header_user_agent"],"query": {"query_string": {"query": "message:key"}}}, index="")
我上面的 python 代码有效。我得到 90 万个结果并处理它们等等。
我正在使用基本的 scala 代码来试用 e4s。这只是一个简单的查询。我的查询有误吗?
import com.sksamuel.elastic4s.ElasticClient
import com.sksamuel.elastic4s.ElasticDsl._
object Banana {
def main(args: Array[String]) {
val client = ElasticClient.local
val res = client execute { search in "*" query "apiKey" } await
println(res.getHits.totalHits)
println(res)
}
}
我的运行结果:
info] Running Banana
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
0
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 0,
"successful" : 0,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : 0.0,
"hits" : [ ]
}
}
以及带有 curl 查询的响应:
$ curl -s 'localhost:9200/_search?q=apiKey&pretty=true' | head -12
{
"took" : 21,
"timed_out" : false,
"_shards" : {
"total" : 1200,
"successful" : 1200,
"failed" : 0
},
"hits" : {
"total" : 756253,
"max_score" : 1.5905869,
"hits" : [ {
最佳答案
@chengpohi 是正确的,当您使用 val client = ElasticClient.local
时,您正在创建一个本地节点而不是连接到您的集群,因此您需要使用
val uri = ElasticsearchClientUri("elasticsearch://127.0.0.1:9300")
val settings = ImmutableSettings.settingsBuilder().put("cluster.name", "myClusterName").build()
val client = ElasticClient.remote(settings, uri)
但除此之外,您正在搜索名为 *
的索引,这并不像您认为的那样意味着所有索引。如果你想在所有索引中搜索,那么你需要使用_all
eg
client execute {
search in "_all" query "findme"
}
关于python - 使用 elastic4s 查询产生零结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29110519/