Skip to content

Instantly share code, notes, and snippets.

Created October 21, 2011 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1305141 to your computer and use it in GitHub Desktop.
Save anonymous/1305141 to your computer and use it in GitHub Desktop.
package test;
import java.io.IOException;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.action.count.CountResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
import de.svenjacobs.loremipsum.LoremIpsum;
public class MappingTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MappingTest m = new MappingTest();
try {
m.doTest();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static final String CLUSTER_NAME_PROP_KEY = "cluster.name";
public static final String DEFAULT_CLUSTER_NAME = "elasticsearch";
public static final String NUMBER_OF_SHARDS_PROP_KEY = "number_of_shards";
public static final String NUMBER_OF_REPLICAS_PROP_KEY = "number_of_replicas";
enum CONNECTION_MODE { NODE, TRANSPORT_CLIENT};
Node node;
Client client;
String globalIndexname;
int numberOfReplicas;
int numberOfShards;
String typeName;
private void doTest() throws InterruptedException{
//FIRST RUN WITH DOCCUMENTS ADDITION
initialize();
createIndex(true);
createMapping();
putDocuments();
System.out.println("im a lazy index ....");
Thread.sleep(10000);
countDocs();
closeClient();
//1001 docs in the index
//SECOND RUN WITHOUT DOCUMENTS ADDITION
initClient();
createMapping();
System.out.println("im still sleepy ....");
Thread.sleep(10000);
countDocs();
closeClient();
//nomore docs in the index
}
private void initialize(){
initClient();
this.globalIndexname = "foobar";
this.numberOfReplicas = 0;
this.numberOfShards = 1;
this.typeName = "somethingclass";
}
private void initClient(){
this.node = NodeBuilder.nodeBuilder().client(true).node(); //clientOnly
this.client = node.client();
}
private void closeClient(){
this.client.close();
}
private void createIndex(boolean forceRecreate){
boolean exists = getClient().admin().indices().prepareExists(globalIndexname).execute().actionGet().exists();
if(forceRecreate && exists){
getClient().admin().indices().prepareDelete(globalIndexname).execute().actionGet();
exists = false;
}
if( !exists ){
Map<String,Object> settings = new HashMap<String, Object>();
settings.put(NUMBER_OF_REPLICAS_PROP_KEY, this.numberOfReplicas);
settings.put(NUMBER_OF_SHARDS_PROP_KEY, this.numberOfShards);
System.out.println(" >> Index did not exist , creating it ");
getClient().admin().indices().prepareCreate(globalIndexname)
.setSettings(settings)
.execute().actionGet();
getClient().admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet();
System.out.println(" >> index should have been created ");
}
}
public void createMapping(){
String mappingDef = "{"+
" \"somethingclass\" : {" +
"\"dynamic\" : false,"+
"\"properties\" : {"+
"\"someProp1\" : {"+
"\"index_name\" : \"monField1\","+
"\"type\" : \"string\","+
"\"store\" : \"no\","+
"\"index\" : \"analyzed\","+
"\"term_vector\" : \"no\","+
"\"boost\" : 2.0,"+
"\"analyzer\" : \"standard\""+
"},"+ //someProp1
"\"uniqueRandom\" : {"+
"\"index_name\" : \"uniqueRandom\","+
"\"type\" : \"string\","+
"\"store\" : \"no\","+
"\"index\" : \"not_analyzed\","+
"\"term_vector\" : \"no\","+
"\"boost\" : 1.0,"+
"\"analyzer\" : \"standard\""+
"}"+ //uniqueRandom
"}"+//props
"}"+//somethingClass
"}"; //mappingDef
System.out.println(" ----- > putMapping ");
try {
System.out.println(" >> taking index down --> " + globalIndexname);
getClient().admin().indices().prepareClose(globalIndexname).execute().actionGet();
getClient().admin().indices()
.preparePutMapping().setType(typeName)
.setSource(mappingDef).execute().actionGet();
System.out.println( " >> waiting for green status .... >> ");
getClient().admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet();
//take index back up
System.out.println(" >> reopening index " + globalIndexname);
getClient().admin().indices().prepareOpen(globalIndexname).execute().actionGet();
} catch (ElasticSearchException e) {
e.printStackTrace();
}
}
private void putDocuments(){
System.out.println(" ADDING DOCUMENTS ");
SecureRandom random = new SecureRandom();
Random r = new Random();
for(int i=0;i<1000;i++){
//Generate random stuff
int words = r.nextInt(40);
int start = r.nextInt(40);
LoremIpsum lorem = new LoremIpsum();
String w1 = lorem.getWords(words, start);
words = r.nextInt(40);
start = r.nextInt(40);
String w2 = lorem.getWords(words, start);
String unique = new BigInteger(130, random).toString(32);
String docStr = "{"+
"\"className\" : \"test.Something\","+
"\"someProp1\" : \""+w1+"\","+
"\"superDuperProp\" : \""+w2+"\","+
"\"uniqueRandom\" : \""+unique+"\""+
"}";//docstr
client.prepareIndex(globalIndexname, this.typeName) //, idVal.toString()
.setSource(docStr)
.execute().actionGet();
}
System.out.println("random stuff added");
//add just one additional doc to perform some real search in the middle of all the sh...
String docStr = "{"+
"\"className\" : \"test.Something\","+
"\"someProp1\" : \"Once upon a time there was a pricess bla bla bla ...\","+
"\"superDuperProp\" : \"foooo this is not indexed ... \","+
"\"uniqueRandom\" : \"ABCDEFG\""+
"}";//docstr
client.prepareIndex(globalIndexname, this.typeName) //, idVal.toString()
.setSource(docStr)
.execute().actionGet();
System.out.println("all docs added");
}
private void countDocs(){
MatchAllQueryBuilder qb = QueryBuilders.matchAllQuery();
SearchResponse sr = client.prepareSearch(globalIndexname).setQuery(qb).execute().actionGet();
// CountResponse cr = client.prepareCount(this.globalIndexname).setQuery(qb).execute().actionGet();
System.out.println("WE CURRENTLY HAVE " +sr.getHits().getTotalHits()+" DOCUMENTS IN OUR INDEX");
}
public Client getClient() {
return client;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment