Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
refactored Sort json serialization process in Search - now using gson…
Browse files Browse the repository at this point in the history
… instead of string building
  • Loading branch information
Cihat Keser committed Dec 3, 2014
1 parent 86170c6 commit dcf2807
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 197 deletions.
23 changes: 12 additions & 11 deletions jest-common/src/main/java/io/searchbox/core/Search.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@
import io.searchbox.core.search.sort.Sort;
import io.searchbox.params.Parameters;
import io.searchbox.params.SearchType;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.*;

/**
* @author Dogukan Sonmez
Expand Down Expand Up @@ -63,17 +60,21 @@ public String getRestMethodName() {
return "POST";
}

@SuppressWarnings("unchecked")
@Override
public Object getData(Gson gson) {
String data;
if (sortList.size() > 0) {
StringBuilder sorting = new StringBuilder("\"sort\": [");
sorting.append(StringUtils.join(sortList, ","));
sorting.append("],");

data = query.replaceFirst("\\{", "\\{" + sorting.toString());
} else {
if (sortList.isEmpty()) {
data = query;
} else {
List<Map<String, Object>> sortMaps = new ArrayList<Map<String, Object>>(sortList.size());
for (Sort sort : sortList) {
sortMaps.add(sort.toMap());
}

Map rootJson = gson.fromJson(query, Map.class);
rootJson.put("sort", sortMaps);
data = gson.toJson(rootJson);
}
return data;
}
Expand Down
97 changes: 47 additions & 50 deletions jest-common/src/main/java/io/searchbox/core/search/sort/Sort.java
Original file line number Diff line number Diff line change
@@ -1,45 +1,35 @@
package io.searchbox.core.search.sort;

import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

/**
* @author Riccardo Tasso
* @author cihat keser
*/

// TODO:
// * Geo Distance Sorting (Lat Lon as Properties, Lat Lon as String, Geohash, Lat Lon as Array)
// * Script Based Sorting
// * Track Scores (it should be in the Search object)

public class Sort {
// TODO:
// * Geo Distance Sorting (Lat Lon as Properties, Lat Lon as String, Geohash, Lat Lon as Array)
// * Script Based Sorting
// * Track Scores (it should be in the Search object)

private String field;
private Sorting direction;
private Sorting order;
private Object missing;
private Boolean unmapped;
private Gson gson = new Gson();

public Sort(String field) {
this.field = field;
}

public Sort(String field, Sorting direction) {
public Sort(String field, Sorting order) {
this.field = field;
this.direction = direction;
}

public Gson getGson() {
return gson;
}

public void setGson(Gson gson) {
this.gson = gson;
this.order = order;
}

/**
* @param m should be a Missing object (LAST or FIRST) or a custom value (String, Integer, Double, ...)
* @param m should be a Missing object (LAST or FIRST) or a custom value
* (String, Integer, Double, ...) that will be used for missing docs as the sort value
*/
public void setMissing(Object m) {
this.missing = m;
Expand All @@ -49,45 +39,52 @@ public void setIgnoreUnmapped() {
this.unmapped = true;
}

public String toString() {
// simple case
if (direction == null && missing == null && unmapped == null)
return "\"" + this.field + "\"";

// build of complex cases
public Map<String, Object> toMap() {
Map<String, Object> innerMap = new HashMap<String, Object>();

Map<String, Object> obj = new HashMap<String, Object>();

if (direction != null) {
String dir = "asc";
if (direction == Sorting.DESC)
dir = "desc";
obj.put("order", dir);
if (order != null) {
innerMap.put("order", order.toString());
}

if (missing != null) {
Object miss = null;
if (this.missing instanceof Missing) {
Missing current = (Missing) this.missing;
if (current == Missing.LAST) miss = "_last";
else miss = "_first";
} else {
miss = this.missing;
}
obj.put("missing", miss);
innerMap.put("missing", missing.toString());
}

if (unmapped != null) {
obj.put("ignore_unmapped", unmapped);
innerMap.put("ignore_unmapped", unmapped);
}

String json = gson.toJson(obj);
Map<String, Object> rootMap = new HashMap<String, Object>();
rootMap.put(field, innerMap);
return rootMap;
}

public enum Sorting {
ASC("asc"),
DESC("desc");

private final String name;

return "{ \"" + this.field + "\" : " + json + "}";
private Sorting(String s) {
name = s;
}

public String toString() {
return name;
}
}

public enum Sorting {ASC, DESC}
public enum Missing {
LAST("_last"),
FIRST("_first");

public enum Missing {LAST, FIRST}
private final String name;

private Missing(String s) {
name = s;
}

public String toString() {
return name;
}
}

}
15 changes: 9 additions & 6 deletions jest-common/src/test/java/io/searchbox/core/SearchTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package io.searchbox.core;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.*;
import io.searchbox.action.Action;
import io.searchbox.core.search.sort.Sort;
import io.searchbox.core.search.sort.Sort.Sorting;
Expand All @@ -13,6 +10,7 @@
import java.util.List;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;

/**
Expand Down Expand Up @@ -79,7 +77,7 @@ public void sortTest() {
Action search = new Search.Builder(query).addSort(sorting).build();

JsonParser parser = new JsonParser();
JsonElement parsed = parser.parse(search.getData(null).toString());
JsonElement parsed = parser.parse(search.getData(new Gson()).toString());
JsonObject obj = parsed.getAsJsonObject();
JsonArray sort = obj.getAsJsonArray("sort");

Expand All @@ -102,6 +100,11 @@ public void sortTest() {
assertEquals("desc", test.get("order").getAsString());

// sort 2
assertEquals("population", sort.get(2).getAsString());
test = sort.get(2).getAsJsonObject();
assertTrue(test.has("population"));

test = test.getAsJsonObject("population");
assertFalse(test.has("order"));
assertFalse(test.has("order"));
}
}
Loading

0 comments on commit dcf2807

Please sign in to comment.