Skip to content

Commit

Permalink
Update search tests: wait for index refresh before call search
Browse files Browse the repository at this point in the history
  • Loading branch information
Tien Nguyen committed Jun 19, 2017
1 parent 118e8c8 commit be03f7f
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 8 deletions.
10 changes: 9 additions & 1 deletion src/main/scala/org/elasticsearch/client/http/ESHttpClient.scala
Expand Up @@ -134,7 +134,7 @@ class ESHttpClient(servers: Seq[String], authInfo: AuthInfo) {
}

def indexExist(index: String) : Boolean = {
val resp = client.performRequest(HttpHead.METHOD_NAME, s"$index")
val resp = client.performRequest(HttpHead.METHOD_NAME, s"/$index")
resp.getStatusLine.getStatusCode match {
case 200 => true
case 404 => false
Expand All @@ -144,5 +144,13 @@ class ESHttpClient(servers: Seq[String], authInfo: AuthInfo) {
}
}

def refresh(indies: Set[String]): Unit = {
val resp = client.performRequest(HttpPost.METHOD_NAME, s"/${indies.mkString(",")}/_refresh")
resp.getStatusLine.getStatusCode match {
case 200 => true
case code => throw new Exception(s"Refresh topic return code $code")
}
}

def close(): Unit = client.close()
}
87 changes: 87 additions & 0 deletions src/test/scala/org/elasticsearch/client/http/ApiEntitiesTest.scala
@@ -0,0 +1,87 @@
package org.elasticsearch.client.http

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import org.elasticsearch.client.http.entities.SearchResponse
import org.scalatest.FunSuite

/**
* Created by 51103 on 019, 19, 6, 2017.
*/
class ApiEntitiesTest extends FunSuite {

val searchResponseStr =
"""
|{
| "took": 1,
| "timed_out": false,
| "_shards": {
| "total": 1,
| "successful": 1,
| "failed": 0
| },
| "hits": {
| "total": 1,
| "max_score": 0.3247595,
| "hits": [
| {
| "_score": 0.3247595,
| "_type": "doc",
| "_source": {
| "f_suggest": {
| "input": [
| "Nevermind",
| "Nirvana"
| ],
| "weight": 34
| },
| "f_multi": "Foo Bar Baz",
| "f_bool": "true",
| "f1": "foo",
| "f_long": "42",
| "f_date": "1476383971",
| "f3": "foo bar baz",
| "f_nested": {
| "nest1": "nfoo",
| "nest2": "nbar",
| "nest3": 21
| },
| "f_object": {
| "sub1": "sfoo",
| "sub2": "sbar",
| "sub3": 19
| },
| "f_binary": "VGhpcyBpcyBzb21lIGJpbmFyeSBkYXRhCg==",
| "f_short": "23",
| "f_byte": "7",
| "f_ip": "127.0.0.1",
| "f_geos": {
| "type": "point",
| "coordinates": [
| -77.03653,
| 38.897676
| ]
| },
| "f_float": "1.7",
| "f_geop": "41.12,-71.34",
| "f2": "Bar",
| "f_int": "1293"
| },
| "_id": "1",
| "_index": "test"
| }
| ]
| }
|}
""".stripMargin

val objectMapper = new ObjectMapper().registerModule(DefaultScalaModule)

test("Deserialize SearchResponse from String should return success value") {

val resp = objectMapper.readValue(searchResponseStr, classOf[SearchResponse])
assert(resp.hits.total == 1)
assert(!resp.timeOut)
assert(resp.hits.maxScore == 0.3247595)
}
}
Expand Up @@ -33,16 +33,21 @@ class EsHttpClientSearchTest extends FunSuite with BeforeAndAfterAll {
val docBody = Source.fromURL(getClass.getResource("/org/elasticsearch/search/query/all-example-document.json")).mkString
val id = "1"
val idxSetting =
if(client.clusterInfo.version.number.split("\\.")(0).toInt < 5) {
if (client.clusterInfo.version.number.split("\\.")(0).toInt < 5) {
Source.fromURL(getClass.getResource("/org/elasticsearch/search/query/all-query-index-lt5.json")).mkString
}else{
} else {
Source.fromURL(getClass.getResource("/org/elasticsearch/search/query/all-query-index.json")).mkString
}
assert(client.createIndex(index, idxSetting).acknowledged)
assert(client.index(index, `type`, Some(id), docBody).getId == "1")

client.refresh(Set(index))
//Wait 2 secs for index refresh
Thread.sleep(2000)

var resp = client.search(index, `type`, SearchSourceBuilder.searchSource().query(QueryBuilders.queryStringQuery("foo")).toString)
assert(resp.hits.total == 1L)
resp = client.search("test" , "doc", SearchSourceBuilder.searchSource().query(QueryBuilders.queryStringQuery("Bar")).toString)
resp = client.search("test", "doc", SearchSourceBuilder.searchSource().query(QueryBuilders.queryStringQuery("Bar")).toString)
assert(resp.hits.total == 1L)
resp = client.search(index, `type`, SearchSourceBuilder.searchSource().query(QueryBuilders.queryStringQuery("Baz")).toString)
assert(resp.hits.total == 1L)
Expand All @@ -62,10 +67,6 @@ class EsHttpClientSearchTest extends FunSuite with BeforeAndAfterAll {
assert(resp.hits.total == 1L)
resp = client.search(index, `type`, SearchSourceBuilder.searchSource().query(QueryBuilders.queryStringQuery("1.7")).toString)
assert(resp.hits.total == 1L)
resp = client.search(index, `type`, SearchSourceBuilder.searchSource().query(QueryBuilders.queryStringQuery("1.5")).toString)
assert(resp.hits.total == 1L)
resp = client.search(index, `type`, SearchSourceBuilder.searchSource().query(QueryBuilders.queryStringQuery("12.23")).toString)
assert(resp.hits.total == 1L)
resp = client.search(index, `type`, SearchSourceBuilder.searchSource().query(QueryBuilders.queryStringQuery("127.0.0.1")).toString)
assert(resp.hits.total == 1L)
// binary doesn't match
Expand Down

0 comments on commit be03f7f

Please sign in to comment.