Search in sources :

Example 1 with CountResponse

use of org.elasticsearch.action.count.CountResponse in project elasticsearch-river-neo4j by sksamuel.

the class Neo4jRiverIntTest method infiniteTest.

/**
     * I use this test for a shell like experience, has to be cancelled manually.
     */
// @Test
public void infiniteTest() throws InterruptedException {
    // allow river to start
    Thread.sleep(200);
    while (true) {
        // time for poller to index
        Thread.sleep(200);
        refreshIndex();
        CountResponse resp = node.client().count(countRequest(index).types(type).source(queryString("mowbray").defaultField("manager").toString())).actionGet();
        logger.debug("How many moggas? {} !", resp.getCount());
    }
}
Also used : CountResponse(org.elasticsearch.action.count.CountResponse)

Example 2 with CountResponse

use of org.elasticsearch.action.count.CountResponse in project elasticsearch-river-neo4j by sksamuel.

the class Neo4jRiverIntTest method testAddAndRemoveNodes.

@Test
public void testAddAndRemoveNodes() throws InterruptedException {
    // allow river to start
    Thread.sleep(200);
    String name = UUID.randomUUID().toString();
    // add node to neo4j
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("name", name);
    Transaction tx = db.beginTx();
    ArrayList<String> labels = new ArrayList<String>();
    org.neo4j.graphdb.Node n = db.createNode(map, labels);
    tx.success();
    CountResponse resp = null;
    int k = 0;
    while (k++ < 100) {
        // time for poller to index
        Thread.sleep(1000);
        refreshIndex();
        logger.debug("Count request [index={}, type={}, name={}]", new Object[] { index, type, name });
        resp = node.client().count(countRequest(index).types(type).source(queryString(name).defaultField("name").toString())).actionGet();
        if (1 == resp.getCount())
            break;
    }
    assertEquals(1, resp.getCount());
    db.remove(n);
    k = 0;
    while (k++ < 100) {
        // time for poller to index
        Thread.sleep(1000);
        refreshIndex();
        logger.debug("Count request [index={}, type={}, name={}]", new Object[] { index, type, name });
        resp = node.client().count(countRequest(index).types(type).source(queryString(name).defaultField("name").toString())).actionGet();
        if (0 == resp.getCount())
            break;
    }
    assertEquals(0, resp.getCount());
    shutdown();
}
Also used : Transaction(org.neo4j.graphdb.Transaction) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CountResponse(org.elasticsearch.action.count.CountResponse) QueryBuilders.queryString(org.elasticsearch.index.query.QueryBuilders.queryString) Test(org.junit.Test)

Example 3 with CountResponse

use of org.elasticsearch.action.count.CountResponse in project storm by apache.

the class EsIndexBoltTest method testEsIndexBolt.

@Test
public void testEsIndexBolt() throws Exception {
    String index = "index1";
    String type = "type1";
    Tuple tuple = createTestTuple(index, type);
    bolt.execute(tuple);
    verify(outputCollector).ack(tuple);
    node.client().admin().indices().prepareRefresh(index).execute().actionGet();
    CountResponse resp = node.client().prepareCount(index).setQuery(new TermQueryBuilder("_type", type)).execute().actionGet();
    Assert.assertEquals(1, resp.getCount());
}
Also used : CountResponse(org.elasticsearch.action.count.CountResponse) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) Tuple(org.apache.storm.tuple.Tuple) IntegrationTest(org.apache.storm.testing.IntegrationTest) Test(org.junit.Test)

Example 4 with CountResponse

use of org.elasticsearch.action.count.CountResponse in project rssriver by dadoonet.

the class RssRiverAllParametersTest method existSomeDocs.

private void existSomeDocs(final String index, final String source) throws InterruptedException {
    // We wait up to 5 seconds before considering a failing test
    assertThat("Some documents should exist...", awaitBusy(new Predicate<Object>() {

        @Override
        public boolean apply(Object o) {
            QueryBuilder query;
            if (source == null) {
                query = QueryBuilders.matchAllQuery();
            } else {
                query = QueryBuilders.queryString(source).defaultField("feedname");
            }
            CountResponse response = client().prepareCount(index).setQuery(query).execute().actionGet();
            return response.getCount() > 0;
        }
    }, 10, TimeUnit.SECONDS), equalTo(true));
}
Also used : CountResponse(org.elasticsearch.action.count.CountResponse) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Predicate(org.elasticsearch.common.base.Predicate)

Example 5 with CountResponse

use of org.elasticsearch.action.count.CountResponse in project elasticsearch-river-rabbitmq by elastic.

the class RabbitMQIntegrationTest method launchTest.

private void launchTest(XContentBuilder river, final int numMessages, final int numDocsPerMessage, InjectorHook injectorHook, boolean delete, boolean update) throws Exception {
    final String dbName = getDbName();
    logger.info(" --> create index [{}]", dbName);
    try {
        client().admin().indices().prepareDelete(dbName).get();
    } catch (IndexMissingException e) {
    // No worries.
    }
    try {
        createIndex(dbName);
    } catch (IndexMissingException e) {
    // No worries.
    }
    ensureGreen(dbName);
    logger.info("  -> Checking rabbitmq running");
    // We try to connect to RabbitMQ.
    // If it's not launched, we don't fail the test but only log it
    Channel channel = null;
    Connection connection = null;
    try {
        logger.info(" --> connecting to rabbitmq");
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        factory.setPort(AMQP.PROTOCOL.PORT);
        connection = factory.newConnection();
    } catch (ConnectException ce) {
        throw new Exception("RabbitMQ service is not launched on localhost:" + AMQP.PROTOCOL.PORT + ". Can not start Integration test. " + "Launch `rabbitmq-server`.", ce);
    }
    try {
        logger.info("  -> Creating [{}] channel", dbName);
        channel = connection.createChannel();
        logger.info("  -> Creating queue [{}]", dbName);
        channel.queueDeclare(getDbName(), true, false, false, null);
        // We purge the queue in case of something is remaining there
        logger.info("  -> Purging [{}] channel", dbName);
        channel.queuePurge(getDbName());
        logger.info("  -> Put [{}] messages with [{}] documents each = [{}] docs", numMessages, numDocsPerMessage, numMessages * numDocsPerMessage);
        final Set<String> removed = new HashSet<String>();
        int nbUpdated = 0;
        for (int i = 0; i < numMessages; i++) {
            StringBuffer message = new StringBuffer();
            for (int j = 0; j < numDocsPerMessage; j++) {
                if (logger.isTraceEnabled()) {
                    logger.trace("  -> Indexing document [{}] - [{}][{}]", i + "_" + j, i, j);
                }
                message.append("{ \"index\" : { \"_index\" : \"" + dbName + "\", \"_type\" : \"typex\", \"_id\" : \"" + i + "_" + j + "\" } }\n");
                message.append("{ \"field\" : \"" + i + "_" + j + "\",\"numeric\" : " + i * j + " }\n");
                // Sometime we update a document
                if (update && rarely()) {
                    String id = between(0, i) + "_" + between(0, j);
                    // We can only update if it has not been removed :)
                    if (!removed.contains(id)) {
                        logger.debug("  -> Updating document [{}] - [{}][{}]", id, i, j);
                        message.append("{ \"update\" : { \"_index\" : \"" + dbName + "\", \"_type\" : \"typex\", \"_id\" : \"" + id + "\" } }\n");
                        message.append("{ \"doc\": { \"foo\" : \"bar\", \"field2\" : \"" + i + "_" + j + "\" }}\n");
                        nbUpdated++;
                    }
                }
                // Sometime we delete a document
                if (delete && rarely()) {
                    String id = between(0, i) + "_" + between(0, j);
                    if (!removed.contains(id)) {
                        logger.debug("  -> Removing document [{}] - [{}][{}]", id, i, j);
                        message.append("{ \"delete\" : { \"_index\" : \"" + dbName + "\", \"_type\" : \"typex\", \"_id\" : \"" + id + "\" } }\n");
                        removed.add(id);
                    }
                }
            }
            channel.basicPublish("", dbName, null, message.toString().getBytes(StandardCharsets.UTF_8));
        }
        logger.info("  -> We removed [{}] docs and updated [{}] docs", removed.size(), nbUpdated);
        if (injectorHook != null) {
            logger.info("  -> Injecting extra data");
            injectorHook.inject();
        }
        logger.info(" --> create river");
        IndexResponse indexResponse = index("_river", dbName, "_meta", river);
        assertTrue(indexResponse.isCreated());
        logger.info("-->  checking that river [{}] was created", dbName);
        assertThat(awaitBusy(new Predicate<Object>() {

            public boolean apply(Object obj) {
                GetResponse response = client().prepareGet(RiverIndexName.Conf.DEFAULT_INDEX_NAME, dbName, "_status").get();
                return response.isExists();
            }
        }, 5, TimeUnit.SECONDS), equalTo(true));
        // Check that docs are still processed by the river
        logger.info(" --> waiting for expected number of docs: [{}]", numDocsPerMessage * numMessages - removed.size());
        assertThat(awaitBusy(new Predicate<Object>() {

            public boolean apply(Object obj) {
                try {
                    refresh();
                    int expected = numDocsPerMessage * numMessages - removed.size();
                    CountResponse response = client().prepareCount(dbName).get();
                    logger.debug("  -> got {} docs, expected {}", response.getCount(), expected);
                    return response.getCount() == expected;
                } catch (IndexMissingException e) {
                    return false;
                }
            }
        }, 20, TimeUnit.SECONDS), equalTo(true));
    } finally {
        if (channel != null && channel.isOpen()) {
            channel.close();
        }
        if (connection != null && connection.isOpen()) {
            connection.close();
        }
        // Deletes the river
        GetResponse response = client().prepareGet(RiverIndexName.Conf.DEFAULT_INDEX_NAME, dbName, "_status").get();
        if (response.isExists()) {
            client().prepareDelete(RiverIndexName.Conf.DEFAULT_INDEX_NAME, dbName, "_meta").get();
            client().prepareDelete(RiverIndexName.Conf.DEFAULT_INDEX_NAME, dbName, "_status").get();
        }
        assertThat(awaitBusy(new Predicate<Object>() {

            public boolean apply(Object obj) {
                GetResponse response = client().prepareGet(RiverIndexName.Conf.DEFAULT_INDEX_NAME, dbName, "_status").get();
                return response.isExists();
            }
        }, 5, TimeUnit.SECONDS), equalTo(false));
    }
}
Also used : Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) CountResponse(org.elasticsearch.action.count.CountResponse) IndexMissingException(org.elasticsearch.indices.IndexMissingException) GetResponse(org.elasticsearch.action.get.GetResponse) ConnectException(java.net.ConnectException) IndexMissingException(org.elasticsearch.indices.IndexMissingException) Predicate(org.elasticsearch.common.base.Predicate) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) IndexResponse(org.elasticsearch.action.index.IndexResponse) ConnectException(java.net.ConnectException) HashSet(java.util.HashSet)

Aggregations

CountResponse (org.elasticsearch.action.count.CountResponse)5 Predicate (org.elasticsearch.common.base.Predicate)2 Test (org.junit.Test)2 Channel (com.rabbitmq.client.Channel)1 Connection (com.rabbitmq.client.Connection)1 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)1 ConnectException (java.net.ConnectException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 IntegrationTest (org.apache.storm.testing.IntegrationTest)1 Tuple (org.apache.storm.tuple.Tuple)1 GetResponse (org.elasticsearch.action.get.GetResponse)1 IndexResponse (org.elasticsearch.action.index.IndexResponse)1 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)1 QueryBuilders.queryString (org.elasticsearch.index.query.QueryBuilders.queryString)1 TermQueryBuilder (org.elasticsearch.index.query.TermQueryBuilder)1 IndexMissingException (org.elasticsearch.indices.IndexMissingException)1 Transaction (org.neo4j.graphdb.Transaction)1