List of usage examples for com.google.gson JsonElement isJsonNull
public boolean isJsonNull()
From source file:org.hibernate.search.backend.elasticsearch.types.codec.impl.GeoPointFieldCodec.java
License:LGPL
@Override public GeoPoint decode(JsonElement element) { if (element == null || element.isJsonNull()) { return null; }/*from w ww.j a v a 2 s . co m*/ JsonObject object = JsonElementTypes.OBJECT.fromElement(element); double latitude = LATITUDE_ACCESSOR.get(object).orElseThrow(log::elasticsearchResponseMissingData); double longitude = LONGITUDE_ACCESSOR.get(object).orElseThrow(log::elasticsearchResponseMissingData); return new ImmutableGeoPoint(latitude, longitude); }
From source file:org.hibernate.search.backend.elasticsearch.types.codec.impl.IntegerFieldCodec.java
License:LGPL
@Override public Integer decode(JsonElement element) { if (element == null || element.isJsonNull()) { return null; }//from w ww. j a v a 2s .co m return JsonElementTypes.INTEGER.fromElement(element); }
From source file:org.hibernate.search.backend.elasticsearch.types.codec.impl.JsonStringFieldCodec.java
License:LGPL
@Override public String decode(JsonElement element) { if (element == null || element.isJsonNull()) { return null; }/*w w w . j a va 2s.c o m*/ return gson.toJson(element); }
From source file:org.hibernate.search.backend.elasticsearch.types.codec.impl.LocalDateFieldCodec.java
License:LGPL
@Override public LocalDate decode(JsonElement element) { if (element == null || element.isJsonNull()) { return null; }//from ww w. j av a 2 s.co m String stringValue = JsonElementTypes.STRING.fromElement(element); return LocalDate.parse(stringValue, delegate); }
From source file:org.hibernate.search.backend.elasticsearch.types.codec.impl.StringFieldCodec.java
License:LGPL
@Override public String decode(JsonElement element) { if (element == null || element.isJsonNull()) { return null; }/* ww w.jav a 2s.co m*/ return JsonElementTypes.STRING.fromElement(element); }
From source file:org.hibernate.search.elasticsearch.query.impl.ElasticsearchHSQueryImpl.java
License:LGPL
private JsonObject getFilteredQuery(JsonElement originalQuery, Set<String> typeNames) { JsonArray filters = new JsonArray(); JsonObject tenantFilter = getTenantIdFilter(); if (tenantFilter != null) { filters.add(tenantFilter);// w ww.j av a2s . c o m } JsonArray typeFilters = new JsonArray(); for (String typeName : typeNames) { typeFilters.add(getEntityTypeFilter(typeName)); } // wrap type filters into should if there is more than one filters.add(ToElasticsearch.condition("should", typeFilters)); // facet filters for (Query query : getFacetManager().getFacetFilters().getFilterQueries()) { filters.add(ToElasticsearch.fromLuceneQuery(query)); } // user filter if (userFilter != null) { filters.add(ToElasticsearch.fromLuceneQuery(userFilter)); } if (!filterDefinitions.isEmpty()) { for (FullTextFilterImpl fullTextFilter : filterDefinitions.values()) { JsonObject filter = buildFullTextFilter(fullTextFilter); if (filter != null) { filters.add(filter); } } } JsonBuilder.Object boolBuilder = JsonBuilder.object(); if (originalQuery != null && !originalQuery.isJsonNull()) { boolBuilder.add("must", originalQuery); } if (filters.size() == 1) { boolBuilder.add("filter", filters.get(0)); } else { boolBuilder.add("filter", filters); } return JsonBuilder.object().add("bool", boolBuilder.build()).build(); }
From source file:org.hibernate.search.elasticsearch.query.impl.JsonDrivenProjection.java
License:LGPL
@Override public Object convertHit(JsonObject hit, ConversionContext conversionContext) { JsonElement value = extractFieldValue(hit.get("_source").getAsJsonObject(), absoluteName); if (value == null || value.isJsonNull()) { return null; }// ww w.j ava 2 s . c om // TODO: HSEARCH-2255 should we do it? if (!value.isJsonPrimitive()) { throw LOG.unsupportedProjectionOfNonJsonPrimitiveFields(value); } JsonPrimitive primitive = value.getAsJsonPrimitive(); if (primitive.isBoolean()) { return primitive.getAsBoolean(); } else if (primitive.isNumber()) { // TODO HSEARCH-2255 this will expose a Gson-specific Number implementation; Can we somehow return an Integer, // Long... etc. instead? return primitive.getAsNumber(); } else if (primitive.isString()) { return primitive.getAsString(); } else { // TODO HSEARCH-2255 Better raise an exception? return primitive.toString(); } }
From source file:org.hibernate.search.elasticsearch.query.impl.PrimitiveProjection.java
License:LGPL
public void addDocumentField(Document tmp, JsonElement jsonValue) { if (jsonValue == null || jsonValue.isJsonNull()) { return;/* w w w. j ava 2 s. co m*/ } switch (fieldType) { case INTEGER: tmp.add(new IntField(absoluteName, jsonValue.getAsInt(), Store.NO)); break; case LONG: tmp.add(new LongField(absoluteName, jsonValue.getAsLong(), Store.NO)); break; case FLOAT: tmp.add(new FloatField(absoluteName, jsonValue.getAsFloat(), Store.NO)); break; case DOUBLE: tmp.add(new DoubleField(absoluteName, jsonValue.getAsDouble(), Store.NO)); break; case UNKNOWN_NUMERIC: throw LOG.unexpectedNumericEncodingType(rootTypeMetadata.getType(), absoluteName); case BOOLEAN: tmp.add(new StringField(absoluteName, String.valueOf(jsonValue.getAsBoolean()), Store.NO)); break; default: tmp.add(new StringField(absoluteName, jsonValue.getAsString(), Store.NO)); break; } }
From source file:org.hibernate.search.elasticsearch.query.impl.PrimitiveProjection.java
License:LGPL
@Override public Object convertHit(JsonObject hit, ConversionContext conversionContext) { JsonElement jsonValue = extractFieldValue(hit.get("_source").getAsJsonObject(), absoluteName); if (jsonValue == null || jsonValue.isJsonNull()) { return null; }//from w w w.j ava 2 s.c o m switch (fieldType) { case INTEGER: return jsonValue.getAsInt(); case LONG: return jsonValue.getAsLong(); case FLOAT: return jsonValue.getAsFloat(); case DOUBLE: return jsonValue.getAsDouble(); case UNKNOWN_NUMERIC: throw LOG.unexpectedNumericEncodingType(rootTypeMetadata.getType(), absoluteName); case BOOLEAN: return jsonValue.getAsBoolean(); default: return jsonValue.getAsString(); } }
From source file:org.hibernate.search.elasticsearch.query.impl.QueryHitConverter.java
License:LGPL
public EntityInfo convert(SearchResult searchResult, JsonObject hit) { String type = hit.get("_type").getAsString(); EntityIndexBinding binding = targetedEntityBindingsByName.get(type); if (binding == null) { LOG.warnf("Found unknown type in Elasticsearch index: " + type); return null; }// ww w . j av a 2 s. c o m DocumentBuilderIndexedEntity documentBuilder = binding.getDocumentBuilder(); IndexedTypeIdentifier typeId = documentBuilder.getTypeIdentifier(); ConversionContext conversionContext = new ContextualExceptionBridgeHelper(); conversionContext.setConvertedTypeId(typeId); FieldProjection idProjection = idProjectionByEntityBinding.get(binding); Object id = idProjection.convertHit(hit, conversionContext); Object[] projections = null; if (projectedFields != null) { projections = new Object[projectedFields.length]; for (int i = 0; i < projections.length; i++) { String field = projectedFields[i]; if (field == null) { continue; } switch (field) { case ElasticsearchProjectionConstants.SOURCE: projections[i] = hit.getAsJsonObject().get("_source").toString(); break; case ElasticsearchProjectionConstants.ID: projections[i] = id; break; case ElasticsearchProjectionConstants.OBJECT_CLASS: projections[i] = typeId.getPojoType(); break; case ElasticsearchProjectionConstants.SCORE: projections[i] = hit.getAsJsonObject().get("_score").getAsFloat(); break; case ElasticsearchProjectionConstants.SPATIAL_DISTANCE: JsonElement distance = null; // if we sort by distance, we need to find the index of the DistanceSortField and use it // to extract the values from the sort array // if we don't sort by distance, we use the field generated by the script_field added earlier if (sortByDistanceIndex != null) { distance = hit.getAsJsonObject().get("sort").getAsJsonArray().get(sortByDistanceIndex); } else { JsonElement fields = hit.getAsJsonObject().get("fields"); if (fields != null) { // "fields" seems to be missing if there are only null results in script fields distance = hit.getAsJsonObject().get("fields").getAsJsonObject() .get(SPATIAL_DISTANCE_FIELD); } } if (distance != null && distance.isJsonArray()) { JsonArray array = distance.getAsJsonArray(); distance = array.size() >= 1 ? array.get(0) : null; } if (distance == null || distance.isJsonNull()) { projections[i] = null; } else { Double distanceAsDouble = distance.getAsDouble(); if (distanceAsDouble == Double.MAX_VALUE || distanceAsDouble.isInfinite()) { /* * When we extract the distance from the sort, its default value is: * - Double.MAX_VALUE on older ES versions (5.0 and lower) * - Double.POSITIVE_INFINITY on newer ES versions (from somewhere around 5.2 onwards) */ projections[i] = null; } else { projections[i] = distance.getAsDouble(); } } break; case ElasticsearchProjectionConstants.TOOK: projections[i] = searchResult.getTook(); break; case ElasticsearchProjectionConstants.TIMED_OUT: projections[i] = searchResult.getTimedOut(); break; case ElasticsearchProjectionConstants.THIS: // Use EntityInfo.ENTITY_PLACEHOLDER as placeholder. // It will be replaced when we populate // the EntityInfo with the real entity. projections[i] = EntityInfo.ENTITY_PLACEHOLDER; break; default: FieldProjection projection = fieldProjectionsByEntityBinding.get(binding)[i]; projections[i] = projection.convertHit(hit, conversionContext); } } } return new EntityInfoImpl(typeId, documentBuilder.getIdPropertyName(), (Serializable) id, projections); }