使用elastic4s查询产生零结果

时间:2015-03-17 21:58:51

标签: python scala elasticsearch elastic4s

我正在玩弹性搜索并学习scala和elastic4s。

我有一个使用官方elasticsearch模块的python脚本。我在python中的代码如下所示:

res=helpers.scan(es, query={"_source": ["http_uri", "header_user_agent"],"query": {"query_string": {"query": "message:key"}}}, index="")

我上面的python代码有效。我得到900k的结果,我处理它们等等。

我使用基本的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 -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" : [ {

2 个答案:

答案 0 :(得分: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" 
}

答案 1 :(得分: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))

并执行搜索操作。