Matthew Harris
Matthew Harris

Reputation: 95

Algolia Add to nested array

 {"EmailAddress": "[email protected]",  
    "CompanyName": "Patheer",
    "Image": "user/img/matthew.harrispatheer.commatthew2.jpg",
    "FirstName": "Matthew",
    "LastName": "Harris",
    "JobFunction": "Information Technology",
    "PatheerJobsID": "B5PL0YqCeEe7np2j50h5Q",
    "JobTitle": "Database Administrator",
    "Aspirations": [
    {
      "PatheerJobsID": "685a215b4b0f418dbbf9285b28377c21",
      "JobTitle": "Data Scientist",
      "JobFunction": "Information Technology"
    }],
    "Skills": [
    {
      "SkillID": "2vRdiqCOE2XBDS3nsS1LA",
      "SkillName": "SQL",
      "SkillRating": 3
    },
    {
      "SkillID": "WEPMtg3JkU2xlpmgmvkYQ",
      "SkillName": "Accounting",
      "SkillRating": 4
    },
    {
      "SkillID": "d9c514035a554a5d8d99c411e43b683d",
      "SkillName": "Leadership",
      "SkillRating": 4
    },
    {
      "SkillID": "p2Aljae1EK3VDpx25dTKw",
      "SkillName": "MongoDB",
      "SkillRating": 3
    },
    {
      "SkillID": "c8fc21d83ea94842a8ae416df9317305",
      "SkillName": "Business Development",
      "SkillRating": 4
    },
    {
      "SkillID": "8f0a439029784e4e827e343e7f9034b2",
      "SkillName": "GAAP",
      "SkillRating": 3
    }],
    "objectID": "0157be4b-3426-4854-95b5-88e39cbc2403"
}

So above is my JSON and I am trying to add to the Skills array. Here is my code. What is wrong with it? I am using C# ..... and below is my function to "try" to add to the array but it doesn't work. The main function according to the Algolia website is "AddUnique", here is the link, but I am a little lost https://www.algolia.com/doc/csharp#update-an-existing-object-in-the-index

string algoliakey = System.Configuration.ConfigurationManager.AppSettings["AlgoliaKey"];
    string algoliasecret = System.Configuration.ConfigurationManager.AppSettings["AlgoliaSecret"];
    AlgoliaClient client = new AlgoliaClient(algoliakey,algoliasecret);
    var index = client.InitIndex("People");
    string objectID = personid;

    StringBuilder updatealgolia = new StringBuilder();

    updatealgolia.Append("{\"Skills\": {\"SkillID\":\"" + skillid + "\", \"SkillName\":\"" + skillname + "\", \"SkillRating\":5, \"_operation\": \"AddUnique\"}, \"objectID\":\"" + objectID + "\"}");

    index.PartialUpdateObject(JObject.Parse(updatealgolia.ToString()), false);

Upvotes: 0

Views: 209

Answers (1)

aseure
aseure

Reputation: 733

Yes, it's totatlly doable.

However, you're not using the correct syntax for that. The skill element that you want to add must be in the value field as shown in the C# client documentation.

In your case, you simply need to reformat your JSON like that:

{
  "objectID": "<OBJECT_ID>",
  "Skills": {
    "_operation": "AddUnique",
    "value": {
      "SkillID": "<SKILL_ID>",
      "SkillName": "<SKILL_NAME>",
      "SkillRating": <SKILL_RATING>
    }
  }
}

Upvotes: 2

Related Questions