Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. {
  2. "test" : {
  3. "general" : {
  4. "properties" : {
  5. "message" : {
  6. "type" : "string"
  7. },
  8. "source" : {
  9. "type" : "string"
  10. }
  11. }
  12. }
  13. }
  14. }
  15.  
  16. {
  17. "test" : {
  18. "general" : {
  19. "properties" : {
  20. "message" : {
  21. "type" : "string",
  22. "index" : "not_analyzed"
  23. },
  24. "source" : {
  25. "type" : "string"
  26. }
  27. }
  28. }
  29. }
  30. }
  31.  
  32. client.admin().indices().prepareCreate("test")
  33. .setSettings(getGrantSettings());
  34.  
  35. static Settings getGrantSettings(){
  36. JSONObject settingSource = new JSONObject();
  37. try{
  38. settingSource.put("mapping", new JSONObject()
  39. .put("message", new JSONObject()
  40. .put("type", "string")
  41. .put("index", "not_analyzed")
  42. ));
  43. } catch (JSONException e){
  44. e.printStackTrace();
  45. }
  46.  
  47.  
  48. Settings set = ImmutableSettings.settingsBuilder()
  49. .loadFromSource(settingSource.toString()).build();
  50. return set;
  51. }
  52.  
  53. XContentBuilder mapping = jsonBuilder()
  54. .startObject()
  55. .startObject("general")
  56. .startObject("properties")
  57. .startObject("message")
  58. .field("type", "string")
  59. .field("index", "not_analyzed")
  60. .endObject()
  61. .startObject("source")
  62. .field("type","string")
  63. .endObject()
  64. .endObject()
  65. .endObject()
  66. .endObject();
  67.  
  68. PutMappingResponse putMappingResponse = client.admin().indices()
  69. .preparePutMapping("test")
  70. .setType("general")
  71. .setSource(mapping)
  72. .execute().actionGet();
  73.  
  74. client.admin().indices().create(new CreateIndexRequest("indexname")).actionGet();
  75.  
  76. PutMappingResponse putMappingResponse = client.admin().indices()
  77. .preparePutMapping("indexname")
  78. .setType("indextype")
  79. .setSource(jsonBuilder().prettyPrint()
  80. .startObject()
  81. .startObject("indextype")
  82. .startObject("properties")
  83. .startObject("country").field("type", "string").field("index", "not_analyzed").endObject()
  84. .endObject()
  85. .endObject()
  86. .endObject())
  87. .execute().actionGet();
  88.  
  89. IndexResponse response1 = client.prepareIndex("indexname", "indextype")
  90. .setSource(buildIndex())
  91. .execute()
  92. .actionGet();
  93.  
  94. // Now "Sri Lanka" considered to be a single country :)
  95. SearchResponse response = client.prepareSearch("indexname"
  96. ).addAggregation(AggregationBuilders.terms("countryfacet").field("country")).setSize(30).execute().actionGet();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement