Skip to content

Commit

Permalink
Merge pull request Philippus#90 from sksamuel/feature/source-and-fiel…
Browse files Browse the repository at this point in the history
…ds-in-get-api

Add fetchSourceContext and fields definitions to GetDsl
  • Loading branch information
sksamuel committed Feb 13, 2014
2 parents e962d74 + 74a3329 commit f1b65d5
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 5 deletions.
16 changes: 16 additions & 0 deletions src/main/scala/com/sksamuel/elastic4s/GetDsl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.sksamuel.elastic4s

import org.elasticsearch.client.Requests
import org.elasticsearch.action.get._
import org.elasticsearch.search.fetch.source.FetchSourceContext

/** @author Stephen Samuel */
trait GetDsl extends IndexesTypesDsl {
Expand Down Expand Up @@ -34,6 +35,21 @@ case class GetDefinition(indexesTypes: IndexesTypes, id: String)
this
}

def fields(fields: String *) = {
_builder.fields(fields: _*)
this
}

def fetchSourceContext(context: Boolean) = {
_builder.fetchSourceContext(new FetchSourceContext(context))
this
}

def fetchSourceContext(context: FetchSourceContext) = {
_builder.fetchSourceContext(context)
this
}

def preference(pref: Preference): GetDefinition = preference(pref.elastic)
def preference(pref: String): GetDefinition = {
_builder.preference(pref)
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/com/sksamuel/elastic4s/MultiGetDsl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ class MultiGetDefinition(gets: Iterable[GetDefinition])
_builder.setRealtime(realtime)
this
}
}
}
27 changes: 25 additions & 2 deletions src/test/scala/com/sksamuel/elastic4s/GetDslTest.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.sksamuel.elastic4s

import org.scalatest.FlatSpec
import org.scalatest.mock.MockitoSugar
import org.scalatest.Matchers
import ElasticDsl._


/** @author Stephen Samuel */
class GetDslTest extends FlatSpec with MockitoSugar with ElasticSugar {
class GetDslTest extends FlatSpec with Matchers with ElasticSugar {

"a get by id request" should "accept tuple for from" in {
val req = get id 123 from "places" -> "cities"
Expand All @@ -24,4 +25,26 @@ class GetDslTest extends FlatSpec with MockitoSugar with ElasticSugar {
assert(req.build.index() === "places")
assert(req.build.`type`() === "cities")
}

it should "accept one field" in {
val req = get id 123 from "places/cities" fields("name")
assert(req.build.index() === "places")
assert(req.build.`type`() === "cities")
req.build.fields() should equal(Array("name"))
}

it should "accept multiple fields" in {
val req = get id 123 from "places/cities" fields("name", "title", "content")
assert(req.build.index() === "places")
assert(req.build.`type`() === "cities")
req.build.fields() should equal(Array("name", "title", "content"))
}

it should "disable fetchSource" in {
val req = get id 123 from "places/cities" fetchSourceContext(false)
assert(req.build.index() === "places")
assert(req.build.`type`() === "cities")
req.build.fields() should be (null)
req.build.fetchSourceContext().fetchSource should be (false)
}
}
86 changes: 84 additions & 2 deletions src/test/scala/com/sksamuel/elastic4s/GetTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import org.scalatest.FlatSpec
import org.scalatest.mock.MockitoSugar
import ElasticDsl._
import org.elasticsearch.common.Priority
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.Matchers

/** @author Stephen Samuel */
class GetTest extends FlatSpec with MockitoSugar with ElasticSugar {
class GetTest extends FlatSpec with Matchers with ScalaFutures with MockitoSugar with ElasticSugar {

client.execute {
index into "beer/lager" fields(
Expand All @@ -28,11 +30,91 @@ class GetTest extends FlatSpec with MockitoSugar with ElasticSugar {

client.admin.cluster.prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet

"a get request" should "retrieve a document by id" in {
"A Get request" should "retrieve a document by id" in {

val resp = client.sync.execute {
get id 8 from "beer/lager"
}
assert("8" === resp.getId)
}

it should "retrieve a document asynchronously by id" in {

val resp = client.execute {
get id 8 from "beer/lager"
}

whenReady(resp) { result =>
result.isExists should be (true)
result.getId shouldBe "8"
}
}

it should "retrieve a document asynchronously by id w/ source" in {

val resp = client.execute {
get id 8 from "beer/lager"
}

whenReady(resp) { result =>
result.isExists should be (true)
result.getId shouldBe "8"
result.getSource should not be (null)
result.getFields should have size 0
}
}

it should "retrieve a document asynchronously by id w/o source" in {

val resp = client.execute {
get id 8 from "beer/lager" fetchSourceContext false
}

whenReady(resp) { result =>
result.isExists should be (true)
result.getId shouldBe "8"
result.getSource should be (null)
result.getFields should have size 0
}
}

it should "retrieve a document asynchronously by id w/ name and w/o source" in {

val resp = client.execute {
get id 8 from "beer/lager" fields("name")
}

whenReady(resp) { result =>
result.isExists should be (true)
result.getId shouldBe "8"
result.getSource should be (null)
result.getFields should (contain key ("name") and not contain key ("brand"))
}
}

it should "retrieve a document asynchronously by id w/ name and brand and source" in {

val resp = client.execute {
get id 4 from "beer/lager" fields("name") fetchSourceContext true
}

whenReady(resp) { result =>
result.isExists should be (true)
result.getId shouldBe "4"
result.getSource should not be (null)
result.getFields should (contain key ("name") and not contain key ("brand"))
}
}

it should "not retrieve any documents w/ unknown id" in {

val resp = client.execute {
get id 1 from "beer/lager" fields("name") fetchSourceContext true
}

whenReady(resp) { result =>
result.isExists should be (false)
}
}

}

0 comments on commit f1b65d5

Please sign in to comment.