索引管理

索引 action 用于管理集合的二级索引。

认证

需要 Api-KeyApi-Secret,或者在数据库 owner 被允许时使用 Glue owner token。

创建索引

支持的索引类型是 mapbtree

curl -X POST "https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:createIndex" \
  -H "Api-Key: 1abbe476-6ad6-4b97-9cca-6deb6ab2901d" \
  -H "Api-Secret: 4bda6d52-762b-4e5d-bed7-85614c13b8bf" \
  -H "Content-Type: application/json" \
  -d '{"name":"by-email","type":"map","field":"email","unique":true}'
POST /v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:createIndex HTTP/1.1
Host: api.hola.cloud
Api-Key: 1abbe476-6ad6-4b97-9cca-6deb6ab2901d
Api-Secret: 4bda6d52-762b-4e5d-bed7-85614c13b8bf
Content-Type: application/json

{"name":"by-email","type":"map","field":"email","unique":true}
package main

import (
	"fmt"
	"io"
	"net/http"
	"encoding/json"
	"strings"
)

func main() {
	payload := map[string]any{"field": "email", "name": "by-email", "type": "map", "unique": true}
	bodyBytes, err := json.Marshal(payload)
	if err != nil {
		panic(err)
	}
	body := string(bodyBytes)

	req, err := http.NewRequest("POST", "https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:createIndex", strings.NewReader(body))
	if err != nil {
		panic(err)
	}
	req.Header.Set("Api-Key", "1abbe476-6ad6-4b97-9cca-6deb6ab2901d")
	req.Header.Set("Api-Secret", "4bda6d52-762b-4e5d-bed7-85614c13b8bf")
	req.Header.Set("Content-Type", "application/json")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	responseBody, err := io.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(responseBody))
}
<?php
$payload = ['field' => 'email', 'name' => 'by-email', 'type' => 'map', 'unique' => true];
$body = json_encode($payload);

$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => 'https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:createIndex',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => $body,
    CURLOPT_HTTPHEADER => [
        'Api-Key: 1abbe476-6ad6-4b97-9cca-6deb6ab2901d',
        'Api-Secret: 4bda6d52-762b-4e5d-bed7-85614c13b8bf',
        'Content-Type: application/json',
    ],
]);

$response = curl_exec($ch);
if ($response === false) {
    throw new Exception(curl_error($ch));
}
curl_close($ch);

echo $response;
import requests

import json

headers = {
    "Api-Key": "1abbe476-6ad6-4b97-9cca-6deb6ab2901d",
    "Api-Secret": "4bda6d52-762b-4e5d-bed7-85614c13b8bf",
    "Content-Type": "application/json",
}

payload = {"field": "email", "name": "by-email", "type": "map", "unique": True}
body = json.dumps(payload)

response = requests.request(
    "POST",
    "https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:createIndex",
    headers=headers,
    data=body
)

print(response.text)
const payload = {"field": "email", "name": "by-email", "type": "map", "unique": true};

const response = await fetch("https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:createIndex", {
  method: "POST",
  headers: {
    "Api-Key": "1abbe476-6ad6-4b97-9cca-6deb6ab2901d",
    "Api-Secret": "4bda6d52-762b-4e5d-bed7-85614c13b8bf",
    "Content-Type": "application/json"
  },
  body: JSON.stringify(payload)
});

console.log(await response.text());
const payload = {"field": "email", "name": "by-email", "type": "map", "unique": true};

const response = await fetch("https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:createIndex", {
  method: "POST",
  headers: {
    "Api-Key": "1abbe476-6ad6-4b97-9cca-6deb6ab2901d",
    "Api-Secret": "4bda6d52-762b-4e5d-bed7-85614c13b8bf",
    "Content-Type": "application/json"
  },
  body: JSON.stringify(payload)
});

const text = await response.text();
console.log(text);
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
import java.util.List;

public class Main {
    public static void main(String[] args) throws Exception {
        var payload = Map.of("field", "email", "name", "by-email", "type", "map", "unique", true);
        var body = new ObjectMapper().writeValueAsString(payload);

        var request = HttpRequest.newBuilder()
            .uri(URI.create("https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:createIndex"))
            .method("POST", HttpRequest.BodyPublishers.ofString(body))
            .header("Api-Key", "1abbe476-6ad6-4b97-9cca-6deb6ab2901d")
            .header("Api-Secret", "4bda6d52-762b-4e5d-bed7-85614c13b8bf")
            .header("Content-Type", "application/json")
            .build();

        var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
    }
}

响应状态:201 Created

1{"name":"by-email","type":"map","field":"email","unique":true}

列出索引

curl -X POST "https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:listIndexes" \
  -H "Api-Key: 1abbe476-6ad6-4b97-9cca-6deb6ab2901d" \
  -H "Api-Secret: 4bda6d52-762b-4e5d-bed7-85614c13b8bf"
POST /v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:listIndexes HTTP/1.1
Host: api.hola.cloud
Api-Key: 1abbe476-6ad6-4b97-9cca-6deb6ab2901d
Api-Secret: 4bda6d52-762b-4e5d-bed7-85614c13b8bf
package main

import (
	"fmt"
	"io"
	"net/http"
)

func main() {
	req, err := http.NewRequest("POST", "https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:listIndexes", nil)
	if err != nil {
		panic(err)
	}
	req.Header.Set("Api-Key", "1abbe476-6ad6-4b97-9cca-6deb6ab2901d")
	req.Header.Set("Api-Secret", "4bda6d52-762b-4e5d-bed7-85614c13b8bf")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	responseBody, err := io.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(responseBody))
}
<?php
$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => 'https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:listIndexes',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_HTTPHEADER => [
        'Api-Key: 1abbe476-6ad6-4b97-9cca-6deb6ab2901d',
        'Api-Secret: 4bda6d52-762b-4e5d-bed7-85614c13b8bf',
    ],
]);

$response = curl_exec($ch);
if ($response === false) {
    throw new Exception(curl_error($ch));
}
curl_close($ch);

echo $response;
import requests

headers = {
    "Api-Key": "1abbe476-6ad6-4b97-9cca-6deb6ab2901d",
    "Api-Secret": "4bda6d52-762b-4e5d-bed7-85614c13b8bf",
}

response = requests.request(
    "POST",
    "https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:listIndexes",
    headers=headers
)

print(response.text)
const response = await fetch("https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:listIndexes", {
  method: "POST",
  headers: {
    "Api-Key": "1abbe476-6ad6-4b97-9cca-6deb6ab2901d",
    "Api-Secret": "4bda6d52-762b-4e5d-bed7-85614c13b8bf"
  }
});

console.log(await response.text());
const response = await fetch("https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:listIndexes", {
  method: "POST",
  headers: {
    "Api-Key": "1abbe476-6ad6-4b97-9cca-6deb6ab2901d",
    "Api-Secret": "4bda6d52-762b-4e5d-bed7-85614c13b8bf"
  }
});

const text = await response.text();
console.log(text);
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class Main {
    public static void main(String[] args) throws Exception {
        var request = HttpRequest.newBuilder()
            .uri(URI.create("https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:listIndexes"))
            .method("POST", HttpRequest.BodyPublishers.noBody())
            .header("Api-Key", "1abbe476-6ad6-4b97-9cca-6deb6ab2901d")
            .header("Api-Secret", "4bda6d52-762b-4e5d-bed7-85614c13b8bf")
            .build();

        var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
    }
}

响应:

1[{"name":"by-email","type":"map","field":"email","unique":true}]

获取索引

curl -X POST "https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:getIndex" \
  -H "Api-Key: 1abbe476-6ad6-4b97-9cca-6deb6ab2901d" \
  -H "Api-Secret: 4bda6d52-762b-4e5d-bed7-85614c13b8bf" \
  -H "Content-Type: application/json" \
  -d '{"name":"by-email"}'
POST /v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:getIndex HTTP/1.1
Host: api.hola.cloud
Api-Key: 1abbe476-6ad6-4b97-9cca-6deb6ab2901d
Api-Secret: 4bda6d52-762b-4e5d-bed7-85614c13b8bf
Content-Type: application/json

{"name":"by-email"}
package main

import (
	"fmt"
	"io"
	"net/http"
	"encoding/json"
	"strings"
)

func main() {
	payload := map[string]any{"name": "by-email"}
	bodyBytes, err := json.Marshal(payload)
	if err != nil {
		panic(err)
	}
	body := string(bodyBytes)

	req, err := http.NewRequest("POST", "https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:getIndex", strings.NewReader(body))
	if err != nil {
		panic(err)
	}
	req.Header.Set("Api-Key", "1abbe476-6ad6-4b97-9cca-6deb6ab2901d")
	req.Header.Set("Api-Secret", "4bda6d52-762b-4e5d-bed7-85614c13b8bf")
	req.Header.Set("Content-Type", "application/json")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	responseBody, err := io.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(responseBody))
}
<?php
$payload = ['name' => 'by-email'];
$body = json_encode($payload);

$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => 'https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:getIndex',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => $body,
    CURLOPT_HTTPHEADER => [
        'Api-Key: 1abbe476-6ad6-4b97-9cca-6deb6ab2901d',
        'Api-Secret: 4bda6d52-762b-4e5d-bed7-85614c13b8bf',
        'Content-Type: application/json',
    ],
]);

$response = curl_exec($ch);
if ($response === false) {
    throw new Exception(curl_error($ch));
}
curl_close($ch);

echo $response;
import requests

import json

headers = {
    "Api-Key": "1abbe476-6ad6-4b97-9cca-6deb6ab2901d",
    "Api-Secret": "4bda6d52-762b-4e5d-bed7-85614c13b8bf",
    "Content-Type": "application/json",
}

payload = {"name": "by-email"}
body = json.dumps(payload)

response = requests.request(
    "POST",
    "https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:getIndex",
    headers=headers,
    data=body
)

print(response.text)
const payload = {"name": "by-email"};

const response = await fetch("https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:getIndex", {
  method: "POST",
  headers: {
    "Api-Key": "1abbe476-6ad6-4b97-9cca-6deb6ab2901d",
    "Api-Secret": "4bda6d52-762b-4e5d-bed7-85614c13b8bf",
    "Content-Type": "application/json"
  },
  body: JSON.stringify(payload)
});

console.log(await response.text());
const payload = {"name": "by-email"};

const response = await fetch("https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:getIndex", {
  method: "POST",
  headers: {
    "Api-Key": "1abbe476-6ad6-4b97-9cca-6deb6ab2901d",
    "Api-Secret": "4bda6d52-762b-4e5d-bed7-85614c13b8bf",
    "Content-Type": "application/json"
  },
  body: JSON.stringify(payload)
});

const text = await response.text();
console.log(text);
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
import java.util.List;

public class Main {
    public static void main(String[] args) throws Exception {
        var payload = Map.of("name", "by-email");
        var body = new ObjectMapper().writeValueAsString(payload);

        var request = HttpRequest.newBuilder()
            .uri(URI.create("https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:getIndex"))
            .method("POST", HttpRequest.BodyPublishers.ofString(body))
            .header("Api-Key", "1abbe476-6ad6-4b97-9cca-6deb6ab2901d")
            .header("Api-Secret", "4bda6d52-762b-4e5d-bed7-85614c13b8bf")
            .header("Content-Type", "application/json")
            .build();

        var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
    }
}

响应:

1{"name":"by-email","type":"map","field":"email","unique":true}

删除索引

curl -X POST "https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:dropIndex" \
  -H "Api-Key: 1abbe476-6ad6-4b97-9cca-6deb6ab2901d" \
  -H "Api-Secret: 4bda6d52-762b-4e5d-bed7-85614c13b8bf" \
  -H "Content-Type: application/json" \
  -d '{"name":"by-email"}'
POST /v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:dropIndex HTTP/1.1
Host: api.hola.cloud
Api-Key: 1abbe476-6ad6-4b97-9cca-6deb6ab2901d
Api-Secret: 4bda6d52-762b-4e5d-bed7-85614c13b8bf
Content-Type: application/json

{"name":"by-email"}
package main

import (
	"fmt"
	"io"
	"net/http"
	"encoding/json"
	"strings"
)

func main() {
	payload := map[string]any{"name": "by-email"}
	bodyBytes, err := json.Marshal(payload)
	if err != nil {
		panic(err)
	}
	body := string(bodyBytes)

	req, err := http.NewRequest("POST", "https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:dropIndex", strings.NewReader(body))
	if err != nil {
		panic(err)
	}
	req.Header.Set("Api-Key", "1abbe476-6ad6-4b97-9cca-6deb6ab2901d")
	req.Header.Set("Api-Secret", "4bda6d52-762b-4e5d-bed7-85614c13b8bf")
	req.Header.Set("Content-Type", "application/json")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	responseBody, err := io.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(responseBody))
}
<?php
$payload = ['name' => 'by-email'];
$body = json_encode($payload);

$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => 'https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:dropIndex',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => $body,
    CURLOPT_HTTPHEADER => [
        'Api-Key: 1abbe476-6ad6-4b97-9cca-6deb6ab2901d',
        'Api-Secret: 4bda6d52-762b-4e5d-bed7-85614c13b8bf',
        'Content-Type: application/json',
    ],
]);

$response = curl_exec($ch);
if ($response === false) {
    throw new Exception(curl_error($ch));
}
curl_close($ch);

echo $response;
import requests

import json

headers = {
    "Api-Key": "1abbe476-6ad6-4b97-9cca-6deb6ab2901d",
    "Api-Secret": "4bda6d52-762b-4e5d-bed7-85614c13b8bf",
    "Content-Type": "application/json",
}

payload = {"name": "by-email"}
body = json.dumps(payload)

response = requests.request(
    "POST",
    "https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:dropIndex",
    headers=headers,
    data=body
)

print(response.text)
const payload = {"name": "by-email"};

const response = await fetch("https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:dropIndex", {
  method: "POST",
  headers: {
    "Api-Key": "1abbe476-6ad6-4b97-9cca-6deb6ab2901d",
    "Api-Secret": "4bda6d52-762b-4e5d-bed7-85614c13b8bf",
    "Content-Type": "application/json"
  },
  body: JSON.stringify(payload)
});

console.log(await response.text());
const payload = {"name": "by-email"};

const response = await fetch("https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:dropIndex", {
  method: "POST",
  headers: {
    "Api-Key": "1abbe476-6ad6-4b97-9cca-6deb6ab2901d",
    "Api-Secret": "4bda6d52-762b-4e5d-bed7-85614c13b8bf",
    "Content-Type": "application/json"
  },
  body: JSON.stringify(payload)
});

const text = await response.text();
console.log(text);
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
import java.util.List;

public class Main {
    public static void main(String[] args) throws Exception {
        var payload = Map.of("name", "by-email");
        var body = new ObjectMapper().writeValueAsString(payload);

        var request = HttpRequest.newBuilder()
            .uri(URI.create("https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:dropIndex"))
            .method("POST", HttpRequest.BodyPublishers.ofString(body))
            .header("Api-Key", "1abbe476-6ad6-4b97-9cca-6deb6ab2901d")
            .header("Api-Secret", "4bda6d52-762b-4e5d-bed7-85614c13b8bf")
            .header("Content-Type", "application/json")
            .build();

        var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
    }
}

响应状态:204 No Content

评论

发表评论