Search in sources :

Example 1 with Predicate

use of org.elasticsearch.common.base.Predicate in project elasticsearch-river-couchdb by elastic.

the class CouchdbRiverIntegrationTest method testScriptingParentChild_51.

/**
 * Test case for #51: https://github.com/elasticsearch/elasticsearch-river-couchdb/issues/51
 */
@Test
public void testScriptingParentChild_51() throws IOException, InterruptedException, ExecutionException {
    prepareCreate(getDbName()).addMapping("region", jsonBuilder().startObject().startObject("region").endObject().endObject().string()).addMapping("campus", jsonBuilder().startObject().startObject("campus").startObject("_parent").field("type", "region").endObject().endObject().endObject().string()).get();
    launchTest(jsonBuilder().startObject().field("type", "couchdb").startObject("couchdb").field("script", "ctx._type = ctx.doc.type; if (ctx._type == 'campus') { ctx._parent = ctx.doc.parent_id; }").endObject().endObject(), 0, new InjectorHook() {

        @Override
        public void inject() {
            try {
                putDocument(getDbName(), "1", "type", "region", "name", "bretagne");
                putDocument(getDbName(), "2", "type", "campus", "name", "enib", "parent_id", "1");
            } catch (IOException e) {
                logger.error("Error while injecting documents");
            }
        }
    });
    // Check that docs are indexed by the river
    assertThat(awaitBusy(new Predicate<Object>() {

        public boolean apply(Object obj) {
            try {
                refresh();
                SearchResponse response = client().prepareSearch(getDbName()).get();
                logger.info("  -> got {} docs in {} index", response.getHits().totalHits(), getDbName());
                return response.getHits().totalHits() == 2;
            } catch (IndexMissingException e) {
                return false;
            }
        }
    }, 1, TimeUnit.MINUTES), equalTo(true));
    SearchResponse response = client().prepareSearch(getDbName()).setQuery(QueryBuilders.hasChildQuery("campus", QueryBuilders.matchQuery("name", "enib"))).get();
    assertThat(response.getHits().getTotalHits(), is(1L));
    assertThat(response.getHits().getAt(0).getType(), is("region"));
    assertThat(response.getHits().getAt(0).getId(), is("1"));
}
Also used : IOException(java.io.IOException) IndexMissingException(org.elasticsearch.indices.IndexMissingException) Predicate(org.elasticsearch.common.base.Predicate) SearchResponse(org.elasticsearch.action.search.SearchResponse) Test(org.junit.Test) ElasticsearchIntegrationTest(org.elasticsearch.test.ElasticsearchIntegrationTest)

Example 2 with Predicate

use of org.elasticsearch.common.base.Predicate in project elasticsearch-river-couchdb by elastic.

the class CouchdbRiverIntegrationTest method testCreateCouchdbDatabaseWhileRunning_17.

/**
 * Test case for #17: https://github.com/elasticsearch/elasticsearch-river-couchdb/issues/17
 */
@Test
public void testCreateCouchdbDatabaseWhileRunning_17() throws IOException, InterruptedException {
    final int nbDocs = between(50, 300);
    logger.info("  -> Checking couchdb running");
    CouchDBClient.checkCouchDbRunning();
    logger.info("  -> Create index");
    try {
        createIndex(getDbName());
    } catch (IndexAlreadyExistsException e) {
    // No worries. We already created the index before
    }
    logger.info("  -> Create river");
    index("_river", getDbName(), "_meta", jsonBuilder().startObject().field("type", "couchdb").endObject());
    // Check that the river is started
    assertThat(awaitBusy(new Predicate<Object>() {

        public boolean apply(Object obj) {
            try {
                refresh();
                GetResponse response = get("_river", getDbName(), "_status");
                return response.isExists();
            } catch (IndexMissingException e) {
                return false;
            }
        }
    }, 5, TimeUnit.SECONDS), equalTo(true));
    logger.info("  -> Creating test database [{}]", getDbName());
    CouchDBClient.dropAndCreateTestDatabase(getDbName());
    logger.info("  -> Inserting [{}] docs in couchdb", nbDocs);
    for (int i = 0; i < nbDocs; i++) {
        CouchDBClient.putDocument(getDbName(), "" + i, "foo", "bar", "content", "" + i);
    }
    // Check that docs are still processed by the river
    assertThat(awaitBusy(new Predicate<Object>() {

        public boolean apply(Object obj) {
            try {
                refresh();
                SearchResponse response = client().prepareSearch(getDbName()).get();
                logger.info("  -> got {} docs in {} index", response.getHits().totalHits(), getDbName());
                return response.getHits().totalHits() == nbDocs;
            } catch (IndexMissingException e) {
                return false;
            }
        }
    }, 1, TimeUnit.MINUTES), equalTo(true));
}
Also used : IndexAlreadyExistsException(org.elasticsearch.indices.IndexAlreadyExistsException) IndexMissingException(org.elasticsearch.indices.IndexMissingException) GetResponse(org.elasticsearch.action.get.GetResponse) Predicate(org.elasticsearch.common.base.Predicate) SearchResponse(org.elasticsearch.action.search.SearchResponse) Test(org.junit.Test) ElasticsearchIntegrationTest(org.elasticsearch.test.ElasticsearchIntegrationTest)

Example 3 with Predicate

use of org.elasticsearch.common.base.Predicate in project elasticsearch-river-couchdb by elastic.

the class CouchdbRiverIntegrationTest method testClosingWhileIndexing_66.

/**
 * Test case for #66: https://github.com/elasticsearch/elasticsearch-river-couchdb/issues/66
 */
@Test
public void testClosingWhileIndexing_66() throws IOException, InterruptedException {
    final int nbDocs = 10;
    logger.info("  -> Checking couchdb running");
    CouchDBClient.checkCouchDbRunning();
    logger.info("  -> Creating test database [{}]", getDbName());
    CouchDBClient.dropAndCreateTestDatabase(getDbName());
    logger.info("  -> Inserting [{}] docs in couchdb", nbDocs);
    for (int i = 0; i < nbDocs; i++) {
        CouchDBClient.putDocument(getDbName(), "" + i, "foo", "bar", "content", "" + i);
    }
    logger.info("  -> Create index");
    try {
        createIndex(getDbName());
    } catch (IndexAlreadyExistsException e) {
    // No worries. We already created the index before
    }
    logger.info("  -> Create river");
    index("_river", getDbName(), "_meta", jsonBuilder().startObject().field("type", "couchdb").startObject("couchdb").field("script", "for (int x = 0; x < 10000000; x++) { x*x*x } ;").endObject().startObject("index").field("flush_interval", "100ms").endObject().endObject());
    // Check that docs are indexed by the river
    assertThat(awaitBusy(new Predicate<Object>() {

        public boolean apply(Object obj) {
            try {
                refresh();
                SearchResponse response = client().prepareSearch(getDbName()).get();
                logger.info("  -> got {} docs in {} index", response.getHits().totalHits(), getDbName());
                return response.getHits().totalHits() == nbDocs;
            } catch (IndexMissingException e) {
                return false;
            }
        }
    }, 1, TimeUnit.MINUTES), equalTo(true));
    logger.info("  -> Inserting [{}] docs in couchdb", nbDocs);
    for (int i = nbDocs; i < 2 * nbDocs; i++) {
        CouchDBClient.putDocument(getDbName(), "" + i, "foo", "bar", "content", "" + i);
    }
    // Check that docs are still processed by the river
    assertThat(awaitBusy(new Predicate<Object>() {

        public boolean apply(Object obj) {
            try {
                refresh();
                SearchResponse response = client().prepareSearch(getDbName()).get();
                logger.info("  -> got {} docs in {} index", response.getHits().totalHits(), getDbName());
                return response.getHits().totalHits() > nbDocs;
            } catch (IndexMissingException e) {
                return false;
            }
        }
    }, 10, TimeUnit.SECONDS), equalTo(true));
    logger.info("  -> Remove river while injecting");
    client().prepareDelete("_river", getDbName(), "_meta").get();
    logger.info("  -> Inserting [{}] docs in couchdb", nbDocs);
    for (int i = 2 * nbDocs; i < 3 * nbDocs; i++) {
        CouchDBClient.putDocument(getDbName(), "" + i, "foo", "bar", "content", "" + i);
    }
    // Check that docs are indexed by the river
    boolean foundAllDocs = awaitBusy(new Predicate<Object>() {

        public boolean apply(Object obj) {
            try {
                refresh();
                SearchResponse response = client().prepareSearch(getDbName()).get();
                logger.info("  -> got {} docs in {} index", response.getHits().totalHits(), getDbName());
                return response.getHits().totalHits() == 3 * nbDocs;
            } catch (IndexMissingException e) {
                return false;
            }
        }
    }, 10, TimeUnit.SECONDS);
    // We should not have 30 documents at the end as we removed the river immediately after having
    // injecting 10 more docs in couchdb
    assertThat("We should not have 30 documents as the river is supposed to have been stopped!", foundAllDocs, is(false));
// We expect seeing a line in logs like:
// [WARN ][org.elasticsearch.river.couchdb] [node_0] [couchdb][elasticsearch_couch_test_test_closing_while_indexing_66] river was closing while trying to index document [elasticsearch_couch_test_test_closing_while_indexing_66/elasticsearch_couch_test_test_closing_while_indexing_66/11]. Operation skipped.
}
Also used : IndexAlreadyExistsException(org.elasticsearch.indices.IndexAlreadyExistsException) IndexMissingException(org.elasticsearch.indices.IndexMissingException) Predicate(org.elasticsearch.common.base.Predicate) SearchResponse(org.elasticsearch.action.search.SearchResponse) Test(org.junit.Test) ElasticsearchIntegrationTest(org.elasticsearch.test.ElasticsearchIntegrationTest)

Example 4 with Predicate

use of org.elasticsearch.common.base.Predicate in project elasticsearch-river-couchdb by elastic.

the class CouchdbRiverIntegrationTest method runAttachmentTest.

private void runAttachmentTest(boolean disabled) throws IOException, InterruptedException {
    // Create the river
    launchTest(jsonBuilder().startObject().field("type", "couchdb").startObject("couchdb").field("host", CouchDBClient.host).field("port", CouchDBClient.port).field("db", getDbName()).field("ignore_attachments", disabled).endObject().endObject(), 0, new InjectorHook() {

        @Override
        public void inject() {
            try {
                putDocumentWithAttachments(getDbName(), "1", new ImmutableList.Builder<String>().add("foo", "bar").build(), "text-in-english.txt", "God save the queen!", "text-in-french.txt", "Allons enfants !");
            } catch (IOException e) {
                logger.error("Error while injecting attachments");
            }
        }
    });
    // Check that docs are indexed by the river
    assertThat(awaitBusy(new Predicate<Object>() {

        public boolean apply(Object obj) {
            try {
                refresh();
                SearchResponse response = client().prepareSearch(getDbName()).get();
                logger.info("  -> got {} docs in {} index", response.getHits().totalHits(), getDbName());
                return response.getHits().totalHits() == 1;
            } catch (IndexMissingException e) {
                return false;
            }
        }
    }, 1, TimeUnit.MINUTES), equalTo(true));
    SearchResponse response = client().prepareSearch(getDbName()).addField("_attachments.text-in-english.txt.content_type").addField("_attachments.text-in-french.txt.content_type").get();
    assertThat(response.getHits().getAt(0).field("_attachments.text-in-english.txt.content_type"), disabled ? nullValue() : notNullValue());
    assertThat(response.getHits().getAt(0).field("_attachments.text-in-french.txt.content_type"), disabled ? nullValue() : notNullValue());
    if (!disabled) {
        assertThat(response.getHits().getAt(0).field("_attachments.text-in-english.txt.content_type").getValue().toString(), is("text/plain"));
        assertThat(response.getHits().getAt(0).field("_attachments.text-in-french.txt.content_type").getValue().toString(), is("text/plain"));
    }
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentFactory.jsonBuilder(org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder) IOException(java.io.IOException) IndexMissingException(org.elasticsearch.indices.IndexMissingException) Predicate(org.elasticsearch.common.base.Predicate) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 5 with Predicate

use of org.elasticsearch.common.base.Predicate in project elasticsearch-river-couchdb by elastic.

the class CouchdbRiverIntegrationTest method launchTest.

private void launchTest(XContentBuilder river, final Integer numDocs, InjectorHook injectorHook) throws IOException, InterruptedException {
    logger.info("  -> Checking couchdb running");
    CouchDBClient.checkCouchDbRunning();
    logger.info("  -> Creating test database [{}]", getDbName());
    CouchDBClient.dropAndCreateTestDatabase(getDbName());
    logger.info("  -> Put [{}] documents", numDocs);
    for (int i = 0; i < numDocs; i++) {
        CouchDBClient.putDocument(getDbName(), "" + i, "foo", "bar", "content", "" + i);
    }
    logger.info("  -> Put [{}] documents done", numDocs);
    if (injectorHook != null) {
        logger.info("  -> Injecting extra data");
        injectorHook.inject();
    }
    logger.info("  -> Create river");
    try {
        createIndex(getDbName());
    } catch (IndexAlreadyExistsException e) {
    // No worries. We already created the index before
    }
    index("_river", getDbName(), "_meta", river);
    logger.info("  -> Wait for some docs");
    assertThat(awaitBusy(new Predicate<Object>() {

        public boolean apply(Object obj) {
            try {
                refresh();
                SearchResponse response = client().prepareSearch(getDbName()).get();
                logger.info("  -> got {} docs in {} index", response.getHits().totalHits(), getDbName());
                return response.getHits().totalHits() == numDocs;
            } catch (IndexMissingException e) {
                return false;
            }
        }
    }, 1, TimeUnit.MINUTES), equalTo(true));
}
Also used : IndexAlreadyExistsException(org.elasticsearch.indices.IndexAlreadyExistsException) IndexMissingException(org.elasticsearch.indices.IndexMissingException) Predicate(org.elasticsearch.common.base.Predicate) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

Predicate (org.elasticsearch.common.base.Predicate)8 IndexMissingException (org.elasticsearch.indices.IndexMissingException)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)5 GetResponse (org.elasticsearch.action.get.GetResponse)3 IndexAlreadyExistsException (org.elasticsearch.indices.IndexAlreadyExistsException)3 ElasticsearchIntegrationTest (org.elasticsearch.test.ElasticsearchIntegrationTest)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 CountResponse (org.elasticsearch.action.count.CountResponse)2 Channel (com.rabbitmq.client.Channel)1 Connection (com.rabbitmq.client.Connection)1 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)1 ConnectException (java.net.ConnectException)1 HashSet (java.util.HashSet)1 IndexResponse (org.elasticsearch.action.index.IndexResponse)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 XContentFactory.jsonBuilder (org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder)1 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)1