-
-
Save caravone/b204814d5f26b7040cc6 to your computer and use it in GitHub Desktop.
Unit test with embedded elasticsearch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Test | |
import static junit.framework.Assert.* | |
import org.elasticsearch.client.Client | |
import org.elasticsearch.node.Node | |
import org.junit.Before | |
import org.elasticsearch.node.NodeBuilder | |
import org.elasticsearch.action.count.CountRequest | |
import org.junit.After | |
import static org.elasticsearch.client.Requests.* | |
import static org.elasticsearch.index.query.xcontent.TermQueryBuilder.* | |
import static org.elasticsearch.index.query.xcontent.QueryBuilders.* | |
import org.elasticsearch.common.io.stream.Streamables | |
import org.elasticsearch.common.io.stream.DataOutputStreamOutput | |
import org.elasticsearch.action.search.SearchResponse | |
import org.elasticsearch.action.WriteConsistencyLevel; | |
/** | |
* Quick test to prove that we can set up a local elasticsearch node for unit testing | |
* | |
* @author Curtis Caravone | |
* @since 2/28/11 at 1:55 PM | |
*/ | |
class ElasticSearchLocalNodeTest { | |
Node node | |
Client client | |
String index | |
@Before | |
void setup() { | |
node = NodeBuilder.nodeBuilder().local(true).node() | |
client = node.client() | |
index = "test" | |
println "Deleting index $index" | |
try { | |
client.admin().indices().delete(deleteIndexRequest(index)).actionGet() | |
} catch (Exception e) { | |
System.err.println("Warning: " + e); | |
} | |
println "Recreating index $index" | |
client.admin().indices().create(createIndexRequest(index)).actionGet() | |
} | |
@After | |
void teardown() { | |
node.stop() | |
} | |
@Test | |
void testSimpleSearch() { | |
def doc = [name: "curtis", age: 100] | |
def query = termQuery("name", "curtis") | |
client.admin().indices().refresh(refreshRequest(index)).actionGet() | |
assertEquals 0, client.count(countRequest(index).query(query)).get().count | |
client.prepareIndex(index, "doc", "doc1") | |
.setSource(doc) | |
.setConsistencyLevel(WriteConsistencyLevel.QUORUM) | |
.execute() | |
.actionGet() | |
client.admin().indices().refresh(refreshRequest(index)).actionGet() | |
SearchResponse response = client.prepareSearch(index).setQuery(query).execute().actionGet() | |
println response | |
assertEquals([doc], response.hits.hits*.source) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment