Search extends AbstractAction<SearchResult> { @Override public String getData(Gson gson) { String data; if (sortList.isEmpty() && includePatternList.isEmpty() && excludePatternList.isEmpty()) { data = query; } else { JsonObject queryObject = gson.fromJson(query, JsonObject.class); if (queryObject == null) { queryObject = new JsonObject(); } if (!sortList.isEmpty()) { JsonArray sortArray = normalizeSortClause(queryObject); for (Sort sort : sortList) { sortArray.add(sort.toJsonObject()); } } if (!includePatternList.isEmpty() || !excludePatternList.isEmpty()) { JsonObject sourceObject = normalizeSourceClause(queryObject); addPatternListToSource(sourceObject, "includes", includePatternList); addPatternListToSource(sourceObject, "excludes", excludePatternList); } data = gson.toJson(queryObject); } return data; } protected  Search(Builder builder); protected  Search(TemplateBuilder templatedBuilder); private  Search(Builder builder, String templateSuffix); @Override SearchResult createNewElasticSearchResult(String responseBody, int statusCode, String reasonPhrase, Gson gson); String getIndex(); String getType(); @Override String getPathToResult(); @Override String getRestMethodName(); @Override String getData(Gson gson); @Override int hashCode(); @Override boolean equals(Object obj);  }
@Test public void sourceFilteringByQueryTest() { String query = "{\"sort\":[],\"_source\":{\"exclude\":[\"excludeFieldName\"],\"include\":[\"includeFieldName\"]}}"; Action search = new Search.Builder(query).build(); JsonParser parser = new JsonParser(); JsonElement parsed = parser.parse(search.getData(new Gson()).toString()); JsonObject obj = parsed.getAsJsonObject(); JsonObject source = obj.getAsJsonObject("_source"); JsonArray includePattern = source.getAsJsonArray("include"); assertEquals(1, includePattern.size()); assertEquals("includeFieldName", includePattern.get(0).getAsString()); JsonArray excludePattern = source.getAsJsonArray("exclude"); assertEquals(1, excludePattern.size()); assertEquals("excludeFieldName", excludePattern.get(0).getAsString()); }
@Test public void sourceFilteringParamTest() { String query = "{\"query\" : { \"term\" : { \"name\" : \"KangSungJeon\" } }}"; String includePatternItem1 = "SeolaIncludeFieldName"; String includePatternItem2 = "SeohooIncludeFieldName"; String excludePatternItem1 = "SeolaExcludeField.*"; String excludePatternItem2 = "SeohooExcludeField.*"; Action search = new Search.Builder(query) .addSourceIncludePattern(includePatternItem1) .addSourceIncludePattern(includePatternItem2) .addSourceExcludePattern(excludePatternItem1) .addSourceExcludePattern(excludePatternItem2) .build(); JsonParser parser = new JsonParser(); JsonElement parsed = parser.parse(search.getData(new Gson()).toString()); JsonObject obj = parsed.getAsJsonObject(); JsonObject source = obj.getAsJsonObject("_source"); JsonArray includePattern = source.getAsJsonArray("includes"); assertEquals(2, includePattern.size()); assertEquals(includePatternItem1, includePattern.get(0).getAsString()); assertEquals(includePatternItem2, includePattern.get(1).getAsString()); JsonArray excludePattern = source.getAsJsonArray("excludes"); assertEquals(2, excludePattern.size()); assertEquals(excludePatternItem1, excludePattern.get(0).getAsString()); assertEquals(excludePatternItem2, excludePattern.get(1).getAsString()); }
@Test public void supportElasticsearchPermissiveSourceFilteringSyntax() { String query = "{\"query\" : { \"term\" : { \"name\" : \"KangSungJeon\" } }, \"_source\": false}"; String includePatternItem1 = "SeolaIncludeFieldName"; String excludePatternItem1 = "SeolaExcludeField.*"; Action search = new Search.Builder(query) .addSourceIncludePattern(includePatternItem1) .addSourceExcludePattern(excludePatternItem1) .build(); JsonParser parser = new JsonParser(); JsonElement parsed = parser.parse(search.getData(new Gson()).toString()); JsonObject obj = parsed.getAsJsonObject(); JsonObject source = obj.getAsJsonObject("_source"); JsonArray includePattern = source.getAsJsonArray("includes"); assertEquals(1, includePattern.size()); assertEquals(includePatternItem1, includePattern.get(0).getAsString()); JsonArray excludePattern = source.getAsJsonArray("excludes"); assertEquals(1, excludePattern.size()); assertEquals(excludePatternItem1, excludePattern.get(0).getAsString()); query = "{\"query\" : { \"term\" : { \"name\" : \"KangSungJeon\" } }, \"_source\": [\"includeFieldName1\", \"includeFieldName2\"]}"; search = new Search.Builder(query) .addSourceIncludePattern(includePatternItem1) .addSourceExcludePattern(excludePatternItem1) .build(); parsed = parser.parse(search.getData(new Gson()).toString()); obj = parsed.getAsJsonObject(); source = obj.getAsJsonObject("_source"); includePattern = source.getAsJsonArray("includes"); assertEquals(3, includePattern.size()); assertEquals("includeFieldName1", includePattern.get(0).getAsString()); assertEquals("includeFieldName2", includePattern.get(1).getAsString()); assertEquals(includePatternItem1, includePattern.get(2).getAsString()); excludePattern = source.getAsJsonArray("excludes"); assertEquals(1, excludePattern.size()); assertEquals(excludePatternItem1, excludePattern.get(0).getAsString()); }
@Test public void sortTest() { String query = "{\"query\" : { \"term\" : { \"name\" : \"Milano\" } }}"; Action search = new Search.Builder(query) .addSort(Arrays.asList(sortByPopulationAsc, sortByPopulationDesc, sortByPopulation)).build(); JsonParser parser = new JsonParser(); JsonElement parsed = parser.parse(search.getData(new Gson()).toString()); JsonObject obj = parsed.getAsJsonObject(); JsonArray sort = obj.getAsJsonArray("sort"); assertEquals(3, sort.size()); JsonObject test = sort.get(0).getAsJsonObject(); assertTrue(test.has("population")); test = test.getAsJsonObject("population"); assertTrue(test.has("order")); assertEquals("asc", test.get("order").getAsString()); test = sort.get(1).getAsJsonObject(); assertTrue(test.has("population")); test = test.getAsJsonObject("population"); assertTrue(test.has("order")); assertEquals("desc", test.get("order").getAsString()); test = sort.get(2).getAsJsonObject(); assertTrue(test.has("population")); test = test.getAsJsonObject("population"); assertFalse(test.has("order")); assertFalse(test.has("order")); AbstractAction srch = (AbstractAction) search; assertTrue(srch.getParameter(Parameters.TRACK_SCORES).size() == 0); search = new Search.Builder(query) .addSort(Arrays.asList(sortByPopulationAsc, sortByPopulationDesc, sortByPopulation)) .enableTrackScores() .build(); srch = (AbstractAction) search; assertTrue(srch.getParameter(Parameters.TRACK_SCORES).size() == 1); assertTrue((Boolean) srch.getParameter(Parameters.TRACK_SCORES).iterator().next()); }