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().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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elasticsearch/7e3ed8ae-a65b-4fa7-9f2d-053c87cff3f8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to