Last active
December 14, 2015 12:48
-
-
Save bitwjg/5088703 to your computer and use it in GitHub Desktop.
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
package com.msra.km.jingang; | |
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 { | |
public static ESLogger logger = Loggers.getLogger(BitwjgTest.class); | |
public static 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; | |
} | |
public static void main(String[] args) throws Exception { | |
Node node = nodeBuilder().clusterName("MSRA-KM").node().start(); | |
Client client = node.client(); | |
// create index "test" | |
client.admin().indices().create(new CreateIndexRequest("mytest")); | |
// wait for healthy cluster should be added here... | |
client.admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet(); | |
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("mytest", "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("mytest") | |
.setQuery(qb) | |
.execute() | |
.actionGet(); | |
logger.info("there are " + searchResponse.getHits().totalHits() + " hits in this search"); | |
System.out.println("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")); | |
System.out.println("hit docid = {}" + hits.getAt(i).getSource().get("doc_id")); | |
} | |
// delete index "test" | |
client.admin().indices().delete(new DeleteIndexRequest("mytest")); | |
node.stop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment