scala - Getting zero results in search using elastic4s -
this small code using simple search:
import com.sksamuel.elastic4s.{elasticsearchclienturi, elasticclient} import com.sksamuel.elastic4s.elasticdsl._ import org.elasticsearch.common.settings.immutablesettings object main3 extends app { val uri = elasticsearchclienturi("elasticsearch://localhost:9300") val settings = immutablesettings.settingsbuilder().put("cluster.name", "elasticsearch").build() val client = elasticclient.remote(settings, uri) if (client.exists("bands").await.isexists()) { println("index exists!") val num = readline("want delete index? ") if (num == "y") { client.execute {deleteindex("bands")}.await } else { println("leaving here ...") } } else { println("creating index!") client.execute(create index "bands").await client.execute(index "bands/artists" fields "name"->"coldplay").await val resp = client.execute(search in "bands/artists" query "coldplay").await println(resp) } client.close() }
this result get:
connected target vm, address: '127.0.0.1:51872', transport: 'socket' log4j:warn no appenders found logger (org.elasticsearch.plugins). log4j:warn please initialize log4j system properly. log4j:warn see http://logging.apache.org/log4j/1.2/faq.html#noconfig more info. creating index! { "took" : 1, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 0, "max_score" : null, "hits" : [ ] } } disconnected target vm, address: '127.0.0.1:51872', transport: 'socket' process finished exit code 0
creation of index , adding document index running fine simple search query giving no result. checked on sense.
get bands/artists/_search { "query": { "match": { "name": "coldplay" } } }
gives
{ "took": 4, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 1, "max_score": 0.30685282, "hits": [ { "_index": "bands", "_type": "artists", "_id": "au21oyo9w-qzq8hmdtol", "_score": 0.30685282, "_source": { "name": "coldplay" } } ] } }
how solve issue?
i suspect happening doing search straight after index operation in code. in elasticsearch documents not ready search immediately. see refresh interval setting here. (so when use rest client, waiting few seconds virtue of fact have manually flick between tabs, etc).
you test putting thread.sleep(3000) after index. if confirms works, need think how want write program.
normally index, , when data available, available. called eventual consistency. in meantime (seconds) users might not have available search. that's not problem.
if problem, have tricks in unit tests of elastic4s keep 'count'ing until right number of documents.
finally, can manually 'refresh' index speed things up, calling
client.execute { refresh index "indexname" }
but that's used when turn off automatic refreshing bulk inserts.
Comments
Post a Comment