Using Elastic Search in unit and integration testing

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}"
}

}

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 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
http://www.keteracel.com/paul

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 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.comwrote:

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
http://www.keteracel.com/paul

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.comwrote:

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 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.comwrote:

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
http://www.keteracel.com/paul