Guest User

Untitled

a guest
Aug 17th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.83 KB | None | 0 0
  1. package test;
  2.  
  3. import java.io.IOException;
  4. import java.math.BigInteger;
  5. import java.security.SecureRandom;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import java.util.Random;
  9.  
  10. import org.elasticsearch.ElasticSearchException;
  11. import org.elasticsearch.action.count.CountResponse;
  12. import org.elasticsearch.action.search.SearchResponse;
  13. import org.elasticsearch.client.Client;
  14. import org.elasticsearch.common.xcontent.XContentBuilder;
  15. import org.elasticsearch.common.xcontent.XContentFactory;
  16. import org.elasticsearch.index.query.MatchAllQueryBuilder;
  17. import org.elasticsearch.index.query.QueryBuilders;
  18. import org.elasticsearch.node.Node;
  19. import org.elasticsearch.node.NodeBuilder;
  20.  
  21. import de.svenjacobs.loremipsum.LoremIpsum;
  22.  
  23. public class MappingTest {
  24.  
  25. /**
  26. * @param args
  27. */
  28. public static void main(String[] args) {
  29. // TODO Auto-generated method stub
  30.  
  31. MappingTest m = new MappingTest();
  32.  
  33. try {
  34. m.doTest();
  35. } catch (InterruptedException e) {
  36. // TODO Auto-generated catch block
  37. e.printStackTrace();
  38. }
  39.  
  40.  
  41. }
  42.  
  43.  
  44. public static final String CLUSTER_NAME_PROP_KEY = "cluster.name";
  45.  
  46. public static final String DEFAULT_CLUSTER_NAME = "elasticsearch";
  47.  
  48. public static final String NUMBER_OF_SHARDS_PROP_KEY = "number_of_shards";
  49. public static final String NUMBER_OF_REPLICAS_PROP_KEY = "number_of_replicas";
  50.  
  51.  
  52. enum CONNECTION_MODE { NODE, TRANSPORT_CLIENT};
  53.  
  54. Node node;
  55. Client client;
  56. String globalIndexname;
  57. int numberOfReplicas;
  58. int numberOfShards;
  59. String typeName;
  60.  
  61. private void doTest() throws InterruptedException{
  62.  
  63. //FIRST RUN WITH DOCCUMENTS ADDITION
  64. initialize();
  65.  
  66.  
  67.  
  68. createIndex(true);
  69. createMapping();
  70. putDocuments();
  71.  
  72. System.out.println("im a lazy index ....");
  73. Thread.sleep(10000);
  74. countDocs();
  75. closeClient();
  76.  
  77. //1001 docs in the index
  78.  
  79.  
  80.  
  81. //SECOND RUN WITHOUT DOCUMENTS ADDITION
  82. initClient();
  83. createMapping();
  84.  
  85. System.out.println("im still sleepy ....");
  86. Thread.sleep(10000);
  87. countDocs();
  88. closeClient();
  89.  
  90. //nomore docs in the index
  91.  
  92. }
  93.  
  94. private void initialize(){
  95.  
  96. initClient();
  97.  
  98. this.globalIndexname = "foobar";
  99. this.numberOfReplicas = 0;
  100. this.numberOfShards = 1;
  101. this.typeName = "somethingclass";
  102. }
  103.  
  104. private void initClient(){
  105. this.node = NodeBuilder.nodeBuilder().client(true).node(); //clientOnly
  106. this.client = node.client();
  107. }
  108.  
  109. private void closeClient(){
  110. this.client.close();
  111. }
  112.  
  113.  
  114. private void createIndex(boolean forceRecreate){
  115.  
  116.  
  117. boolean exists = getClient().admin().indices().prepareExists(globalIndexname).execute().actionGet().exists();
  118. if(forceRecreate && exists){
  119. getClient().admin().indices().prepareDelete(globalIndexname).execute().actionGet();
  120. exists = false;
  121. }
  122.  
  123.  
  124.  
  125. if( !exists ){
  126.  
  127. Map<String,Object> settings = new HashMap<String, Object>();
  128. settings.put(NUMBER_OF_REPLICAS_PROP_KEY, this.numberOfReplicas);
  129. settings.put(NUMBER_OF_SHARDS_PROP_KEY, this.numberOfShards);
  130.  
  131.  
  132. System.out.println(" >> Index did not exist , creating it ");
  133. getClient().admin().indices().prepareCreate(globalIndexname)
  134. .setSettings(settings)
  135. .execute().actionGet();
  136. getClient().admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet();
  137. System.out.println(" >> index should have been created ");
  138. }
  139. }
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146. public void createMapping(){
  147.  
  148. String mappingDef = "{"+
  149. " \"somethingclass\" : {" +
  150. "\"dynamic\" : false,"+
  151. "\"properties\" : {"+
  152. "\"someProp1\" : {"+
  153. "\"index_name\" : \"monField1\","+
  154. "\"type\" : \"string\","+
  155. "\"store\" : \"no\","+
  156. "\"index\" : \"analyzed\","+
  157. "\"term_vector\" : \"no\","+
  158. "\"boost\" : 2.0,"+
  159. "\"analyzer\" : \"standard\""+
  160. "},"+ //someProp1
  161. "\"uniqueRandom\" : {"+
  162. "\"index_name\" : \"uniqueRandom\","+
  163. "\"type\" : \"string\","+
  164. "\"store\" : \"no\","+
  165. "\"index\" : \"not_analyzed\","+
  166. "\"term_vector\" : \"no\","+
  167. "\"boost\" : 1.0,"+
  168. "\"analyzer\" : \"standard\""+
  169. "}"+ //uniqueRandom
  170. "}"+//props
  171. "}"+//somethingClass
  172. "}"; //mappingDef
  173.  
  174.  
  175.  
  176. System.out.println(" ----- > putMapping ");
  177. try {
  178.  
  179. System.out.println(" >> taking index down --> " + globalIndexname);
  180. getClient().admin().indices().prepareClose(globalIndexname).execute().actionGet();
  181.  
  182. getClient().admin().indices()
  183. .preparePutMapping().setType(typeName)
  184. .setSource(mappingDef).execute().actionGet();
  185.  
  186. System.out.println( " >> waiting for green status .... >> ");
  187. getClient().admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet();
  188.  
  189. //take index back up
  190. System.out.println(" >> reopening index " + globalIndexname);
  191. getClient().admin().indices().prepareOpen(globalIndexname).execute().actionGet();
  192.  
  193.  
  194. } catch (ElasticSearchException e) {
  195. e.printStackTrace();
  196. }
  197.  
  198.  
  199. }
  200.  
  201.  
  202.  
  203. private void putDocuments(){
  204.  
  205. System.out.println(" ADDING DOCUMENTS ");
  206.  
  207. SecureRandom random = new SecureRandom();
  208. Random r = new Random();
  209. for(int i=0;i<1000;i++){
  210.  
  211. //Generate random stuff
  212. int words = r.nextInt(40);
  213. int start = r.nextInt(40);
  214. LoremIpsum lorem = new LoremIpsum();
  215. String w1 = lorem.getWords(words, start);
  216.  
  217.  
  218. words = r.nextInt(40);
  219. start = r.nextInt(40);
  220. String w2 = lorem.getWords(words, start);
  221.  
  222. String unique = new BigInteger(130, random).toString(32);
  223.  
  224.  
  225.  
  226.  
  227. String docStr = "{"+
  228. "\"className\" : \"test.Something\","+
  229. "\"someProp1\" : \""+w1+"\","+
  230. "\"superDuperProp\" : \""+w2+"\","+
  231. "\"uniqueRandom\" : \""+unique+"\""+
  232. "}";//docstr
  233.  
  234.  
  235.  
  236. client.prepareIndex(globalIndexname, this.typeName) //, idVal.toString()
  237. .setSource(docStr)
  238. .execute().actionGet();
  239.  
  240.  
  241. }
  242.  
  243. System.out.println("random stuff added");
  244.  
  245. //add just one additional doc to perform some real search in the middle of all the sh...
  246.  
  247. String docStr = "{"+
  248. "\"className\" : \"test.Something\","+
  249. "\"someProp1\" : \"Once upon a time there was a pricess bla bla bla ...\","+
  250. "\"superDuperProp\" : \"foooo this is not indexed ... \","+
  251. "\"uniqueRandom\" : \"ABCDEFG\""+
  252. "}";//docstr
  253.  
  254. client.prepareIndex(globalIndexname, this.typeName) //, idVal.toString()
  255. .setSource(docStr)
  256. .execute().actionGet();
  257.  
  258.  
  259. System.out.println("all docs added");
  260. }
  261.  
  262.  
  263.  
  264. private void countDocs(){
  265.  
  266. MatchAllQueryBuilder qb = QueryBuilders.matchAllQuery();
  267. SearchResponse sr = client.prepareSearch(globalIndexname).setQuery(qb).execute().actionGet();
  268. // CountResponse cr = client.prepareCount(this.globalIndexname).setQuery(qb).execute().actionGet();
  269. System.out.println("WE CURRENTLY HAVE " +sr.getHits().getTotalHits()+" DOCUMENTS IN OUR INDEX");
  270. }
  271.  
  272.  
  273. public Client getClient() {
  274. return client;
  275. }
  276.  
  277. }
Add Comment
Please, Sign In to add comment