ElasticRESTClient.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package org.django.acquabooks;
  2. import io.searchbox.client.JestClient;
  3. import io.searchbox.client.JestClientFactory;
  4. import io.searchbox.client.JestResult;
  5. import io.searchbox.client.config.HttpClientConfig;
  6. import io.searchbox.core.*;
  7. import java.io.IOException;
  8. import java.util.List;
  9. import org.django.acquabooks.pojos.Libro;
  10. public class ElasticRESTClient {
  11. private JestClientFactory factory;
  12. private JestClient client;
  13. public static final Integer MAX_RESULTS = 1000;
  14. public static ElasticRESTClient getInstance(String protocol, String host, String port){
  15. ElasticRESTClient r = new ElasticRESTClient();
  16. r.setFactory(new JestClientFactory());
  17. r.getFactory().setHttpClientConfig(new HttpClientConfig
  18. .Builder(protocol + "://" + host + ":" + port)
  19. .multiThreaded(true)
  20. .build());
  21. r.setClient(r.getFactory().getObject());
  22. return r;
  23. }
  24. /**
  25. * @return the factory
  26. */
  27. public JestClientFactory getFactory() {
  28. return factory;
  29. }
  30. /**
  31. * @param factory the factory to set
  32. */
  33. public void setFactory(JestClientFactory factory) {
  34. this.factory = factory;
  35. }
  36. /**
  37. * @return the client
  38. */
  39. public JestClient getClient() {
  40. return client;
  41. }
  42. /**
  43. * @param client the client to set
  44. */
  45. public void setClient(JestClient client) {
  46. this.client = client;
  47. }
  48. public Libro getDetail(String barcode) throws IOException {
  49. Get get = new Get.Builder("acquatorbida", barcode).type("libro").build();
  50. JestResult result = client.execute(get);
  51. if(result.isSucceeded()){
  52. return result.getSourceAsObject(Libro.class);
  53. }
  54. return null;
  55. }
  56. public boolean delete(String barcode) throws IOException {
  57. JestResult result = client.execute(new Delete.Builder(barcode)
  58. .index("acquatorida")
  59. .type("libro")
  60. .build());
  61. return result.isSucceeded();
  62. }
  63. public Boolean index(Libro l) throws IOException {
  64. return this.index(l,Boolean.TRUE);
  65. }
  66. public Boolean index(Libro l, Boolean updateDate) throws IOException {
  67. if(updateDate) {
  68. l.setUltimoAggiornamento(System.currentTimeMillis());
  69. }
  70. Index index = new Index.Builder(l).index("acquatorbida").type("libro").build();
  71. JestResult r = client.execute(index);
  72. return r.isSucceeded();
  73. }
  74. public List<Libro> getAll() throws IOException {
  75. String query = "{" +
  76. "\"query\": {" +
  77. "\"match_all\": {}" +
  78. "}" +
  79. "}";
  80. Search search = new Search.Builder(query)
  81. // multiple index or types can be added.
  82. .addIndex("acquatorbida")
  83. // .setSearchType(SearchType.COUNT)
  84. .setParameter("size", MAX_RESULTS)
  85. .build();
  86. SearchResult result = client.execute(search);
  87. //result.getTotal()
  88. return result.getSourceAsObjectList(Libro.class);
  89. }
  90. public List<Libro> getByPublisher(String publisher) throws IOException {
  91. String query = "{" +
  92. "\"query\": {" +
  93. " \"multi_match\": {" +
  94. "\"query\":" +
  95. "\"" + publisher +"\"," +
  96. "\"fields\":" +
  97. "[ \"editore\", \"tag\" ]" +
  98. " }" +
  99. // "\"match\": {" +
  100. // "\"editore\":\""+publisher+"\""+
  101. // "}" +
  102. "}" +
  103. "}";
  104. Search search = new Search.Builder(query)
  105. // multiple index or types can be added.
  106. .addIndex("acquatorbida").addType("libro")
  107. // .setSearchType(SearchType.COUNT)
  108. .setParameter("size", MAX_RESULTS)
  109. .build();
  110. SearchResult result = client.execute(search);
  111. //result.getTotal()
  112. return result.getSourceAsObjectList(Libro.class);
  113. }
  114. public List<Libro> getVendutoByPublisher(String publisher) throws IOException {
  115. //in range lt è un numero alto (per i volumi di acquatorbida) a caso
  116. String query = "{\"query\" : {" +
  117. " \"bool\" : {" +
  118. " \"must\" : [" +
  119. " {" +
  120. " \"multi_match\": {" +
  121. "\"query\":" +
  122. "\"" + publisher +"\"," +
  123. "\"fields\":" +
  124. "[ \"editore\", \"tag\" ]" +
  125. " }" +
  126. "}," +
  127. // " {" +
  128. // " \"term\" : {" +
  129. // " \"editore\" : \""+ publisher + "\""+
  130. // " }" +
  131. // " }," +
  132. " {" +
  133. " \"range\" : {" +
  134. " \"qv\" : { \"gt\" : 0, \"lt\" : 20000 }" +
  135. " }" +
  136. " }" +
  137. " ]" +
  138. " }" +
  139. "}" +
  140. "}";
  141. Search search = new Search.Builder(query)
  142. // multiple index or types can be added.
  143. .addIndex("acquatorbida").addType("libro")
  144. // .setSearchType(SearchType.COUNT)
  145. .setParameter("size", MAX_RESULTS)
  146. .build();
  147. SearchResult result = client.execute(search);
  148. //result.getTotal()
  149. return result.getSourceAsObjectList(Libro.class);
  150. }
  151. }