How to fetch current value of _version for a document - spring-data-elasticsearch

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.

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 ignore null field when deserializing in 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?

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

Avro default value for user defined types

Is it possible to have a default value for a user defined type?
ie given the avdl:
protocol {
record A { }
record B {
union { A, string } foo = A;
}
}
record B is valid and thing by default is an instance of A?
Found the answer, {}
idl:
protocol {
record A { }
record B {
union { A, string } foo = {};
}
}
results in the avsc:
{
"type" : "record",
"name" : "B",
"fields" : [ {
"name" : "foo",
"type" : [ {
"type" : "record",
"name" : "A",
"fields" : [ ]
}, "string" ],
"default" : { }
} ]
}
This implies: new of the first type of the union, in this case A.

Resources