Local testing not working

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Local testing not working

Magnus Poromaa
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 [hidden email].
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 | Threaded
Open this post in threaded view
|

Re: Local testing not working

Binh Ly-2
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/fd32daf9-fc4a-4330-8268-ee9597308d3f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Local testing not working

Alexander Reelsen-2
In reply to this post by Magnus Poromaa
Hey,

I highly recommend reusing the existing elasticsearch test infrastructure, as it cleans up behind some test runs and also creates everything upfront, see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/randomized-testing.html#randomized-testing

The integration tests might be especially interesting for you


--Alex


On Tue, Apr 15, 2014 at 1:11 PM, Magnus Poromaa <[hidden email]> wrote:
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 [hidden email].
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.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/CAGCwEM8NGw%2BvXOj9BYrWaMfYQNvHM233O3UvutSCEXa9NNCWRA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.