alexanoid
alexanoid

Reputation: 25770

Create a id(key) baed on JSON document for Elasticsearch

At my Java application I need to store a JSON document at ElasticSearch. I want to prevent a duplication of the documents in ES so I'm going to calculate some kind of id(key) based on JSON object/string and use it as own id for this document when indexing at ES. Unfortunately I don't have any candidates for a natural key inside of this JSON, so should take into account the whole JSON object/string for this key generation.

This is an example of JSON document:

{
   "filterQueries":[
      {
         "type":"LessOrEqualQuery",
         "characteristicId":630,
         "value":799621200000,
         "operator":"<="
      }
   ],
   "sortCriteriaIds":[
      566,
      572
   ],
   "sortWeightCriteriaDirection":"DESC",
   "sortTotalVotesCriteriaDirection":null,
   "sortCriteriaCoefficients":{
      "572":20.0
   },
   "sortCharacteristicId":631,
   "sortCharacteristicDirection":"DESC",
   "sortDecisionPropertyName":"createDate",
   "sortDecisionPropertyDirection":"DESC",
   "excludeChildDecisionIds":null,
   "includeChildDecisionIds":null,
   "pageNumber":0,
   "pageSize":100
}

What is the best way to calculate this key based on JSON object/string in Java ? Performance is a very important criterion to me there.

Upvotes: 0

Views: 192

Answers (1)

laser
laser

Reputation: 570

If speed concerns very much. You can use XOR operation (almost CRC32 for any size).

Pseudocode:

input_string = Stringify(json)
result = 0;
for(each chunk of size K from input_string){
    result = result XOR chunk;
}
return result

Upvotes: 1

Related Questions