Search code examples
elasticsearchkibana

How to create a composite primary key in my document in elasticsearch


I want to create primary keys using name and ssn.

"hits" : [
  {
    "_index" : "students",
    "_type" : "_doc",
    "_id" : "_prfVnIB9H5oRoSoC",
    "_score" : 1.0,
    "_source" : {
      "name" : Sam,
      "age" : "23",
      "SSN" : "2365PK",
      "Class" : "10",
      "School" : "St jose School "
     }
 ]

I am trying something like this, I don't think this is correct :

  PUT students/_doc/_create
   {
     "name" : "ssn"
    }

Here is the code where I'm sending the data to elasticsearch :

 String newESURL = esUrl + "/students/_doc";
        int diplayCount = 0;
        for (; rec < Records.size(); rec++) {
            String StudRecord = Obj.writeValueAsString(Records.get(rec));
            String Resp = given().trustStore("keystore/cacerts", "xyz").baseUri(newESURL).contentType("application/json").accept("application/json")
                    .body(StudRecord)
                    .auth().basic(User, Password)
                    .post().then().extract().body().asString();

Solution

  • There we go. Here is the modified code that would achieve what you need:

    String newESURL = esUrl + "/students/_doc/";
        int diplayCount = 0;
        for (; rec < Records.size(); rec++) {
            Record = record = Records.get(rec);
            String StudRecord = Obj.writeValueAsString(record);
            String StudentKey = "TODO";
            String Resp = given().trustStore("keystore/cacerts", "xyz")
                    .baseUri(newESURL + StudentKey)
                    .contentType("application/json")
                    .accept("application/json")
                    .body(StudRecord)
                    .auth().basic(User, Password)
                    .put().then().extract().body().asString();
    

    Where you see "TODO", you need to create a compound key using the name and SSN fields of the record and join them together, e.g. john-123-456-6789 So the URL is actually different for each record because it contains the ID (i.e. primary key) of your record. This will prevent duplication on subsequent loads.

    Also make sure to use put() instead of post()