Пример #1
0
 @Override
 public JSONObject serialize(Query bean) throws JSONException {
   if (bean == null) {
     return null;
   }
   if (!typeNames.containsKey(bean.getClass())) {
     throw new UnsupportedOperationException(
         "Class "
             + bean.getClass()
             + " is not supported for serialization by the QueryJsonHandler");
   }
   JSONObject defaultSerialization = (JSONObject) JsonSerializer.serialize(bean, false);
   if (bean instanceof FieldAwareQuery) {
     defaultSerialization.remove("field");
     if (bean instanceof SpanTerm && ((SpanTerm) bean).getBoost() == null) {
       SpanTerm spanTerm = (SpanTerm) bean;
       defaultSerialization = new JSONObject().put(spanTerm.getField(), spanTerm.getValue());
     } else {
       defaultSerialization =
           new JSONObject().put(((FieldAwareQuery) bean).getField(), defaultSerialization);
     }
   }
   if (bean instanceof Selection) {
     defaultSerialization.remove("field");
     defaultSerialization =
         new JSONObject().put(((Selection) bean).getField(), defaultSerialization);
   }
   if (bean.getRelevance() != null) {
     defaultSerialization.remove("relevance");
     defaultSerialization.put("relevance", JsonSerializer.serialize(bean.getRelevance()));
   }
   return new JSONObject().put(typeNames.get(bean.getClass()), defaultSerialization);
 }
Пример #2
0
  @Override
  public JSONObject serialize(Filter bean) throws JSONException {
    if (bean == null) {
      return null;
    }
    if (bean instanceof Selection) {
      return selectionJsonHandler.serialize((Selection) bean);
    }
    if (bean instanceof StringQuery) {
      JSONObject ret = (JSONObject) JsonSerializer.serialize(bean);
      return new JSONObject().put("query", ret);
    }
    if (bean instanceof AndOr) {
      AndOr andOr = (AndOr) bean;
      String operation = andOr.getOperation().name();

      List<JSONObject> filters = convertToJson(andOr.filters);
      return new JSONObject().put(operation, new JSONArray(filters));
    }
    if (bean instanceof BoolFilter) {
      BoolFilter bool = (BoolFilter) bean;
      JSONObject ret = new JSONObject();
      if (bool.getMust() != null) {
        ret.put("must", new JSONArray(convertToJson(bool.getMust())));
      }
      if (bool.getMust_not() != null) {
        ret.put("must_not", new JSONArray(convertToJson(bool.getMust_not())));
      }
      if (bool.getShould() != null) {
        ret.put("should", new JSONArray(convertToJson(bool.getShould())));
      }
      return new JSONObject().put("bool", ret);
    }
    if (bean instanceof Ids) {
      Ids ids = (Ids) bean;
      JSONObject ret = new JSONObject();
      if (ids.getValues() != null) {
        ret.put("values", new JSONArray(ids.getValues()));
      }
      if (ids.getExcludes() != null) {
        ret.put("excludes", new JSONArray(ids.getExcludes()));
      }

      return new JSONObject().put("ids", ret);
    }
    if (bean instanceof Query) {
      return queryJsonHandler.serialize((Query) bean);
    }
    if (bean instanceof QueryFilter) {
      return new JSONObject()
          .put("query", queryJsonHandler.serialize(((QueryFilter) bean).getQuery()));
    }
    throw new UnsupportedOperationException(bean.getClass() + " is not supported");
  }