Chilkat Online Tools

SQL Server / MeiliSearch v0.24 / Add or replace documents

Back to Collection Items

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

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

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

    -- [
    --   {
    --     "id": 2,
    --     "title": "Pride and Prejudice",
    --     "author": "Jane Austin",
    --     "genre": "romance",
    --     "price": 3.5
    --   },
    --   {
    --     "id": 456,
    --     "title": "Le Petit Prince",
    --     "author": "Antoine de Saint-Exupéry",
    --     "genre": "adventure",
    --     "price": 10.0
    --   },
    --   {
    --     "id": 1,
    --     "title": "Alice In Wonderland",
    --     "author": "Lewis Carroll",
    --     "genre": "fantasy",
    --     "price": 25.99
    --   },
    --   {
    --     "id": 1344,
    --     "title": "The Hobbit",
    --     "author": "J. R. R. Tolkien",
    --     "genre": "fantasy"
    --   },
    --   {
    --     "id": 4,
    --     "title": "Harry Potter and the Half-Blood Prince",
    --     "author": "J. K. Rowling",
    --     "genre": "fantasy"
    --   },
    --   {
    --     "id": 42,
    --     "title": "The Hitchhiker's Guide to the Galaxy",
    --     "author": "Douglas Adams"
    --   }
    -- ]

    DECLARE @jarr int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonArray', @jarr OUT

    EXEC sp_OAMethod @jarr, 'AddObjectAt', @success OUT, -1
    DECLARE @jsonObj_1 int
    EXEC sp_OAGetProperty @jarr, 'Size', @iTmp0 OUT
    EXEC sp_OAMethod @jarr, 'ObjectAt', @jsonObj_1 OUT, @iTmp0 - 1
    EXEC sp_OAMethod @jsonObj_1, 'UpdateInt', @success OUT, 'id', 2
    EXEC sp_OAMethod @jsonObj_1, 'UpdateString', @success OUT, 'title', 'Pride and Prejudice'
    EXEC sp_OAMethod @jsonObj_1, 'UpdateString', @success OUT, 'author', 'Jane Austin'
    EXEC sp_OAMethod @jsonObj_1, 'UpdateString', @success OUT, 'genre', 'romance'
    EXEC sp_OAMethod @jsonObj_1, 'UpdateNumber', @success OUT, 'price', '3.5'
    EXEC @hr = sp_OADestroy @jsonObj_1

    EXEC sp_OAMethod @jarr, 'AddObjectAt', @success OUT, -1
    EXEC sp_OAGetProperty @jarr, 'Size', @iTmp0 OUT
    EXEC sp_OAMethod @jarr, 'ObjectAt', @jsonObj_1 OUT, @iTmp0 - 1
    EXEC sp_OAMethod @jsonObj_1, 'UpdateInt', @success OUT, 'id', 456
    EXEC sp_OAMethod @jsonObj_1, 'UpdateString', @success OUT, 'title', 'Le Petit Prince'
    EXEC sp_OAMethod @jsonObj_1, 'UpdateString', @success OUT, 'author', 'Antoine de Saint-Exupéry'
    EXEC sp_OAMethod @jsonObj_1, 'UpdateString', @success OUT, 'genre', 'adventure'
    EXEC sp_OAMethod @jsonObj_1, 'UpdateNumber', @success OUT, 'price', '10.0'
    EXEC @hr = sp_OADestroy @jsonObj_1

    EXEC sp_OAMethod @jarr, 'AddObjectAt', @success OUT, -1
    EXEC sp_OAGetProperty @jarr, 'Size', @iTmp0 OUT
    EXEC sp_OAMethod @jarr, 'ObjectAt', @jsonObj_1 OUT, @iTmp0 - 1
    EXEC sp_OAMethod @jsonObj_1, 'UpdateInt', @success OUT, 'id', 1
    EXEC sp_OAMethod @jsonObj_1, 'UpdateString', @success OUT, 'title', 'Alice In Wonderland'
    EXEC sp_OAMethod @jsonObj_1, 'UpdateString', @success OUT, 'author', 'Lewis Carroll'
    EXEC sp_OAMethod @jsonObj_1, 'UpdateString', @success OUT, 'genre', 'fantasy'
    EXEC sp_OAMethod @jsonObj_1, 'UpdateNumber', @success OUT, 'price', '25.99'
    EXEC @hr = sp_OADestroy @jsonObj_1

    EXEC sp_OAMethod @jarr, 'AddObjectAt', @success OUT, -1
    EXEC sp_OAGetProperty @jarr, 'Size', @iTmp0 OUT
    EXEC sp_OAMethod @jarr, 'ObjectAt', @jsonObj_1 OUT, @iTmp0 - 1
    EXEC sp_OAMethod @jsonObj_1, 'UpdateInt', @success OUT, 'id', 1344
    EXEC sp_OAMethod @jsonObj_1, 'UpdateString', @success OUT, 'title', 'The Hobbit'
    EXEC sp_OAMethod @jsonObj_1, 'UpdateString', @success OUT, 'author', 'J. R. R. Tolkien'
    EXEC sp_OAMethod @jsonObj_1, 'UpdateString', @success OUT, 'genre', 'fantasy'
    EXEC @hr = sp_OADestroy @jsonObj_1

    EXEC sp_OAMethod @jarr, 'AddObjectAt', @success OUT, -1
    EXEC sp_OAGetProperty @jarr, 'Size', @iTmp0 OUT
    EXEC sp_OAMethod @jarr, 'ObjectAt', @jsonObj_1 OUT, @iTmp0 - 1
    EXEC sp_OAMethod @jsonObj_1, 'UpdateInt', @success OUT, 'id', 4
    EXEC sp_OAMethod @jsonObj_1, 'UpdateString', @success OUT, 'title', 'Harry Potter and the Half-Blood Prince'
    EXEC sp_OAMethod @jsonObj_1, 'UpdateString', @success OUT, 'author', 'J. K. Rowling'
    EXEC sp_OAMethod @jsonObj_1, 'UpdateString', @success OUT, 'genre', 'fantasy'
    EXEC @hr = sp_OADestroy @jsonObj_1

    EXEC sp_OAMethod @jarr, 'AddObjectAt', @success OUT, -1
    EXEC sp_OAGetProperty @jarr, 'Size', @iTmp0 OUT
    EXEC sp_OAMethod @jarr, 'ObjectAt', @jsonObj_1 OUT, @iTmp0 - 1
    EXEC sp_OAMethod @jsonObj_1, 'UpdateInt', @success OUT, 'id', 42
    EXEC sp_OAMethod @jsonObj_1, 'UpdateString', @success OUT, 'title', 'The Hitchhiker''s Guide to the Galaxy'
    EXEC sp_OAMethod @jsonObj_1, 'UpdateString', @success OUT, 'author', 'Douglas Adams'
    EXEC @hr = sp_OADestroy @jsonObj_1

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'X-Meili-Api-Key', 'masterKey'

    DECLARE @sbRequestBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbRequestBody OUT

    EXEC sp_OAMethod @jarr, 'EmitSb', @success OUT, @sbRequestBody

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PTextSb', @resp OUT, 'POST', 'http://localhost:7700/indexes/indexUID/documents', @sbRequestBody, 'utf-8', 'application/json', 0, 0
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @jarr
        EXEC @hr = sp_OADestroy @sbRequestBody
        RETURN
      END

    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    PRINT @iTmp0
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    PRINT @sTmp0
    EXEC @hr = sp_OADestroy @resp


    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @jarr
    EXEC @hr = sp_OADestroy @sbRequestBody


END
GO

Curl Command

curl -X POST
	-H "X-Meili-Api-Key: masterKey"
	-d '[
  { "id": 2,    "title": "Pride and Prejudice",                    "author": "Jane Austin",              "genre": "romance",    "price": 3.5 },
  { "id": 456,  "title": "Le Petit Prince",                        "author": "Antoine de Saint-Exupéry", "genre": "adventure" , "price": 10.0 },
  { "id": 1,    "title": "Alice In Wonderland",                    "author": "Lewis Carroll",            "genre": "fantasy",    "price": 25.99 },
  { "id": 1344, "title": "The Hobbit",                             "author": "J. R. R. Tolkien",         "genre": "fantasy" },
  { "id": 4,    "title": "Harry Potter and the Half-Blood Prince", "author": "J. K. Rowling",            "genre": "fantasy" },
  { "id": 42,   "title": "The Hitchhiker\'s Guide to the Galaxy",   "author": "Douglas Adams" }
]'
http://localhost:7700/indexes/indexUID/documents

Postman Collection Item JSON

{
  "name": "Add or replace documents",
  "request": {
    "method": "POST",
    "header": [
    ],
    "body": {
      "mode": "raw",
      "raw": "[\n  { \"id\": 2,    \"title\": \"Pride and Prejudice\",                    \"author\": \"Jane Austin\",              \"genre\": \"romance\",    \"price\": 3.5 },\n  { \"id\": 456,  \"title\": \"Le Petit Prince\",                        \"author\": \"Antoine de Saint-Exupéry\", \"genre\": \"adventure\" , \"price\": 10.0 },\n  { \"id\": 1,    \"title\": \"Alice In Wonderland\",                    \"author\": \"Lewis Carroll\",            \"genre\": \"fantasy\",    \"price\": 25.99 },\n  { \"id\": 1344, \"title\": \"The Hobbit\",                             \"author\": \"J. R. R. Tolkien\",         \"genre\": \"fantasy\" },\n  { \"id\": 4,    \"title\": \"Harry Potter and the Half-Blood Prince\", \"author\": \"J. K. Rowling\",            \"genre\": \"fantasy\" },\n  { \"id\": 42,   \"title\": \"The Hitchhiker's Guide to the Galaxy\",   \"author\": \"Douglas Adams\" }\n]",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{url}}/indexes/{{indexUID}}/documents",
      "host": [
        "{{url}}"
      ],
      "path": [
        "indexes",
        "{{indexUID}}",
        "documents"
      ],
      "query": [
        {
          "key": "primaryKey",
          "value": "id",
          "disabled": true
        }
      ]
    }
  },
  "response": [
  ]
}