Skip to content

Instantly share code, notes, and snippets.

@moodysalem
Last active March 15, 2017 20:41
Show Gist options
  • Save moodysalem/585033149bb85f1e6079f1d507f8c72d to your computer and use it in GitHub Desktop.
Save moodysalem/585033149bb85f1e6079f1d507f8c72d to your computer and use it in GitHub Desktop.
Used for building ElasticSearch query objects
package com.fastmodelsports.social.lambda.util.es;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
public abstract class SearchBuilder {
public static JsonArray sortOn(final String attribute, final boolean desc) {
final JsonObject sort = new JsonObject(),
attr = new JsonObject();
sort.add(attribute, attr);
attr.addProperty("order", desc ? "desc" : "asc");
final JsonArray arr = new JsonArray();
arr.add(sort);
return arr;
}
public static JsonObject range(final String property, final String gte, final String lte) {
final JsonObject q = new JsonObject(),
r = new JsonObject(),
d = new JsonObject();
q.add("range", r);
r.add(property, d);
if (gte != null) {
d.addProperty("gte", gte);
}
if (lte != null) {
d.addProperty("lte", lte);
}
return q;
}
private enum BoolType {
must,
should,
must_not
}
public static JsonObject query(final JsonObject query) {
return query(query, null, null);
}
public static JsonObject query(final JsonObject query, final Integer size, final Integer from) {
final JsonObject o = new JsonObject();
o.add("query", query);
if (size != null) {
o.addProperty("size", size);
}
if (from != null) {
o.addProperty("from", from);
}
return o;
}
public static JsonObject must(final JsonObject... must) {
return bool(BoolType.must, must);
}
public static JsonObject should(final JsonObject... should) {
return bool(BoolType.should, should);
}
public static JsonObject mustNot(final JsonObject... should) {
return bool(BoolType.must_not, should);
}
private static JsonObject bool(final BoolType sm, final JsonObject... opts) {
if (opts == null || opts.length == 0) {
return sm.equals(BoolType.should) ? matchNone() : matchAll();
}
final JsonObject outer = new JsonObject(),
inner = new JsonObject();
final JsonArray arr = new JsonArray();
outer.add("bool", inner);
inner.add(sm.name(), arr);
for (final JsonObject j : opts) {
arr.add(j);
}
return outer;
}
public static JsonObject matchNone() {
final JsonObject o = new JsonObject();
o.add("match_none", new JsonObject());
return o;
}
public static JsonObject matchAll() {
final JsonObject o = new JsonObject();
o.add("match_all", new JsonObject());
return o;
}
public static JsonObject termQuery(final String property, final String value, final String name) {
final JsonObject o = new JsonObject(),
t = new JsonObject(),
v = new JsonObject();
o.add("term", t);
t.add(property, v);
v.addProperty("value", value);
if (name != null) {
v.addProperty("_name", name);
}
return o;
}
/**
* produces {"exists" : { "field" : "!property!" }}
*
* @param property that must exist
* @return jsonobject representing the es query
*/
public static JsonObject existsQuery(final String property) {
final JsonObject exists = new JsonObject(),
val = new JsonObject();
exists.add("exists", val);
val.addProperty("field", property);
return exists;
}
public static JsonObject matchQuery(final String property, final String match, final String name) {
final JsonObject outer = new JsonObject(),
inner = new JsonObject(),
info = new JsonObject();
outer.add("match", inner);
inner.add(property, info);
info.addProperty("query", match);
info.addProperty("_name", name);
info.addProperty("operator", "AND");
info.addProperty("zero_terms_query", "NONE");
return outer;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment