# Using Elastic Search in unit and integration testing

**URL:** <https://discuss.elastic.co/t/using-elastic-search-in-unit-and-integration-testing/3018>\
**Category:** Elasticsearch\
**Created:** [June 14, 2010, 5:23pm UTC](https://discuss.elastic.co/t/using-elastic-search-in-unit-and-integration-testing/3018 "2010-06-14T17:23:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Samuel\_Doyle](https://avatars.discourse-cdn.com/v4/letter/s/e19adc/32.png) [@Samuel\_Doyle](https://discuss.elastic.co/u/Samuel_Doyle)\
**Post date:** [June 14, 2010, 5:23pm UTC](https://discuss.elastic.co/t/using-elastic-search-in-unit-and-integration-testing/3018/1 "2010-06-14T17:23:19Z")

</div>

Hello,  
I am trying to perform some unit testing in grails and doing a simple test I  
appear to be able to index but a following search will find nothing. Here is  
an example:

// Setup the test node and client  
@Before  
public void init() {  
assertNotNull dataSource  
GNodeBuilder nodeBuilder = nodeBuilder()  
nodeBuilder.settings {  
node {  
local = true  
}  
cluster {  
name = "Integration Test"  
}  
}  
testNode = nodeBuilder.node()  
testClient = testNode.client  
assertNotNull testNode  
assertNotNull testClient  
}

// Perform a simple index followed by a simple search  
@Test  
void simpleTest() {

```
def indexF = testClient.index {
      index "inventory"
      type "aType"
      id "1"
      source {
        code = "somecode"
      }
    }
println "Indexed id ${indexF.response.id}"

def searchF = testClient.search {
  indices "inventory"
  types "aType"
  source {
    query {
        term(code: "somecode")
    }
  }
}

// This should get a hit but I receive nothing.
println "Search Result"
searchF.response.hits.each {SearchHit hit ->
  println "Got hit $hit.id from ${hit.index}/${hit.type}"
}

```

}

---

<div class="post-metadata">

**Author:** ![Paul\_Loy](https://avatars.discourse-cdn.com/v4/letter/p/ad7895/32.png) [@Paul\_Loy](https://discuss.elastic.co/u/Paul_Loy)\
**Post date:** [June 14, 2010, 6:52pm UTC](https://discuss.elastic.co/t/using-elastic-search-in-unit-and-integration-testing/3018/2 "2010-06-14T18:52:06Z")

</div>

you'll need to refresh the index before searching. Something like this in  
Java:

testClient.admin().indices().refresh("inventory");

On Mon, Jun 14, 2010 at 6:23 PM, Samuel Doyle [samueldoyle@gmail.com](mailto:samueldoyle@gmail.com) wrote:

> Hello,  
> I am trying to perform some unit testing in grails and doing a simple test  
> I appear to be able to index but a following search will find nothing. Here  
> is an example:
> 
> // Setup the test node and client  
> @Before  
> public void init() {  
> assertNotNull dataSource  
> GNodeBuilder nodeBuilder = nodeBuilder()  
> nodeBuilder.settings {  
> node {  
> local = true  
> }  
> cluster {  
> name = "Integration Test"  
> }  
> }  
> testNode = nodeBuilder.node()  
> testClient = testNode.client  
> assertNotNull testNode  
> assertNotNull testClient  
> }
> 
> // Perform a simple index followed by a simple search  
> @Test  
> void simpleTest() {
> 
> ```
> def indexF = testClient.index {
> index "inventory"
> type "aType"
> id "1"
> source {
> code = "somecode"
> }
> }
> println "Indexed id ${indexF.response.id}"
> 
> def searchF = testClient.search {
> indices "inventory"
> types "aType"
> source {
> query {
> term(code: "somecode")
> }
> }
> }
> 
> // This should get a hit but I receive nothing.
> println "Search Result"
> searchF.response.hits.each {SearchHit hit ->
> println "Got hit $hit.id from ${hit.index}/${hit.type}"
> }
> 
> ```
> 
> }

## --

Paul Loy  
[paul@keteracel.com](mailto:paul@keteracel.com)  
[http://www.keteracel.com/paul](http://www.keteracel.com/paul)

---

<div class="post-metadata">

**Author:** ![Samuel\_Doyle](https://avatars.discourse-cdn.com/v4/letter/s/e19adc/32.png) [@Samuel\_Doyle](https://discuss.elastic.co/u/Samuel_Doyle)\
**Post date:** [June 14, 2010, 7:15pm UTC](https://discuss.elastic.co/t/using-elastic-search-in-unit-and-integration-testing/3018/3 "2010-06-14T19:15:36Z")

</div>

Not sure exactly what the deal is but when I switched to the Java API I was  
able to get it to work.  
Perhaps it was something I missed with the Groovy API that resulted in an  
error or exception that didn't get raised, I'll revisit later.

Thanks

On Mon, Jun 14, 2010 at 11:52 AM, Paul Loy [keteracel@gmail.com](mailto:keteracel@gmail.com) wrote:

> you'll need to refresh the index before searching. Something like this in  
> Java:
> 
> testClient.admin().indices().refresh("inventory");
> 
> On Mon, Jun 14, 2010 at 6:23 PM, Samuel Doyle [samueldoyle@gmail.com](mailto:samueldoyle@gmail.com)wrote:
> 
> > Hello,  
> > I am trying to perform some unit testing in grails and doing a simple test  
> > I appear to be able to index but a following search will find nothing. Here  
> > is an example:
> > 
> > // Setup the test node and client  
> > @Before  
> > public void init() {  
> > assertNotNull dataSource  
> > GNodeBuilder nodeBuilder = nodeBuilder()  
> > nodeBuilder.settings {  
> > node {  
> > local = true  
> > }  
> > cluster {  
> > name = "Integration Test"  
> > }  
> > }  
> > testNode = nodeBuilder.node()  
> > testClient = testNode.client  
> > assertNotNull testNode  
> > assertNotNull testClient  
> > }
> > 
> > // Perform a simple index followed by a simple search  
> > @Test  
> > void simpleTest() {
> > 
> > ```
> > def indexF = testClient.index {
> > index "inventory"
> > type "aType"
> > id "1"
> > source {
> > code = "somecode"
> > }
> > }
> > println "Indexed id ${indexF.response.id}"
> > 
> > def searchF = testClient.search {
> > indices "inventory"
> > types "aType"
> > source {
> > query {
> > term(code: "somecode")
> > }
> > }
> > }
> > 
> > // This should get a hit but I receive nothing.
> > println "Search Result"
> > searchF.response.hits.each {SearchHit hit ->
> > println "Got hit $hit.id from ${hit.index}/${hit.type}"
> > }
> > 
> > ```
> > 
> > }
> 
> ## --
> 
> Paul Loy  
> [paul@keteracel.com](mailto:paul@keteracel.com)  
> [http://www.keteracel.com/paul](http://www.keteracel.com/paul)

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [June 14, 2010, 7:21pm UTC](https://discuss.elastic.co/t/using-elastic-search-in-unit-and-integration-testing/3018/4 "2010-06-14T19:21:10Z")

</div>

The previous answer is the correct one, and has nothing to do with Java API  
or Groovy API, you need to call refresh right after the index operation to  
see it, otherwise, it will be refreshed in the background periodically. It  
might be that the Java test you wrote got into the timing window where it  
refreshed after the index operation.

-shay.banon

On Mon, Jun 14, 2010 at 10:15 PM, Samuel Doyle [samueldoyle@gmail.com](mailto:samueldoyle@gmail.com)wrote:

> Not sure exactly what the deal is but when I switched to the Java API I was  
> able to get it to work.  
> Perhaps it was something I missed with the Groovy API that resulted in an  
> error or exception that didn't get raised, I'll revisit later.
> 
> Thanks
> 
> On Mon, Jun 14, 2010 at 11:52 AM, Paul Loy [keteracel@gmail.com](mailto:keteracel@gmail.com) wrote:
> 
> > you'll need to refresh the index before searching. Something like this in  
> > Java:
> > 
> > testClient.admin().indices().refresh("inventory");
> > 
> > On Mon, Jun 14, 2010 at 6:23 PM, Samuel Doyle [samueldoyle@gmail.com](mailto:samueldoyle@gmail.com)wrote:
> > 
> > > Hello,  
> > > I am trying to perform some unit testing in grails and doing a simple  
> > > test I appear to be able to index but a following search will find nothing.  
> > > Here is an example:
> > > 
> > > // Setup the test node and client  
> > > @Before  
> > > public void init() {  
> > > assertNotNull dataSource  
> > > GNodeBuilder nodeBuilder = nodeBuilder()  
> > > nodeBuilder.settings {  
> > > node {  
> > > local = true  
> > > }  
> > > cluster {  
> > > name = "Integration Test"  
> > > }  
> > > }  
> > > testNode = nodeBuilder.node()  
> > > testClient = testNode.client  
> > > assertNotNull testNode  
> > > assertNotNull testClient  
> > > }
> > > 
> > > // Perform a simple index followed by a simple search  
> > > @Test  
> > > void simpleTest() {
> > > 
> > > ```
> > > def indexF = testClient.index {
> > > index "inventory"
> > > type "aType"
> > > id "1"
> > > source {
> > > code = "somecode"
> > > }
> > > }
> > > println "Indexed id ${indexF.response.id}"
> > > 
> > > def searchF = testClient.search {
> > > indices "inventory"
> > > types "aType"
> > > source {
> > > query {
> > > term(code: "somecode")
> > > }
> > > }
> > > }
> > > 
> > > // This should get a hit but I receive nothing.
> > > println "Search Result"
> > > searchF.response.hits.each {SearchHit hit ->
> > > println "Got hit $hit.id from ${hit.index}/${hit.type}"
> > > }
> > > 
> > > ```
> > > 
> > > }
> > 
> > ## --
> > 
> > Paul Loy  
> > [paul@keteracel.com](mailto:paul@keteracel.com)  
> > [http://www.keteracel.com/paul](http://www.keteracel.com/paul)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 4:23am UTC](https://discuss.elastic.co/t/using-elastic-search-in-unit-and-integration-testing/3018/5 "2017-07-06T04:23:19Z")

</div>


