Running embedded elastic search for unit testing: configuration problem

Hi,
I'm trying to run elasticsearch locally inside a unit testing framework,
and running into a bit of trouble. The behavior I am seeing is that Get
requests work fine, but Search requests for some reason give me 0 results
always. here's an example:

    // first, try a get request, to make sure there is something in the 

index
GetResponse results = client.prepareGet(INDEX_NAME, INDEX_TYPE,
testID).execute().actionGet();
// this assertion succeeds, as we expect it to.
assertThat(results.getId()).isEqualTo(testID);

    // next, try the simplest possible search
    SearchResponse s1 = 

client.prepareSearch(INDEX_NAME).setQuery(matchAllQuery()).setTypes(INDEX_TYPE).execute().get();

  •    // this assertion fails. why?*
      assertThat(s1.getHits().totalHits()).isGreaterThanOrEqualTo(1);
    

When I run the same code against a "real" elastic search index, it gives me
all the documents in my index, which is what I would expect. I think that
the problem is how I am configuring my local node. Does anyone see any red
flags in the below Node configuration?

@BeforeMethod
public void setup() {
Settings settings = ImmutableSettings.settingsBuilder()
.put("node.http.enabled", true)
.put("path.logs","target/elasticsearch/logs")
.put("path.data","target/elasticsearch/data")
.put("gateway.type", "none")
.put("index.store.type", "memory")
.put("index.number_of_shards", 1)
.put("index.number_of_replicas", 1).build();

    node = 

NodeBuilder.nodeBuilder().local(true).settings(settings).node();
client = node.client();
}

thanks in advance for your help!

--paul

--

Hi Paul,

Just add a refresh after indexing your docs.

See here: https://github.com/elasticsearchfr/hands-on/blob/answers/src/test/java/org/elasticsearchfr/handson/ex2/SearchTest.java

--
David :wink:
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 2 oct. 2012 à 19:20, Paul Sanwald pcsanwald@gmail.com a écrit :

Hi,
I'm trying to run elasticsearch locally inside a unit testing framework, and running into a bit of trouble. The behavior I am seeing is that Get requests work fine, but Search requests for some reason give me 0 results always. here's an example:

    // first, try a get request, to make sure there is something in the index
    GetResponse results = client.prepareGet(INDEX_NAME, INDEX_TYPE, testID).execute().actionGet();
    // this assertion succeeds, as we expect it to.
    assertThat(results.getId()).isEqualTo(testID);

    // next, try the simplest possible search
    SearchResponse s1 = client.prepareSearch(INDEX_NAME).setQuery(matchAllQuery()).setTypes(INDEX_TYPE).execute().get();

    // this assertion fails. why?
    assertThat(s1.getHits().totalHits()).isGreaterThanOrEqualTo(1);

When I run the same code against a "real" Elasticsearch index, it gives me all the documents in my index, which is what I would expect. I think that the problem is how I am configuring my local node. Does anyone see any red flags in the below Node configuration?

@BeforeMethod
public void setup() {
Settings settings = ImmutableSettings.settingsBuilder()
.put("node.http.enabled", true)
.put("path.logs","target/elasticsearch/logs")
.put("path.data","target/elasticsearch/data")
.put("gateway.type", "none")
.put("index.store.type", "memory")
.put("index.number_of_shards", 1)
.put("index.number_of_replicas", 1).build();

    node = NodeBuilder.nodeBuilder().local(true).settings(settings).node();
    client = node.client();
}

thanks in advance for your help!

--paul

--

David,
thanks so much, this was just the thing!

adding:

node.client().admin().indices().prepareRefresh().execute().actionGet();

after my submit fixed the issue.

best,

--paul

On Tuesday, October 2, 2012 1:34:00 PM UTC-4, David Pilato wrote:

Hi Paul,

Just add a refresh after indexing your docs.

See here:
https://github.com/elasticsearchfr/hands-on/blob/answers/src/test/java/org/elasticsearchfr/handson/ex2/SearchTest.java

--
David :wink:
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 2 oct. 2012 à 19:20, Paul Sanwald <pcsa...@gmail.com <javascript:>> a
écrit :

Hi,
I'm trying to run elasticsearch locally inside a unit testing framework,
and running into a bit of trouble. The behavior I am seeing is that Get
requests work fine, but Search requests for some reason give me 0 results
always. here's an example:

    // first, try a get request, to make sure there is something in 

the index
GetResponse results = client.prepareGet(INDEX_NAME, INDEX_TYPE,
testID).execute().actionGet();
// this assertion succeeds, as we expect it to.
assertThat(results.getId()).isEqualTo(testID);

    // next, try the simplest possible search
    SearchResponse s1 = 

client.prepareSearch(INDEX_NAME).setQuery(matchAllQuery()).setTypes(INDEX_TYPE).execute().get();

  •    // this assertion fails. why?*
      assertThat(s1.getHits().totalHits()).isGreaterThanOrEqualTo(1);
    

When I run the same code against a "real" Elasticsearch index, it gives
me all the documents in my index, which is what I would expect. I think
that the problem is how I am configuring my local node. Does anyone see any
red flags in the below Node configuration?

@BeforeMethod
public void setup() {
Settings settings = ImmutableSettings.settingsBuilder()
.put("node.http.enabled", true)
.put("path.logs","target/elasticsearch/logs")
.put("path.data","target/elasticsearch/data")
.put("gateway.type", "none")
.put("index.store.type", "memory")
.put("index.number_of_shards", 1)
.put("index.number_of_replicas", 1).build();

    node = 

NodeBuilder.nodeBuilder().local(true).settings(settings).node();
client = node.client();
}

thanks in advance for your help!

--paul

--

--