1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
package io.searchbox.core;
import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; import com.google.gson.JsonSyntaxException; import io.searchbox.action.AbstractAction; import io.searchbox.action.AbstractMultiTypeActionBuilder; import io.searchbox.client.config.ElasticsearchVersion; import io.searchbox.core.search.sort.Sort; import io.searchbox.params.SearchType; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Objects;
public class Search extends AbstractAction<SearchResult> { private String query; private List<Sort> sortList; protected List<String> includePatternList; protected List<String> excludePatternList; private final String templateSuffix;
protected Search(Search.Builder builder) { this(builder, ""); }
protected Search(Search.TemplateBuilder templatedBuilder) { this(templatedBuilder, "/template"); }
private Search(Search.Builder builder, String templateSuffix) { super(builder); this.query = builder.query; this.sortList = builder.sortList; this.includePatternList = builder.includePatternList; this.excludePatternList = builder.excludePatternList; this.templateSuffix = templateSuffix; }
public SearchResult createNewElasticSearchResult(String responseBody, int statusCode, String reasonPhrase, Gson gson) { return (SearchResult)this.createNewElasticSearchResult(new SearchResult(gson), responseBody, statusCode, reasonPhrase, gson); }
public String getIndex() { return this.indexName; }
public String getType() { return this.typeName; }
protected String buildURI(ElasticsearchVersion elasticsearchVersion) { return super.buildURI(elasticsearchVersion) + "/_search" + this.templateSuffix; }
public String getPathToResult() { return "hits/hits/_source"; }
public String getRestMethodName() { return "POST"; }
public String getData(Gson gson) { String data; if (this.sortList.isEmpty() && this.includePatternList.isEmpty() && this.excludePatternList.isEmpty()) { data = this.query; } else { JsonObject queryObject = (JsonObject)gson.fromJson(this.query, JsonObject.class); if (queryObject == null) { queryObject = new JsonObject(); }
if (!this.sortList.isEmpty()) { JsonArray sortArray = normalizeSortClause(queryObject); Iterator var5 = this.sortList.iterator();
while(var5.hasNext()) { Sort sort = (Sort)var5.next(); sortArray.add(sort.toJsonObject()); } }
if (!this.includePatternList.isEmpty() || !this.excludePatternList.isEmpty()) { JsonObject sourceObject = normalizeSourceClause(queryObject); addPatternListToSource(sourceObject, "includes", this.includePatternList); addPatternListToSource(sourceObject, "excludes", this.excludePatternList); }
data = gson.toJson(queryObject); }
return data; }
private static JsonArray normalizeSortClause(JsonObject queryObject) { JsonArray sortArray; if (queryObject.has("sort")) { JsonElement sortElement = queryObject.get("sort"); if (sortElement.isJsonArray()) { sortArray = sortElement.getAsJsonArray(); } else if (sortElement.isJsonObject()) { sortArray = new JsonArray(); sortArray.add(sortElement.getAsJsonObject()); } else { if (!sortElement.isJsonPrimitive() || !sortElement.getAsJsonPrimitive().isString()) { throw new JsonSyntaxException("_source must be an array, an object or a string"); }
String sortField = sortElement.getAsString(); sortArray = new JsonArray(); queryObject.add("sort", sortArray); String order; if ("_score".equals(sortField)) { order = "desc"; } else { order = "asc"; }
JsonObject sortOrder = new JsonObject(); sortOrder.add("order", new JsonPrimitive(order)); JsonObject sortDefinition = new JsonObject(); sortDefinition.add(sortField, sortOrder); sortArray.add(sortDefinition); } } else { sortArray = new JsonArray(); }
queryObject.add("sort", sortArray); return sortArray; }
private static JsonObject normalizeSourceClause(JsonObject queryObject) { JsonObject sourceObject; if (queryObject.has("_source")) { JsonElement sourceElement = queryObject.get("_source"); if (sourceElement.isJsonObject()) { sourceObject = sourceElement.getAsJsonObject(); } else if (sourceElement.isJsonArray()) { sourceObject = new JsonObject(); queryObject.add("_source", sourceObject); sourceObject.add("includes", sourceElement.getAsJsonArray()); } else { if (!sourceElement.isJsonPrimitive() || !sourceElement.getAsJsonPrimitive().isBoolean()) { throw new JsonSyntaxException("_source must be an object, an array or a boolean"); }
sourceObject = new JsonObject(); } } else { sourceObject = new JsonObject(); }
queryObject.add("_source", sourceObject); return sourceObject; }
private static void addPatternListToSource(JsonObject sourceObject, String rule, List<String> patternList) { if (!patternList.isEmpty()) { JsonArray ruleArray; if (sourceObject.has(rule)) { ruleArray = sourceObject.get(rule).getAsJsonArray(); } else { ruleArray = new JsonArray(); sourceObject.add(rule, ruleArray); }
Iterator var4 = patternList.iterator();
while(var4.hasNext()) { String pattern = (String)var4.next(); ruleArray.add(pattern); } }
}
public int hashCode() { return Objects.hash(new Object[]{super.hashCode(), this.query, this.sortList, this.includePatternList, this.excludePatternList}); }
public boolean equals(Object obj) { if (obj == null) { return false; } else if (obj == this) { return true; } else if (obj.getClass() != this.getClass()) { return false; } else { Search rhs = (Search)obj; return super.equals(obj) && Objects.equals(this.query, rhs.query) && Objects.equals(this.sortList, rhs.sortList) && Objects.equals(this.includePatternList, rhs.includePatternList) && Objects.equals(this.excludePatternList, rhs.excludePatternList); } }
public static class TemplateBuilder extends Search.Builder { public TemplateBuilder(String templatedQuery) { super(templatedQuery); }
public Search build() { return new Search(this); } }
public static class VersionBuilder extends Search.Builder { public VersionBuilder(String query) { super(query); this.setParameter("version", "true"); } }
public static class Builder extends AbstractMultiTypeActionBuilder<Search, Search.Builder> { protected String query; protected List<Sort> sortList = new LinkedList(); protected List<String> includePatternList = new ArrayList(); protected List<String> excludePatternList = new ArrayList();
public Builder(String query) { this.query = query; }
public Search.Builder setSearchType(SearchType searchType) { return (Search.Builder)this.setParameter("search_type", searchType); }
public Search.Builder enableTrackScores() { this.setParameter("track_scores", true); return this; }
public Search.Builder addSort(Sort sort) { this.sortList.add(sort); return this; }
public Search.Builder addSourceExcludePattern(String excludePattern) { this.excludePatternList.add(excludePattern); return this; }
public Search.Builder addSourceIncludePattern(String includePattern) { this.includePatternList.add(includePattern); return this; }
public Search.Builder addSort(Collection<Sort> sorts) { this.sortList.addAll(sorts); return this; }
public Search build() { return new Search(this); } } }
|