Skip to content

Instantly share code, notes, and snippets.

@jprante
Last active Dec 14, 2015
Embed
What would you like to do?
Java code example for bitwijg
package org.elasticsearch.test;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.node.Node;
import org.elasticsearch.search.SearchHits;
import org.testng.annotations.Test;
import java.io.IOException;
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
public class BitwjgTest {
private final ESLogger logger = Loggers.getLogger(BitwjgTest.class);
private XContentBuilder makeJson(int i) throws IOException {
XContentBuilder xb = JsonXContent.contentBuilder()
.startObject()
.field("stream_id", i)
.field("source", "source")
.field("doc_id", i)
.startObject("stream_time")
.field("epoch_ticks", "epoch_ticks")
.field("zulu_timestamp", "zulu_timestamp")
.endObject()
.startObject("body")
.field("cleansed", "steve ballmer gaylor")
.field("ner", "body_ner_text")
.endObject()
.endObject();
return xb;
}
@Test
public void test() throws Exception {
Node node = nodeBuilder().clusterName("MSRA-KM").node().start();
Client client = node.client();
// create index "test"
client.admin().indices().create(new CreateIndexRequest("test"));
// wait for healthy cluster should be added here...
for (int i = 0; i < 2; i++) {
//new a bulk request, for every folder, create an index once
BulkRequestBuilder bulkRequest = client.prepareBulk();
XContentBuilder xb = makeJson(i);
bulkRequest.add(client.prepareIndex("test", "kba").setSource(xb));
BulkResponse response = bulkRequest.execute().actionGet();
if (response.hasFailures()) {
logger.error("bulk index doc error: " + response.buildFailureMessage());
}
}
client.admin().indices().refresh(new RefreshRequest()).actionGet();
QueryBuilder qb = QueryBuilders
.boolQuery()
.should(matchQuery("cleansed", "steve"))
.should(matchQuery("cleansed", "ballmer"))
.should(matchQuery("cleansed", "Gaylor"));
SearchResponse searchResponse = client.prepareSearch("test")
.setQuery(qb)
.execute()
.actionGet();
logger.info("there are " + searchResponse.getHits().totalHits() + " hits in this search");
SearchHits hits = searchResponse.getHits();
for (int i = 0; i < hits.totalHits(); i++) {
logger.info("hit docid = {}", hits.getAt(i).getSource().get("doc_id"));
}
// delete index "test"
client.admin().indices().delete(new DeleteIndexRequest("test"));
node.stop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment