1

我正在玩和学习用于弹性搜索的 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" : [ {
4

2 回答 2

3

@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例如

client execute { 
  search in "_all" query "findme" 
}
于 2015-03-18T22:26:52.863 回答
1
val client = ElasticClient.local

这一行意味着elastic4s它将创建它自己的数据存储,所以如果你没有将你的数据索引到这个地方,你的搜索结果就什么都不是。如果你想连接你自己的Elasticsearch,你可以:

  //Set the Cluster name
  val settings = ImmutableSettings.settingsBuilder().put("cluster.name", "elasticsearch").build()

  //Set the Cluster Connection
  val client = ElasticClient.remote(settings, ("127.0.0.1", 9300))

并执行搜索操作。

于 2015-03-18T04:56:11.580 回答