Chilkat Online Tools

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

Back to Collection Items

IncludeFile "CkJsonArray.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    ; This example assumes the Chilkat API to have been previously unlocked.
    ; See Global Unlock Sample for sample code.

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i

    CkHttp::setCkDigestAuth(http, 1)
    CkHttp::setCkLogin(http, "username")
    CkHttp::setCkPassword(http, "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"
    ;     ]
    ;   }
    ; ]

    jarr.i = CkJsonArray::ckCreate()
    If jarr.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonArray::ckAddObjectAt(jarr,-1)
    jsonObj_1.i = CkJsonArray::ckObjectAt(jarr,CkJsonArray::ckSize(jarr) - 1)
    CkJsonObject::ckUpdateString(jsonObj_1,"name","my_new_analyzer")
    CkJsonObject::ckUpdateString(jsonObj_1,"baseAnalyzer","lucene.standard")
    CkJsonObject::ckUpdateInt(jsonObj_1,"maxTokenLength",48)
    CkJsonObject::ckDispose(jsonObj_1)

    CkJsonArray::ckAddObjectAt(jarr,-1)
    jsonObj_1 = CkJsonArray::ckObjectAt(jarr,CkJsonArray::ckSize(jarr) - 1)
    CkJsonObject::ckUpdateString(jsonObj_1,"name","my_other_new_analyzer")
    CkJsonObject::ckUpdateString(jsonObj_1,"baseAnalyzer","lucene.english")
    CkJsonObject::ckUpdateString(jsonObj_1,"stopwords[0]","foo")
    CkJsonObject::ckUpdateString(jsonObj_1,"stopwords[1]","bar")
    CkJsonObject::ckUpdateString(jsonObj_1,"stopwords[2]","baz")
    CkJsonObject::ckDispose(jsonObj_1)

    sbRequestBody.i = CkStringBuilder::ckCreate()
    If sbRequestBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonArray::ckEmitSb(jarr,sbRequestBody)

    resp.i = CkHttp::ckPTextSb(http,"PUT","https://domain.com/api/atlas/{{version}}/groups/{{ProjectID}}/clusters/{{CLUSTER-NAME}}/fts/analyzers",sbRequestBody,"utf-8","application/json",0,0)
    If CkHttp::ckLastMethodSuccess(http) = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkJsonArray::ckDispose(jarr)
        CkStringBuilder::ckDispose(sbRequestBody)
        ProcedureReturn
    EndIf

    Debug Str(CkHttpResponse::ckStatusCode(resp))
    Debug CkHttpResponse::ckBodyStr(resp)
    CkHttpResponse::ckDispose(resp)



    CkHttp::ckDispose(http)
    CkJsonArray::ckDispose(jarr)
    CkStringBuilder::ckDispose(sbRequestBody)


    ProcedureReturn
EndProcedure

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": [
  ]
}