/clients/java/src/main/java/com/senseidb/search/client/req/SenseiClientRequest.java

https://bitbucket.org/icosplays/sensei · Java · 309 lines · 209 code · 53 blank · 47 comment · 9 complexity · b79587e30f1a7e13caf00bf92c6277bd MD5 · raw file

  1. package com.senseidb.search.client.req;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import org.json.JSONException;
  8. import org.json.JSONObject;
  9. import com.senseidb.search.client.json.CustomJsonHandler;
  10. import com.senseidb.search.client.json.JsonField;
  11. import com.senseidb.search.client.req.filter.Filter;
  12. import com.senseidb.search.client.req.filter.FilterJsonHandler;
  13. import com.senseidb.search.client.req.query.Query;
  14. import com.senseidb.search.client.req.query.QueryJsonHandler;
  15. import com.senseidb.search.client.req.relevance.Relevance;
  16. /**
  17. * The sensei request object, that is used to send the Sensei query to the
  18. * server
  19. *
  20. */
  21. public class SenseiClientRequest {
  22. /* *//**
  23. * @see com.senseidb.search.client.req.Paging
  24. */
  25. /*
  26. * private Paging paging;
  27. */
  28. private Integer size;
  29. private Integer from;
  30. /**
  31. *
  32. * @see com.senseidb.search.client.req.GroupBy
  33. *
  34. */
  35. private GroupBy groupBy;
  36. private List<Selection> selections = new ArrayList<Selection>();
  37. @CustomJsonHandler(value = QueryJsonHandler.class)
  38. private Query query;
  39. /**
  40. * Initializing parameters for runtime facet handlers: a map that contains the
  41. * initializing parameters that are needed by all runtime facet handlers
  42. */
  43. private Map<String, Map<String, FacetInit>> facetInit = new HashMap<String, Map<String, FacetInit>>();
  44. private List<Object> sort = new ArrayList<Object>();
  45. private Map<String, Facet> facets = new HashMap<String, Facet>();
  46. /**
  47. * Flag indicating whether stored fields are to be fetched
  48. */
  49. private boolean fetchStored;
  50. private List<String> fieldsToFetch;
  51. private List<String> termVectors = new ArrayList<String>();
  52. /**
  53. * shards of the index to be searched
  54. */
  55. private List<Integer> partitions = new ArrayList<Integer>();
  56. /**
  57. * Flag indicating whether explanation information should be returned
  58. */
  59. private boolean explain;
  60. /**
  61. * the field value used for routing
  62. */
  63. private String routeParam;
  64. @CustomJsonHandler(value = FilterJsonHandler.class)
  65. private Filter filter;
  66. /**
  67. * Allows template substitution on the server. The template occurrence in
  68. * other places should begin with the dollar sign<br>
  69. * Example: <br>
  70. * { { "query_string" : { "query" : "$color1 or $color2", boost :
  71. * "$customBoost" }, { templateMapping {color1:"red", color2:"blue",
  72. * customBoost : 1.0}} } <br>
  73. * will produce<br>
  74. * { { "query_string" : { "query" : "red or blue, boost : 1.0 }
  75. *
  76. * } on the server
  77. */
  78. private Map<String, Object> templateMapping;
  79. private MapReduce mapReduce;
  80. private RequestMetadata meta;
  81. public static class Builder {
  82. private SenseiClientRequest request = new SenseiClientRequest();
  83. public Builder paging(int size, int offset) {
  84. request.size = size;
  85. request.from = offset;
  86. return this;
  87. }
  88. public Builder fetchStored(boolean fetchStored) {
  89. request.fetchStored = fetchStored;
  90. return this;
  91. }
  92. public Builder setFieldsToFetch(List<String> fieldsToFetch) {
  93. request.fieldsToFetch = fieldsToFetch;
  94. return this;
  95. }
  96. public Builder partitions(List<Integer> partitions) {
  97. request.partitions = partitions;
  98. return this;
  99. }
  100. public Builder explain(boolean explain) {
  101. request.explain = explain;
  102. return this;
  103. }
  104. public Builder query(Query query) {
  105. request.query = query;
  106. return this;
  107. }
  108. public Builder groupBy(int top, String... columns) {
  109. request.groupBy = new GroupBy(Arrays.asList(columns), top);
  110. return this;
  111. }
  112. public Builder groupBy(List<String> columns, int top) {
  113. request.groupBy = new GroupBy(columns, top);
  114. return this;
  115. }
  116. public Builder addSelection(Selection selection) {
  117. if (selection == null) {
  118. throw new IllegalArgumentException("The selectionContainer should be not null");
  119. }
  120. request.selections.add(selection);
  121. return this;
  122. }
  123. public Builder addFacetInit(String name, Map<String, FacetInit> facetInits) {
  124. request.facetInit.put(name, facetInits);
  125. return this;
  126. }
  127. public Builder showOnlyFields(String...fields ) {
  128. request.meta = new RequestMetadata(Arrays.asList(fields));
  129. return this;
  130. }
  131. /**
  132. * @see com.senseidb.search.client.req.SenseiClientRequest#templateMapping
  133. */
  134. public Builder addTemplateMapping(String name, Object value) {
  135. if (request.templateMapping == null) {
  136. request.templateMapping = new HashMap<String, Object>();
  137. }
  138. request.templateMapping.put(name, value);
  139. return this;
  140. }
  141. public Builder addSort(Sort sort) {
  142. if (sort == null) {
  143. throw new IllegalArgumentException("The sort should be not null");
  144. }
  145. if ("_score".equalsIgnoreCase(sort.getField())) {
  146. request.sort.add("_score");
  147. } else {
  148. try {
  149. request.sort.add(new JSONObject().put(sort.getField(), sort.getOrder().name()));
  150. } catch (JSONException e) {
  151. throw new RuntimeException(e);
  152. }
  153. }
  154. return this;
  155. }
  156. public Builder addTermVector(String term) {
  157. request.termVectors.add(term);
  158. return this;
  159. }
  160. public Builder addFacetInit(String name, String parameter, FacetInit facetInit) {
  161. if (!request.facetInit.containsKey(name)) {
  162. request.facetInit.put(name, new HashMap<String, FacetInit>());
  163. }
  164. request.facetInit.get(name).put(parameter, facetInit);
  165. return this;
  166. }
  167. public Builder addFacet(String name, Facet facet) {
  168. request.facets.put(name, facet);
  169. return this;
  170. }
  171. public Builder routeParam(String routeParam) {
  172. request.routeParam = routeParam;
  173. return this;
  174. }
  175. public Builder filter(Filter filter) {
  176. request.filter = filter;
  177. return this;
  178. }
  179. public Builder mapReduce(String function, Map<String, Object> parameters) {
  180. request.mapReduce = new MapReduce(function, parameters);
  181. return this;
  182. }
  183. public SenseiClientRequest build() {
  184. return request;
  185. }
  186. }
  187. public static Builder builder() {
  188. return new Builder();
  189. }
  190. public Paging getPaging() {
  191. return new Paging(size, from);
  192. }
  193. public GroupBy getGroupBy() {
  194. return groupBy;
  195. }
  196. public List<Selection> getSelections() {
  197. return selections;
  198. }
  199. public Map<String, Map<String, FacetInit>> getFacetInit() {
  200. return facetInit;
  201. }
  202. public List<Object> getSorts() {
  203. return sort;
  204. }
  205. public Map<String, Facet> getFacets() {
  206. return facets;
  207. }
  208. public boolean isFetchStored() {
  209. return fetchStored;
  210. }
  211. public List<String> getTermVectors() {
  212. return termVectors;
  213. }
  214. public List<String> getFieldsToFetch() {
  215. return fieldsToFetch;
  216. }
  217. public List<Integer> getPartitions() {
  218. return partitions;
  219. }
  220. public boolean isExplain() {
  221. return explain;
  222. }
  223. public String getRouteParam() {
  224. return routeParam;
  225. }
  226. public Integer getCount() {
  227. return size;
  228. }
  229. public Integer getFrom() {
  230. return from;
  231. }
  232. public Query getQuery() {
  233. return query;
  234. }
  235. public Filter getFilter() {
  236. return filter;
  237. }
  238. public Map<String, Object> getTemplateMapping() {
  239. return templateMapping;
  240. }
  241. public void setQuery(Query query) {
  242. this.query = query;
  243. }
  244. public void setFilter(Filter filter) {
  245. this.filter = filter;
  246. }
  247. public void setSelections(List<Selection> selections) {
  248. this.selections = selections;
  249. }
  250. public void setMapReduce(MapReduce mapReduce){
  251. this.mapReduce = mapReduce;
  252. }
  253. public MapReduce getMapReduce(){
  254. return mapReduce;
  255. }
  256. }