# Running embedded elastic search for unit testing: configuration problem

**URL:** <https://discuss.elastic.co/t/running-embedded-elastic-search-for-unit-testing-configuration-problem/9213>\
**Category:** Elasticsearch\
**Created:** [October 2, 2012, 5:20pm UTC](https://discuss.elastic.co/t/running-embedded-elastic-search-for-unit-testing-configuration-problem/9213 "2012-10-02T17:20:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Sanwald](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paul_sanwald/32/2076_2.png) [@Paul\_Sanwald](https://discuss.elastic.co/u/Paul_Sanwald)\
**Post date:** [October 2, 2012, 5:20pm UTC](https://discuss.elastic.co/t/running-embedded-elastic-search-for-unit-testing-configuration-problem/9213/1 "2012-10-02T17:20:03Z")

</div>

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

--

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 2, 2012, 5:33pm UTC](https://discuss.elastic.co/t/running-embedded-elastic-search-for-unit-testing-configuration-problem/9213/2 "2012-10-02T17:33:46Z")

</div>

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](https://github.com/elasticsearchfr/hands-on/blob/answers/src/test/java/org/elasticsearchfr/handson/ex2/SearchTest.java)

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

Le 2 oct. 2012 à 19:20, Paul Sanwald [pcsanwald@gmail.com](mailto: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

--

---

<div class="post-metadata">

**Author:** ![Paul\_Sanwald](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paul_sanwald/32/2076_2.png) [@Paul\_Sanwald](https://discuss.elastic.co/u/Paul_Sanwald)\
**Post date:** [October 2, 2012, 6:01pm UTC](https://discuss.elastic.co/t/running-embedded-elastic-search-for-unit-testing-configuration-problem/9213/3 "2012-10-02T18:01:15Z")

</div>

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](https://github.com/elasticsearchfr/hands-on/blob/answers/src/test/java/org/elasticsearchfr/handson/ex2/SearchTest.java)
> 
> --  
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 2 oct. 2012 à 19:20, Paul Sanwald \<[pcsa...@gmail.com](mailto: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
> 
> --

--

---

<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, 3:10am UTC](https://discuss.elastic.co/t/running-embedded-elastic-search-for-unit-testing-configuration-problem/9213/4 "2017-07-06T03:10:30Z")

</div>


