Chilkat Online Tools

Swift / MongoDB Atlas / Update All User-Defined Analyzers for a Cluster

Back to Collection Items

func chilkatTest() {
    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    let http = CkoHttp()
    var success: Bool

    http.DigestAuth = true
    http.Login = "username"
    http.Password = "password"

    // Use this online tool to generate code from sample JSON: Generate Code to Create JSON

    // The following JSON is sent in the request body.

    // [
    //   {
    //     "name": "my_new_analyzer",
    //     "baseAnalyzer": "lucene.standard",
    //     "maxTokenLength": 48
    //   },
    //   {
    //     "name": "my_other_new_analyzer",
    //     "baseAnalyzer": "lucene.english",
    //     "stopwords": [
    //       "foo",
    //       "bar",
    //       "baz"
    //     ]
    //   }
    // ]

    let jarr = CkoJsonArray()

    jarr.AddObjectAt(-1)
    var jsonObj_1: CkoJsonObject? = jarr.ObjectAt(jarr.Size.intValue - 1)
    jsonObj_1!.UpdateString("name", value: "my_new_analyzer")
    jsonObj_1!.UpdateString("baseAnalyzer", value: "lucene.standard")
    jsonObj_1!.UpdateInt("maxTokenLength", value: 48)
    jsonObj_1 = nil

    jarr.AddObjectAt(-1)
    jsonObj_1 = jarr.ObjectAt(jarr.Size.intValue - 1)
    jsonObj_1!.UpdateString("name", value: "my_other_new_analyzer")
    jsonObj_1!.UpdateString("baseAnalyzer", value: "lucene.english")
    jsonObj_1!.UpdateString("stopwords[0]", value: "foo")
    jsonObj_1!.UpdateString("stopwords[1]", value: "bar")
    jsonObj_1!.UpdateString("stopwords[2]", value: "baz")
    jsonObj_1 = nil

    let sbRequestBody = CkoStringBuilder()
    jarr.EmitSb(sbRequestBody)

    var resp: CkoHttpResponse? = http.PTextSb("PUT", url: "https://domain.com/api/atlas/{{version}}/groups/{{ProjectID}}/clusters/{{CLUSTER-NAME}}/fts/analyzers", textData: sbRequestBody, charset: "utf-8", contentType: "application/json", md5: false, gzip: false)
    if http.LastMethodSuccess == false {
        print("\(http.LastErrorText)")
        return
    }

    print("\(resp!.StatusCode.intValue)")
    print("\(resp!.BodyStr)")
    resp = nil

}

Curl Command

curl -X PUT
	--digest -u 'username:password'
	-d '[
    {
        "name": "my_new_analyzer",
        "baseAnalyzer": "lucene.standard",
        "maxTokenLength": 48
    },
    {
        "name": "my_other_new_analyzer",
        "baseAnalyzer": "lucene.english",
        "stopwords": [
            "foo",
            "bar",
            "baz"
        ]
    }
]'
https://domain.com/api/atlas/{{version}}/groups/{{ProjectID}}/clusters/{{CLUSTER-NAME}}/fts/analyzers

Postman Collection Item JSON

{
  "name": "Update All User-Defined Analyzers for a Cluster",
  "request": {
    "method": "PUT",
    "header": [
    ],
    "body": {
      "mode": "raw",
      "raw": "[\n    {\n        \"name\": \"my_new_analyzer\",\n        \"baseAnalyzer\": \"lucene.standard\",\n        \"maxTokenLength\": 48\n    },\n    {\n        \"name\": \"my_other_new_analyzer\",\n        \"baseAnalyzer\": \"lucene.english\",\n        \"stopwords\": [\n            \"foo\",\n            \"bar\",\n            \"baz\"\n        ]\n    }\n]",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{base_url}}/api/atlas/{{version}}/groups/{{ProjectID}}/clusters/{{CLUSTER-NAME}}/fts/analyzers",
      "host": [
        "{{base_url}}"
      ],
      "path": [
        "api",
        "atlas",
        "{{version}}",
        "groups",
        "{{ProjectID}}",
        "clusters",
        "{{CLUSTER-NAME}}",
        "fts",
        "analyzers"
      ]
    },
    "description": "https://docs.atlas.mongodb.com/reference/api/fts-analyzers-update-all/"
  },
  "response": [
  ]
}