python - 使用 elastic4s 查询产生零结果

标签 python scala elasticsearch elastic4s

我正在为 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/

相关文章:

python - Modal Bootstrap - 源代码与 Chrome 呈现的信息不同

scala - List 是怎样的一个单子(monad)?

java - 异步库的优点

scala - 是否有任何Scala功能允许您调用名称存储在字符串中的方法?

elasticsearch - 数组中的术语过滤器只考虑最后一项?

python - 来自导入 : GObject 的 undefined variable

python - 需要一些基本的 Pandas 帮助——尝试逐行打印数据框并对该行的特定列中的元素执行操作

python - 如何将二维数组复制到第三维,N次?

ios - 使用新的 Firebase 在 Heroku 上为 ElasticSearch 设置手电筒

postgresql - Elasticsearch使用Node.js添加数据