how to ignore null field when deserializing in spring data elasticsearch - spring-data-elasticsearch

I have saved two objects.
#Document(indexName = "test_index", createIndex = false)
public class ZTest {
#Id
private String id;
private String testOne;
#Transient
private String testTwo;
private String testThree;
}
{
"_index" : "test_index",
"_type" : "_doc",
"_id" : "w0mCgYEBXSVfca67-ayl",
"_score" : 1.0,
"_source" : {
"_class" : "com.ZTest",
"testOne" : "test_one"
}
},
{
"_index" : "test_index",
"_type" : "_doc",
"_id" : "xEmLgYEBXSVfca67r6wf",
"_score" : 1.0,
"_source" : {
"_class" : "com.ZTest",
"testOne" : "test_one",
"testThree" : "test_three"
}
}
Iterable<ZTest> t = zTestRepository.findAll();
Is it possible to exclude those null fields in the read result?
I have tried setting this spring.jackson.default-property-inclusion=non_null in property files. It doesn't work.

Spring Data Elasticsearch returns entities, and these Java POJO entities have 3 defined properties. And for properties that are transient or that are not returned by Elasticsearch the property value will be the value that is set in the object when on object of the entity class is created - here it's null.
What else do you expect to be returned?

Related

Elastic Search Update Operation not working in nodejs client

I am updating the existing document in elastic search by below update query.
const result = await client.update({
index,
id,
body: {
script: {
lang: "painless",
source: "ctx._source = params.customerDetailsObj",
params: {
customerDetailsObj: item
}
}
}
});
where item has below structure -
customerDetails: {
details,
metadata
}
and client is elasticsearch client
id and index are correct values where id belongs to already existing id in elastic search
const { Client } = require("#elastic/elasticsearch");
and already saved document structure is below -
{
"_index" : "customerdetailsreport",
"_type" : "_doc",
"_id" : "cu-qo4MBxhUIUIg_lZUJ",
"_score" : 12.078913,
"_source" : {
"customerDetails" : {
"details" : {
"id" : "1234567",
"name" : "Atul Joshi",
},
"metadata" : {
"id" : "1234567"
}
}
}
}
The issue I am facing is , after update operation , existing item is not updated but a new entry is created
So , In elastic search I find two documents with different _id meaning two different entries are created
Can anybody please help what I am missing here ?

How to fetch current value of _version for a document

How to fetch current value of _version for a document using spring-data-elasticsearch API(s)
{
"_index" : "foos",
"_type" : "_doc",
"_id" : "FQOO93sBMpiRC0Jqlyn8",
"_version" : 3,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 84,
"_primary_term" : 2
}
In your entity define a property that is annotated with #Version:
#Document(indexName="foos")
class Foo {
#Id private String id;
#Version private Long version;
// other properties, getter and setter
}
when returning entities in get or search results, this property will be populated with the version value.

Mapping a variety of JSON responses with the same top - level structure in Swift

I have a variety of JSON responses that I'm getting back from a server via Alamofire; two are included below:
1)
{
"json" : {
"harkUpdate" : {
"more" : [
{
"unread-count" : {
"last" : 1613507864973,
"index" : {
"graph" : {
"graph" : "\/ship\/~dopzod\/urbit-help",
"index" : "\/"
}
}
}
},
{
"seen-index" : {
"index" : {
"graph" : {
"index" : "\/",
"graph" : "\/ship\/~dopzod\/urbit-help"
}
},
"time" : 1613507864973
}
}
]
}
},
"response" : "diff",
"id" : 3
}
{
"json" : {
"graph-update" : {
"keys" : [
{
"name" : "book-club-4717",
"ship" : "sicdev-pilnup"
},
{
"name" : "dm--locrev-finlys",
"ship" : "haddef-sigwen"
},
{
"name" : "smol-bibliotheca",
"ship" : "dasfeb"
},
{
"name" : "interface",
"ship" : "bitpyx-dildus"
},
{
"name" : "jobs-gigs",
"ship" : "nibset-napwyn"
},
{
"name" : "tlon-general",
"ship" : "bolbex-fogdys"
}
]
}
},
"id" : 1,
"response" : "diff"
}
As you can see, they both have an id, a response, and json fields - but the actual json response in them varies.
Normally what I would do is generate classes using something like quicktype.io, or use one of the many ObjectMapping frameworks out there. What I'm trying to get mentally un-blocked with is that all these json responses have a field called "json", which has a different structure. I'm trying to figure out who I can make unique classes for the responses given this structuring. Any help appreciated.
You can declare a basic object which's json property will be a generic type conforming to Codable.
struct JSON<T : Codable>: Codable {
let json: T
let id: Int
let response: String
}
After you define two structs which will represent the first and second json objects. For simplicity I will define only the objects that represent the second JSON:
struct GraphUpdate: Codable {
let graphUpdate: Keys
enum CodingKeys: String, CodingKey {
case graphUpdate = "graph-update"
}
}
struct Keys: Codable {
let keys: [Key]
}
struct Key: Codable {
let name, ship: String
}
And when trying to decode, you have to specify what object you want to decode to:
let item = try? JSONDecoder().decode(JSON<GraphUpdate>.self, from: data)

How do I use swagger api to generate swagger with additionalProperties as false for object

I somehow have to generate a swagger doc with addtionalProperties as false to not allow additional properties. For example, this is what I want to get:
"TestDTO" : {
"type" : "object",
"additionalProperties":false
"properties" : {
"property1" : {
"description" : "This is a test object.",
"$ref" : "#/definitions/TestDTO"
},
"perperty2" : {
"type" : "string",
"description" : "this is a property."
}
}
},
I looked at https://github.com/swagger-api/swagger-core/blob/master/modules/swagger-models/src/main/java/io/swagger/models/ModelImpl.java
but setAdditionalProperties accept a Property only, how do I set it with a boolean value?
public void setAdditionalProperties(Property additionalProperties) {
type(OBJECT);
this.additionalProperties = additionalProperties;
}
addtionalProperties as boolean value is not supported in swagger spec 2.0,
swagger spec 3.0 added support for boolean value.
see details here:
https://groups.google.com/forum/#!topic/swagger-swaggersocket/1J5KznDibzA

GeoJson polygons from MVC controller to gmaps.js through ajax

I'm trying to send a few polygons from a controller to a ajax request on the view.
I'm using gmaps.js on client side. And I'm trying to follow the GeoJson specs for polygon object. Here is my raw object:
[
{
"type" : "POLYGON",
"id" : null,
"geometry" :
[
[
[25.24718989858574, 55.34980773925781],
[25.24715108267782, 55.34893870353699],
[25.24763628063545, 55.34891724586487],
[25.247869174966873, 55.34907817840576],
[25.247869174966873, 55.34965753555298],
[25.24767509638834, 55.34981846809387]
]
]
},
{
"type" : "POLYGON",
"id" : null,
"geometry" :
[
[
[25.248684301611156, 55.34983992576599],
[25.248480520462763, 55.349721908569336],
[25.24788858280767, 55.34993648529053],
[25.247578056982842, 55.35008668899536],
[25.247354866056035, 55.35041928291321],
[25.246830850964393, 55.35105228424072],
[25.2466755867995, 55.35133123397827],
[25.24611275253863, 55.35210371017456],
[25.24532672101826, 55.353219509124756],
[25.24626801742273, 55.35389542579651],
[25.24648150631442, 55.354024171829224],
[25.248655190039447, 55.3501296043396]
]
]
},
{
"type" : "POLYGON",
"id" : null,
"geometry" :
[
[
[25.246190385005487, 55.35040855407715],
[25.246190385005487, 55.35080552101135],
[25.246374761915458, 55.35084843635559],
[25.24653002646476, 55.35092353820801],
[25.24661736268653, 55.35104155540466],
[25.24703463487969, 55.350472927093506],
[25.246918186969957, 55.35039782524109]
]
]
},
{
"type" : "POLYGON",
"id" : null,
"geometry" :
[
[
[25.24541891016349, 55.350982546806335],
[25.24540920604625, 55.35240948200226],
[25.24495311166098, 55.352463126182556],
[25.244967667891192, 55.350998640060425]
]
]
},
{
"type" : "POLYGON",
"id" : null,
"geometry" :
[
[
[25.245229679737204, 55.35044610500336],
[25.245244235934287, 55.35085916519165],
[25.246098196445583, 55.35085916519165],
[25.24614671674897, 55.3508323431015],
[25.24614671674897, 55.35039246082306],
[25.24603997205592, 55.35036027431488]
]
]
}
]
To represent one polygon, I have this class on server side:
public class TempPolygon
{
public string Type { get; set; }
public string Id { get; set; }
public List<Tuple<decimal, decimal>> Geometry { get; set; }
public TempPolygon()
{
Type = "POLYGON";
Id = null;
}
}
Now the action will return a List<TempPolygon> as JsonResult but I'm not able to figure out if my definition of the TempPolygon class is correctly reflecting the GeoJson. Also if I paste the Json on http://json2csharp.com/, I get this class:
public class RootObject
{
public string type { get; set; }
public object id { get; set; }
public List<List<List<double>>> geometry { get; set; }
}
The List<List<List<double>>> makes no sense as clearly, latitude and longitude are pairs. How do i define my class's Geometry property to reflect the specs of GeoJson

Resources