Reputation: 537
I'm playing with and learning scala and elastic4s for elasticsearch.
I have a python script that uses the official elasticsearch module. My code in python looks like this:
res=helpers.scan(es, query={"_source": ["http_uri", "header_user_agent"],"query": {"query_string": {"query": "message:key"}}}, index="")
My above python code works. I get 900k results and I process them and so on.
I'm using the basic scala code to try out e4s. This is just a simple query. Is my query wrong?
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)
}
}
My result on running this:
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" : [ ]
}
}
And the response with a curl query:
$ 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" : [ {
Upvotes: 1
Views: 1412
Reputation: 16387
@chengpohi is correct that you are creating a local node and not connecting to your cluster when you use val client = ElasticClient.local
, so you need to use
val uri = ElasticsearchClientUri("elasticsearch://127.0.0.1:9300")
val settings = ImmutableSettings.settingsBuilder().put("cluster.name", "myClusterName").build()
val client = ElasticClient.remote(settings, uri)
But in addition you are searching in an index called *
which does not mean all indexes as you think it does. If you want to search in all indexes, then you need to use _all
eg
client execute {
search in "_all" query "findme"
}
Upvotes: 3
Reputation: 14217
val client = ElasticClient.local
this line means that elastic4s
will create it's own data store, so if you not index your data into this place, your search result is nothing. if you want to connect your own Elasticsearch
, you can:
//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))
and do the search action.
Upvotes: 1