# Local testing not working

**URL:** <https://discuss.elastic.co/t/local-testing-not-working/17024>\
**Category:** Elasticsearch\
**Created:** [April 15, 2014, 11:11am UTC](https://discuss.elastic.co/t/local-testing-not-working/17024 "2014-04-15T11:11:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Magnus\_Poromaa](https://avatars.discourse-cdn.com/v4/letter/m/e19b73/32.png) [@Magnus\_Poromaa](https://discuss.elastic.co/u/Magnus_Poromaa)\
**Post date:** [April 15, 2014, 11:11am UTC](https://discuss.elastic.co/t/local-testing-not-working/17024/1 "2014-04-15T11:11:12Z")

</div>

Hi

I am just starting up with elastic search and I have creaed a local builder  
like this

public class EmbeddedElasticSearchServer {

```
private static final String DEFAULT_DATA_DIRECTORY = 

```

"data/elasticsearch-data";

```
private final Node node;
private final String dataDirectory;

public EmbeddedElasticSearchServer() {
    this(DEFAULT_DATA_DIRECTORY);
}

public EmbeddedElasticSearchServer(String dataDirectory) {
    this.dataDirectory = dataDirectory;

    ImmutableSettings.Builder settings = 

```

ImmutableSettings.settingsBuilder()  
.put("http.enabled", "false")  
.put("index.number\_of\_shards", 1)  
.put("index.number\_of\_replicas", 1)  
.put("path.data", dataDirectory);

```
    node = NodeBuilder.nodeBuilder()
            .local(true)
            .settings(settings.build())
            .build()
            .start();

}

public Client getClient() {

    return node.client();
}

public void shutdown() {
    node.close();
    deleteDataDirectory();
}

private void deleteDataDirectory() {
    try {
        FileUtils.deleteDirectory(new File(dataDirectory));
    } catch (IOException e) {
        throw new RuntimeException("Could not delete data directory of 

```

embedded elastic search server", e);  
}  
}

}

The problem is that it fails and if I remove the  
FileUtils.deleteDirectory(new File(dataDirectory)); is succeeds the second  
time. This most likely mean it works when the directory is there.

The test I run is just

@Test  
public void testFindInstruments() throws Exception {  
String json =  
mapper.writeValueAsString(InstrumentIndex.builder().withName("test").withShortName("olle").build());  
IndexResponse response = client.prepareIndex("accounts", "account",  
"1")  
.setSource(json)  
.setConsistencyLevel(WriteConsistencyLevel.ONE)  
.execute()  
.actionGet();

```
    //This is how we create the index more completely

```

[//client.admin](https://client.admin)().indices().create(Requests.createIndexRequest("")).actionGet();

```
    SearchRequestBuilder searchRequestBuilder = new 

```

SearchRequestBuilder(client);

```
    //@TODO MAGPOR We need to further develop this but it should be our 

```

own query API and not elastic search that  
//we front with  
InstrumentQuery instrumentQuery = InstrumentQuery.builder()  
.withNameEquals("test")  
.or()  
.withNameEquals("test")  
.build();

```
    BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
    for (QueryBuilder childQueryBuilder : instrumentQuery) {
        queryBuilder.should(childQueryBuilder);
    }
    searchRequestBuilder.setQuery(queryBuilder);
    SearchResponse searchResponse = searchRequestBuilder.get();
    Assert.assertEquals(1, searchResponse.getHits().totalHits());
}

```

The InstrumentQuery is using the elastic search query builder. The failig  
part is the

Assert.assertEquals(1, searchResponse.getHits().totalHits());

which seems to be the first time i I run it for some reason.

I guess it has something to do with that elastic expects the directory to  
be there when it starts up or ?

Would be good to get some advice on getting this working soince it is a  
very nice way to test

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/7e3ed8ae-a65b-4fa7-9f2d-053c87cff3f8%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7e3ed8ae-a65b-4fa7-9f2d-053c87cff3f8%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Binh\_Ly\_2](https://avatars.discourse-cdn.com/v4/letter/b/d07c76/32.png) [@Binh\_Ly\_2](https://discuss.elastic.co/u/Binh_Ly_2)\
**Post date:** [April 16, 2014, 5:39pm UTC](https://discuss.elastic.co/t/local-testing-not-working/17024/2 "2014-04-16T17:39:28Z")

</div>

Just setRefresh(true) when you index a doc, and it should be there  
immediately when you search it afterwards.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/fd32daf9-fc4a-4330-8268-ee9597308d3f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/fd32daf9-fc4a-4330-8268-ee9597308d3f%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [April 21, 2014, 12:49pm UTC](https://discuss.elastic.co/t/local-testing-not-working/17024/3 "2014-04-21T12:49:07Z")

</div>

Hey,

I highly recommend reusing the existing elasticsearch test infrastructure,  
as it cleans up behind some test runs and also creates everything upfront,  
see

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

The integration tests might be especially interesting for you

--Alex

On Tue, Apr 15, 2014 at 1:11 PM, Magnus Poromaa [magnus.poromaa@gmail.com](mailto:magnus.poromaa@gmail.com)wrote:

> Hi
> 
> I am just starting up with Elasticsearch and I have creaed a local  
> builder like this
> 
> public class EmbeddedElasticSearchServer {
> 
> ```
> private static final String DEFAULT_DATA_DIRECTORY =
> 
> ```
> 
> "data/elasticsearch-data";
> 
> ```
> private final Node node;
> private final String dataDirectory;
> 
> public EmbeddedElasticSearchServer() {
> this(DEFAULT_DATA_DIRECTORY);
> }
> 
> public EmbeddedElasticSearchServer(String dataDirectory) {
> this.dataDirectory = dataDirectory;
> 
> ImmutableSettings.Builder settings =
> 
> ```
> 
> ImmutableSettings.settingsBuilder()  
> .put("http.enabled", "false")  
> .put("index.number\_of\_shards", 1)  
> .put("index.number\_of\_replicas", 1)  
> .put("path.data", dataDirectory);
> 
> ```
> node = NodeBuilder.nodeBuilder()
> .local(true)
> .settings(settings.build())
> .build()
> .start();
> 
> }
> 
> public Client getClient() {
> 
> return node.client();
> }
> 
> public void shutdown() {
> node.close();
> deleteDataDirectory();
> }
> 
> private void deleteDataDirectory() {
> try {
> FileUtils.deleteDirectory(new File(dataDirectory));
> } catch (IOException e) {
> throw new RuntimeException("Could not delete data directory of
> 
> ```
> 
> embedded Elasticsearch server", e);  
> }  
> }
> 
> }
> 
> The problem is that it fails and if I remove the  
> FileUtils.deleteDirectory(new File(dataDirectory)); is succeeds the second  
> time. This most likely mean it works when the directory is there.
> 
> The test I run is just
> 
> @Test  
> public void testFindInstruments() throws Exception {  
> String json =  
> mapper.writeValueAsString(InstrumentIndex.builder().withName("test").withShortName("olle").build());  
> IndexResponse response = client.prepareIndex("accounts",  
> "account", "1")  
> .setSource(json)  
> .setConsistencyLevel(WriteConsistencyLevel.ONE)  
> .execute()  
> .actionGet();
> 
> ```
> //This is how we create the index more completely
> 
> ```
> 
> [//client.admin](https://client.admin)().indices().create(Requests.createIndexRequest("")).actionGet();
> 
> ```
> SearchRequestBuilder searchRequestBuilder = new
> 
> ```
> 
> SearchRequestBuilder(client);
> 
> ```
> //@TODO MAGPOR We need to further develop this but it should be
> 
> ```
> 
> our own query API and not Elasticsearch that  
> //we front with  
> InstrumentQuery instrumentQuery = InstrumentQuery.builder()  
> .withNameEquals("test")  
> .or()  
> .withNameEquals("test")  
> .build();
> 
> ```
> BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
> for (QueryBuilder childQueryBuilder : instrumentQuery) {
> queryBuilder.should(childQueryBuilder);
> }
> searchRequestBuilder.setQuery(queryBuilder);
> SearchResponse searchResponse = searchRequestBuilder.get();
> Assert.assertEquals(1, searchResponse.getHits().totalHits());
> }
> 
> ```
> 
> The InstrumentQuery is using the Elasticsearch query builder. The failig  
> part is the
> 
> Assert.assertEquals(1, searchResponse.getHits().totalHits());
> 
> which seems to be the first time i I run it for some reason.
> 
> I guess it has something to do with that elastic expects the directory to  
> be there when it starts up or ?
> 
> Would be good to get some advice on getting this working soince it is a  
> very nice way to test
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/7e3ed8ae-a65b-4fa7-9f2d-053c87cff3f8%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7e3ed8ae-a65b-4fa7-9f2d-053c87cff3f8%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/7e3ed8ae-a65b-4fa7-9f2d-053c87cff3f8%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/7e3ed8ae-a65b-4fa7-9f2d-053c87cff3f8%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAGCwEM8NGw%2BvXOj9BYrWaMfYQNvHM233O3UvutSCEXa9NNCWRA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAGCwEM8NGw%2BvXOj9BYrWaMfYQNvHM233O3UvutSCEXa9NNCWRA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 1:34am UTC](https://discuss.elastic.co/t/local-testing-not-working/17024/4 "2017-07-06T01:34:33Z")

</div>


