索引

没有索引时,查询会读取集合中的所有文档。几千个文档时这很快。如果有数百万个文档、需要有序结果,或要确保某个值永不重复,请在你查询的字段上创建索引。

示例使用 customers 集合以及 快速开始 中的环境变量。

我需要哪种索引?

类型 用途 值唯一 查询参数
map 按精确值查找文档:邮箱、用户名、外部 id。 是,始终唯一 value
btree 范围和排序:日期、金额、分数。值可以重复。 可选(unique) from、to、reverse
fts 按文本中的词查找文档:名称、标题、描述。 否 match

索引保存在内存中,每次写入时更新。map 索引以及设置了 unique 的 btree 索引会在每次写入前检查,因此重复的值会被拒绝。

按邮箱查找(map)

创建索引:

curl -X POST "https://inceptiondb.hola.cloud/v1/databases/$INCEPTIONDB_DATABASE/collections/customers:createIndex" \
  -H "Api-Key: $INCEPTIONDB_API_KEY" \
  -H "Api-Secret: $INCEPTIONDB_API_SECRET" \
  -d '{"name": "by_email", "type": "map", "field": "email"}'
POST /v1/databases/$INCEPTIONDB_DATABASE/collections/customers:createIndex HTTP/1.1
Host: inceptiondb.hola.cloud
Api-Key: $INCEPTIONDB_API_KEY
Api-Secret: $INCEPTIONDB_API_SECRET

{"name": "by_email", "type": "map", "field": "email"}
package main

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

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

	req, err := http.NewRequest("POST", "https://inceptiondb.hola.cloud/v1/databases/" + os.Getenv("INCEPTIONDB_DATABASE") + "/collections/customers:createIndex", strings.NewReader(body))
	if err != nil {
		panic(err)
	}
	req.Header.Set("Api-Key", os.Getenv("INCEPTIONDB_API_KEY"))
	req.Header.Set("Api-Secret", os.Getenv("INCEPTIONDB_API_SECRET"))

	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'];
$body = json_encode($payload);

$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => 'https://inceptiondb.hola.cloud/v1/databases/' . getenv('INCEPTIONDB_DATABASE') . '/collections/customers:createIndex',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => $body,
    CURLOPT_HTTPHEADER => [
        'Api-Key: ' . getenv('INCEPTIONDB_API_KEY'),
        'Api-Secret: ' . getenv('INCEPTIONDB_API_SECRET'),
    ],
]);

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

echo $response;
import os
import requests

import json

headers = {
    "Api-Key": os.environ["INCEPTIONDB_API_KEY"],
    "Api-Secret": os.environ["INCEPTIONDB_API_SECRET"],
}

payload = {"field": "email", "name": "by_email", "type": "map"}
body = json.dumps(payload)

response = requests.request(
    "POST",
    "https://inceptiondb.hola.cloud/v1/databases/" + os.environ["INCEPTIONDB_DATABASE"] + "/collections/customers:createIndex",
    headers=headers,
    data=body
)

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

const response = await fetch("https://inceptiondb.hola.cloud/v1/databases/" + process.env.INCEPTIONDB_DATABASE + "/collections/customers:createIndex", {
  method: "POST",
  headers: {
    "Api-Key": process.env.INCEPTIONDB_API_KEY,
    "Api-Secret": process.env.INCEPTIONDB_API_SECRET
  },
  body: JSON.stringify(payload)
});

console.log(await response.text());
// Fill in your values. Never put an API secret in code that runs in a public web page.
const INCEPTIONDB_DATABASE = "your-inceptiondb-database";
const INCEPTIONDB_API_KEY = "your-inceptiondb-api-key";
const INCEPTIONDB_API_SECRET = "your-inceptiondb-api-secret";

const payload = {"field": "email", "name": "by_email", "type": "map"};

const response = await fetch("https://inceptiondb.hola.cloud/v1/databases/" + INCEPTIONDB_DATABASE + "/collections/customers:createIndex", {
  method: "POST",
  headers: {
    "Api-Key": INCEPTIONDB_API_KEY,
    "Api-Secret": INCEPTIONDB_API_SECRET
  },
  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");
        var body = new ObjectMapper().writeValueAsString(payload);

        var request = HttpRequest.newBuilder()
            .uri(URI.create("https://inceptiondb.hola.cloud/v1/databases/" + System.getenv("INCEPTIONDB_DATABASE") + "/collections/customers:createIndex"))
            .method("POST", HttpRequest.BodyPublishers.ofString(body))
            .header("Api-Key", System.getenv("INCEPTIONDB_API_KEY"))
            .header("Api-Secret", System.getenv("INCEPTIONDB_API_SECRET"))
            .build();

        var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
    }
}
1{"field":"email","name":"by_email","sparse":false,"type":"map"}

按邮箱查找客户:

curl -X POST "https://inceptiondb.hola.cloud/v1/databases/$INCEPTIONDB_DATABASE/collections/customers:find" \
  -H "Api-Key: $INCEPTIONDB_API_KEY" \
  -H "Api-Secret: $INCEPTIONDB_API_SECRET" \
  -d '{"index": "by_email", "value": "grace@example.com"}'
POST /v1/databases/$INCEPTIONDB_DATABASE/collections/customers:find HTTP/1.1
Host: inceptiondb.hola.cloud
Api-Key: $INCEPTIONDB_API_KEY
Api-Secret: $INCEPTIONDB_API_SECRET

{"index": "by_email", "value": "grace@example.com"}
package main

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

func main() {
	payload := map[string]any{"index": "by_email", "value": "grace@example.com"}
	bodyBytes, err := json.Marshal(payload)
	if err != nil {
		panic(err)
	}
	body := string(bodyBytes)

	req, err := http.NewRequest("POST", "https://inceptiondb.hola.cloud/v1/databases/" + os.Getenv("INCEPTIONDB_DATABASE") + "/collections/customers:find", strings.NewReader(body))
	if err != nil {
		panic(err)
	}
	req.Header.Set("Api-Key", os.Getenv("INCEPTIONDB_API_KEY"))
	req.Header.Set("Api-Secret", os.Getenv("INCEPTIONDB_API_SECRET"))

	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 = ['index' => 'by_email', 'value' => 'grace@example.com'];
$body = json_encode($payload);

$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => 'https://inceptiondb.hola.cloud/v1/databases/' . getenv('INCEPTIONDB_DATABASE') . '/collections/customers:find',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => $body,
    CURLOPT_HTTPHEADER => [
        'Api-Key: ' . getenv('INCEPTIONDB_API_KEY'),
        'Api-Secret: ' . getenv('INCEPTIONDB_API_SECRET'),
    ],
]);

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

echo $response;
import os
import requests

import json

headers = {
    "Api-Key": os.environ["INCEPTIONDB_API_KEY"],
    "Api-Secret": os.environ["INCEPTIONDB_API_SECRET"],
}

payload = {"index": "by_email", "value": "grace@example.com"}
body = json.dumps(payload)

response = requests.request(
    "POST",
    "https://inceptiondb.hola.cloud/v1/databases/" + os.environ["INCEPTIONDB_DATABASE"] + "/collections/customers:find",
    headers=headers,
    data=body
)

print(response.text)
const payload = {"index": "by_email", "value": "grace@example.com"};

const response = await fetch("https://inceptiondb.hola.cloud/v1/databases/" + process.env.INCEPTIONDB_DATABASE + "/collections/customers:find", {
  method: "POST",
  headers: {
    "Api-Key": process.env.INCEPTIONDB_API_KEY,
    "Api-Secret": process.env.INCEPTIONDB_API_SECRET
  },
  body: JSON.stringify(payload)
});

console.log(await response.text());
// Fill in your values. Never put an API secret in code that runs in a public web page.
const INCEPTIONDB_DATABASE = "your-inceptiondb-database";
const INCEPTIONDB_API_KEY = "your-inceptiondb-api-key";
const INCEPTIONDB_API_SECRET = "your-inceptiondb-api-secret";

const payload = {"index": "by_email", "value": "grace@example.com"};

const response = await fetch("https://inceptiondb.hola.cloud/v1/databases/" + INCEPTIONDB_DATABASE + "/collections/customers:find", {
  method: "POST",
  headers: {
    "Api-Key": INCEPTIONDB_API_KEY,
    "Api-Secret": INCEPTIONDB_API_SECRET
  },
  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("index", "by_email", "value", "grace@example.com");
        var body = new ObjectMapper().writeValueAsString(payload);

        var request = HttpRequest.newBuilder()
            .uri(URI.create("https://inceptiondb.hola.cloud/v1/databases/" + System.getenv("INCEPTIONDB_DATABASE") + "/collections/customers:find"))
            .method("POST", HttpRequest.BodyPublishers.ofString(body))
            .header("Api-Key", System.getenv("INCEPTIONDB_API_KEY"))
            .header("Api-Secret", System.getenv("INCEPTIONDB_API_SECRET"))
            .build();

        var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
    }
}
1{"id":"grace","name":"Grace Hopper","email":"grace@example.com","plan":"team","country":"US","mrr":480,"tags":["vip"],"address":{"city":"New York","zip":"10001"}}

从现在起,插入一个邮箱已存在的客户会失败,返回 409 Conflict:

1{"error":{"description":"Unexpected error","message":"index add 'by_email': index conflict: field 'email' with value 'ada@example.com'"}}

范围和排序(btree)

在月收入上创建索引:

curl -X POST "https://inceptiondb.hola.cloud/v1/databases/$INCEPTIONDB_DATABASE/collections/customers:createIndex" \
  -H "Api-Key: $INCEPTIONDB_API_KEY" \
  -H "Api-Secret: $INCEPTIONDB_API_SECRET" \
  -d '{"name": "by_mrr", "type": "btree", "fields": ["mrr"]}'
POST /v1/databases/$INCEPTIONDB_DATABASE/collections/customers:createIndex HTTP/1.1
Host: inceptiondb.hola.cloud
Api-Key: $INCEPTIONDB_API_KEY
Api-Secret: $INCEPTIONDB_API_SECRET

{"name": "by_mrr", "type": "btree", "fields": ["mrr"]}
package main

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

func main() {
	payload := map[string]any{"fields": []any{"mrr"}, "name": "by_mrr", "type": "btree"}
	bodyBytes, err := json.Marshal(payload)
	if err != nil {
		panic(err)
	}
	body := string(bodyBytes)

	req, err := http.NewRequest("POST", "https://inceptiondb.hola.cloud/v1/databases/" + os.Getenv("INCEPTIONDB_DATABASE") + "/collections/customers:createIndex", strings.NewReader(body))
	if err != nil {
		panic(err)
	}
	req.Header.Set("Api-Key", os.Getenv("INCEPTIONDB_API_KEY"))
	req.Header.Set("Api-Secret", os.Getenv("INCEPTIONDB_API_SECRET"))

	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 = ['fields' => ['mrr'], 'name' => 'by_mrr', 'type' => 'btree'];
$body = json_encode($payload);

$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => 'https://inceptiondb.hola.cloud/v1/databases/' . getenv('INCEPTIONDB_DATABASE') . '/collections/customers:createIndex',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => $body,
    CURLOPT_HTTPHEADER => [
        'Api-Key: ' . getenv('INCEPTIONDB_API_KEY'),
        'Api-Secret: ' . getenv('INCEPTIONDB_API_SECRET'),
    ],
]);

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

echo $response;
import os
import requests

import json

headers = {
    "Api-Key": os.environ["INCEPTIONDB_API_KEY"],
    "Api-Secret": os.environ["INCEPTIONDB_API_SECRET"],
}

payload = {"fields": ["mrr"], "name": "by_mrr", "type": "btree"}
body = json.dumps(payload)

response = requests.request(
    "POST",
    "https://inceptiondb.hola.cloud/v1/databases/" + os.environ["INCEPTIONDB_DATABASE"] + "/collections/customers:createIndex",
    headers=headers,
    data=body
)

print(response.text)
const payload = {"fields": ["mrr"], "name": "by_mrr", "type": "btree"};

const response = await fetch("https://inceptiondb.hola.cloud/v1/databases/" + process.env.INCEPTIONDB_DATABASE + "/collections/customers:createIndex", {
  method: "POST",
  headers: {
    "Api-Key": process.env.INCEPTIONDB_API_KEY,
    "Api-Secret": process.env.INCEPTIONDB_API_SECRET
  },
  body: JSON.stringify(payload)
});

console.log(await response.text());
// Fill in your values. Never put an API secret in code that runs in a public web page.
const INCEPTIONDB_DATABASE = "your-inceptiondb-database";
const INCEPTIONDB_API_KEY = "your-inceptiondb-api-key";
const INCEPTIONDB_API_SECRET = "your-inceptiondb-api-secret";

const payload = {"fields": ["mrr"], "name": "by_mrr", "type": "btree"};

const response = await fetch("https://inceptiondb.hola.cloud/v1/databases/" + INCEPTIONDB_DATABASE + "/collections/customers:createIndex", {
  method: "POST",
  headers: {
    "Api-Key": INCEPTIONDB_API_KEY,
    "Api-Secret": INCEPTIONDB_API_SECRET
  },
  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("fields", List.of("mrr"), "name", "by_mrr", "type", "btree");
        var body = new ObjectMapper().writeValueAsString(payload);

        var request = HttpRequest.newBuilder()
            .uri(URI.create("https://inceptiondb.hola.cloud/v1/databases/" + System.getenv("INCEPTIONDB_DATABASE") + "/collections/customers:createIndex"))
            .method("POST", HttpRequest.BodyPublishers.ofString(body))
            .header("Api-Key", System.getenv("INCEPTIONDB_API_KEY"))
            .header("Api-Secret", System.getenv("INCEPTIONDB_API_SECRET"))
            .build();

        var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
    }
}
1{"fields":["mrr"],"name":"by_mrr","sparse":false,"type":"btree","unique":false}

付费在 100 到 500 之间的客户,从低到高:

curl -X POST "https://inceptiondb.hola.cloud/v1/databases/$INCEPTIONDB_DATABASE/collections/customers:find" \
  -H "Api-Key: $INCEPTIONDB_API_KEY" \
  -H "Api-Secret: $INCEPTIONDB_API_SECRET" \
  -d '{"index": "by_mrr", "from": {"mrr": 100}, "to": {"mrr": 500}, "limit": 10}'
POST /v1/databases/$INCEPTIONDB_DATABASE/collections/customers:find HTTP/1.1
Host: inceptiondb.hola.cloud
Api-Key: $INCEPTIONDB_API_KEY
Api-Secret: $INCEPTIONDB_API_SECRET

{"index": "by_mrr", "from": {"mrr": 100}, "to": {"mrr": 500}, "limit": 10}
package main

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

func main() {
	payload := map[string]any{"from": map[string]any{"mrr": 100}, "index": "by_mrr", "limit": 10, "to": map[string]any{"mrr": 500}}
	bodyBytes, err := json.Marshal(payload)
	if err != nil {
		panic(err)
	}
	body := string(bodyBytes)

	req, err := http.NewRequest("POST", "https://inceptiondb.hola.cloud/v1/databases/" + os.Getenv("INCEPTIONDB_DATABASE") + "/collections/customers:find", strings.NewReader(body))
	if err != nil {
		panic(err)
	}
	req.Header.Set("Api-Key", os.Getenv("INCEPTIONDB_API_KEY"))
	req.Header.Set("Api-Secret", os.Getenv("INCEPTIONDB_API_SECRET"))

	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 = ['from' => ['mrr' => 100], 'index' => 'by_mrr', 'limit' => 10, 'to' => ['mrr' => 500]];
$body = json_encode($payload);

$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => 'https://inceptiondb.hola.cloud/v1/databases/' . getenv('INCEPTIONDB_DATABASE') . '/collections/customers:find',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => $body,
    CURLOPT_HTTPHEADER => [
        'Api-Key: ' . getenv('INCEPTIONDB_API_KEY'),
        'Api-Secret: ' . getenv('INCEPTIONDB_API_SECRET'),
    ],
]);

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

echo $response;
import os
import requests

import json

headers = {
    "Api-Key": os.environ["INCEPTIONDB_API_KEY"],
    "Api-Secret": os.environ["INCEPTIONDB_API_SECRET"],
}

payload = {"from": {"mrr": 100}, "index": "by_mrr", "limit": 10, "to": {"mrr": 500}}
body = json.dumps(payload)

response = requests.request(
    "POST",
    "https://inceptiondb.hola.cloud/v1/databases/" + os.environ["INCEPTIONDB_DATABASE"] + "/collections/customers:find",
    headers=headers,
    data=body
)

print(response.text)
const payload = {"from": {"mrr": 100}, "index": "by_mrr", "limit": 10, "to": {"mrr": 500}};

const response = await fetch("https://inceptiondb.hola.cloud/v1/databases/" + process.env.INCEPTIONDB_DATABASE + "/collections/customers:find", {
  method: "POST",
  headers: {
    "Api-Key": process.env.INCEPTIONDB_API_KEY,
    "Api-Secret": process.env.INCEPTIONDB_API_SECRET
  },
  body: JSON.stringify(payload)
});

console.log(await response.text());
// Fill in your values. Never put an API secret in code that runs in a public web page.
const INCEPTIONDB_DATABASE = "your-inceptiondb-database";
const INCEPTIONDB_API_KEY = "your-inceptiondb-api-key";
const INCEPTIONDB_API_SECRET = "your-inceptiondb-api-secret";

const payload = {"from": {"mrr": 100}, "index": "by_mrr", "limit": 10, "to": {"mrr": 500}};

const response = await fetch("https://inceptiondb.hola.cloud/v1/databases/" + INCEPTIONDB_DATABASE + "/collections/customers:find", {
  method: "POST",
  headers: {
    "Api-Key": INCEPTIONDB_API_KEY,
    "Api-Secret": INCEPTIONDB_API_SECRET
  },
  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("from", Map.of("mrr", 100), "index", "by_mrr", "limit", 10, "to", Map.of("mrr", 500));
        var body = new ObjectMapper().writeValueAsString(payload);

        var request = HttpRequest.newBuilder()
            .uri(URI.create("https://inceptiondb.hola.cloud/v1/databases/" + System.getenv("INCEPTIONDB_DATABASE") + "/collections/customers:find"))
            .method("POST", HttpRequest.BodyPublishers.ofString(body))
            .header("Api-Key", System.getenv("INCEPTIONDB_API_KEY"))
            .header("Api-Secret", System.getenv("INCEPTIONDB_API_SECRET"))
            .build();

        var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
    }
}
1{"id":"ada","name":"Ada Lovelace","email":"ada@example.com","plan":"pro","country":"GB","mrr":120,"tags":["vip","beta"],"address":{"city":"London","zip":"N1"}}
2{"id":"grace","name":"Grace Hopper","email":"grace@example.com","plan":"team","country":"US","mrr":480,"tags":["vip"],"address":{"city":"New York","zip":"10001"}}

收入最高的客户,从高到低:"reverse": true。

curl -X POST "https://inceptiondb.hola.cloud/v1/databases/$INCEPTIONDB_DATABASE/collections/customers:find" \
  -H "Api-Key: $INCEPTIONDB_API_KEY" \
  -H "Api-Secret: $INCEPTIONDB_API_SECRET" \
  -d '{"index": "by_mrr", "reverse": true, "limit": 3}'
POST /v1/databases/$INCEPTIONDB_DATABASE/collections/customers:find HTTP/1.1
Host: inceptiondb.hola.cloud
Api-Key: $INCEPTIONDB_API_KEY
Api-Secret: $INCEPTIONDB_API_SECRET

{"index": "by_mrr", "reverse": true, "limit": 3}
package main

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

func main() {
	payload := map[string]any{"index": "by_mrr", "limit": 3, "reverse": true}
	bodyBytes, err := json.Marshal(payload)
	if err != nil {
		panic(err)
	}
	body := string(bodyBytes)

	req, err := http.NewRequest("POST", "https://inceptiondb.hola.cloud/v1/databases/" + os.Getenv("INCEPTIONDB_DATABASE") + "/collections/customers:find", strings.NewReader(body))
	if err != nil {
		panic(err)
	}
	req.Header.Set("Api-Key", os.Getenv("INCEPTIONDB_API_KEY"))
	req.Header.Set("Api-Secret", os.Getenv("INCEPTIONDB_API_SECRET"))

	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 = ['index' => 'by_mrr', 'limit' => 3, 'reverse' => true];
$body = json_encode($payload);

$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => 'https://inceptiondb.hola.cloud/v1/databases/' . getenv('INCEPTIONDB_DATABASE') . '/collections/customers:find',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => $body,
    CURLOPT_HTTPHEADER => [
        'Api-Key: ' . getenv('INCEPTIONDB_API_KEY'),
        'Api-Secret: ' . getenv('INCEPTIONDB_API_SECRET'),
    ],
]);

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

echo $response;
import os
import requests

import json

headers = {
    "Api-Key": os.environ["INCEPTIONDB_API_KEY"],
    "Api-Secret": os.environ["INCEPTIONDB_API_SECRET"],
}

payload = {"index": "by_mrr", "limit": 3, "reverse": True}
body = json.dumps(payload)

response = requests.request(
    "POST",
    "https://inceptiondb.hola.cloud/v1/databases/" + os.environ["INCEPTIONDB_DATABASE"] + "/collections/customers:find",
    headers=headers,
    data=body
)

print(response.text)
const payload = {"index": "by_mrr", "limit": 3, "reverse": true};

const response = await fetch("https://inceptiondb.hola.cloud/v1/databases/" + process.env.INCEPTIONDB_DATABASE + "/collections/customers:find", {
  method: "POST",
  headers: {
    "Api-Key": process.env.INCEPTIONDB_API_KEY,
    "Api-Secret": process.env.INCEPTIONDB_API_SECRET
  },
  body: JSON.stringify(payload)
});

console.log(await response.text());
// Fill in your values. Never put an API secret in code that runs in a public web page.
const INCEPTIONDB_DATABASE = "your-inceptiondb-database";
const INCEPTIONDB_API_KEY = "your-inceptiondb-api-key";
const INCEPTIONDB_API_SECRET = "your-inceptiondb-api-secret";

const payload = {"index": "by_mrr", "limit": 3, "reverse": true};

const response = await fetch("https://inceptiondb.hola.cloud/v1/databases/" + INCEPTIONDB_DATABASE + "/collections/customers:find", {
  method: "POST",
  headers: {
    "Api-Key": INCEPTIONDB_API_KEY,
    "Api-Secret": INCEPTIONDB_API_SECRET
  },
  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("index", "by_mrr", "limit", 3, "reverse", true);
        var body = new ObjectMapper().writeValueAsString(payload);

        var request = HttpRequest.newBuilder()
            .uri(URI.create("https://inceptiondb.hola.cloud/v1/databases/" + System.getenv("INCEPTIONDB_DATABASE") + "/collections/customers:find"))
            .method("POST", HttpRequest.BodyPublishers.ofString(body))
            .header("Api-Key", System.getenv("INCEPTIONDB_API_KEY"))
            .header("Api-Secret", System.getenv("INCEPTIONDB_API_SECRET"))
            .build();

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

注意:

  • from 和 to 是可选的;省略其中一个即为开放范围。
  • fields 可以包含多个字段,例如 ["country", "-mrr"]:先按国家,每个国家内按收入从高到低(- 表示降序)。
  • 也可以同时加上 filter:索引决定顺序和范围,过滤器再排除文档。
  • "unique": true 会拒绝重复值,与 map 索引一样。

按词搜索(fts)

curl -X POST "https://inceptiondb.hola.cloud/v1/databases/$INCEPTIONDB_DATABASE/collections/customers:createIndex" \
  -H "Api-Key: $INCEPTIONDB_API_KEY" \
  -H "Api-Secret: $INCEPTIONDB_API_SECRET" \
  -d '{"name": "search_name", "type": "fts", "field": "name"}'
POST /v1/databases/$INCEPTIONDB_DATABASE/collections/customers:createIndex HTTP/1.1
Host: inceptiondb.hola.cloud
Api-Key: $INCEPTIONDB_API_KEY
Api-Secret: $INCEPTIONDB_API_SECRET

{"name": "search_name", "type": "fts", "field": "name"}
package main

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

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

	req, err := http.NewRequest("POST", "https://inceptiondb.hola.cloud/v1/databases/" + os.Getenv("INCEPTIONDB_DATABASE") + "/collections/customers:createIndex", strings.NewReader(body))
	if err != nil {
		panic(err)
	}
	req.Header.Set("Api-Key", os.Getenv("INCEPTIONDB_API_KEY"))
	req.Header.Set("Api-Secret", os.Getenv("INCEPTIONDB_API_SECRET"))

	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' => 'name', 'name' => 'search_name', 'type' => 'fts'];
$body = json_encode($payload);

$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => 'https://inceptiondb.hola.cloud/v1/databases/' . getenv('INCEPTIONDB_DATABASE') . '/collections/customers:createIndex',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => $body,
    CURLOPT_HTTPHEADER => [
        'Api-Key: ' . getenv('INCEPTIONDB_API_KEY'),
        'Api-Secret: ' . getenv('INCEPTIONDB_API_SECRET'),
    ],
]);

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

echo $response;
import os
import requests

import json

headers = {
    "Api-Key": os.environ["INCEPTIONDB_API_KEY"],
    "Api-Secret": os.environ["INCEPTIONDB_API_SECRET"],
}

payload = {"field": "name", "name": "search_name", "type": "fts"}
body = json.dumps(payload)

response = requests.request(
    "POST",
    "https://inceptiondb.hola.cloud/v1/databases/" + os.environ["INCEPTIONDB_DATABASE"] + "/collections/customers:createIndex",
    headers=headers,
    data=body
)

print(response.text)
const payload = {"field": "name", "name": "search_name", "type": "fts"};

const response = await fetch("https://inceptiondb.hola.cloud/v1/databases/" + process.env.INCEPTIONDB_DATABASE + "/collections/customers:createIndex", {
  method: "POST",
  headers: {
    "Api-Key": process.env.INCEPTIONDB_API_KEY,
    "Api-Secret": process.env.INCEPTIONDB_API_SECRET
  },
  body: JSON.stringify(payload)
});

console.log(await response.text());
// Fill in your values. Never put an API secret in code that runs in a public web page.
const INCEPTIONDB_DATABASE = "your-inceptiondb-database";
const INCEPTIONDB_API_KEY = "your-inceptiondb-api-key";
const INCEPTIONDB_API_SECRET = "your-inceptiondb-api-secret";

const payload = {"field": "name", "name": "search_name", "type": "fts"};

const response = await fetch("https://inceptiondb.hola.cloud/v1/databases/" + INCEPTIONDB_DATABASE + "/collections/customers:createIndex", {
  method: "POST",
  headers: {
    "Api-Key": INCEPTIONDB_API_KEY,
    "Api-Secret": INCEPTIONDB_API_SECRET
  },
  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", "name", "name", "search_name", "type", "fts");
        var body = new ObjectMapper().writeValueAsString(payload);

        var request = HttpRequest.newBuilder()
            .uri(URI.create("https://inceptiondb.hola.cloud/v1/databases/" + System.getenv("INCEPTIONDB_DATABASE") + "/collections/customers:createIndex"))
            .method("POST", HttpRequest.BodyPublishers.ofString(body))
            .header("Api-Key", System.getenv("INCEPTIONDB_API_KEY"))
            .header("Api-Secret", System.getenv("INCEPTIONDB_API_SECRET"))
            .build();

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

查找名字中包含 grace 的客户,不区分大小写:

curl -X POST "https://inceptiondb.hola.cloud/v1/databases/$INCEPTIONDB_DATABASE/collections/customers:find" \
  -H "Api-Key: $INCEPTIONDB_API_KEY" \
  -H "Api-Secret: $INCEPTIONDB_API_SECRET" \
  -d '{"index": "search_name", "match": "grace", "limit": 10}'
POST /v1/databases/$INCEPTIONDB_DATABASE/collections/customers:find HTTP/1.1
Host: inceptiondb.hola.cloud
Api-Key: $INCEPTIONDB_API_KEY
Api-Secret: $INCEPTIONDB_API_SECRET

{"index": "search_name", "match": "grace", "limit": 10}
package main

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

func main() {
	payload := map[string]any{"index": "search_name", "limit": 10, "match": "grace"}
	bodyBytes, err := json.Marshal(payload)
	if err != nil {
		panic(err)
	}
	body := string(bodyBytes)

	req, err := http.NewRequest("POST", "https://inceptiondb.hola.cloud/v1/databases/" + os.Getenv("INCEPTIONDB_DATABASE") + "/collections/customers:find", strings.NewReader(body))
	if err != nil {
		panic(err)
	}
	req.Header.Set("Api-Key", os.Getenv("INCEPTIONDB_API_KEY"))
	req.Header.Set("Api-Secret", os.Getenv("INCEPTIONDB_API_SECRET"))

	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 = ['index' => 'search_name', 'limit' => 10, 'match' => 'grace'];
$body = json_encode($payload);

$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => 'https://inceptiondb.hola.cloud/v1/databases/' . getenv('INCEPTIONDB_DATABASE') . '/collections/customers:find',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => $body,
    CURLOPT_HTTPHEADER => [
        'Api-Key: ' . getenv('INCEPTIONDB_API_KEY'),
        'Api-Secret: ' . getenv('INCEPTIONDB_API_SECRET'),
    ],
]);

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

echo $response;
import os
import requests

import json

headers = {
    "Api-Key": os.environ["INCEPTIONDB_API_KEY"],
    "Api-Secret": os.environ["INCEPTIONDB_API_SECRET"],
}

payload = {"index": "search_name", "limit": 10, "match": "grace"}
body = json.dumps(payload)

response = requests.request(
    "POST",
    "https://inceptiondb.hola.cloud/v1/databases/" + os.environ["INCEPTIONDB_DATABASE"] + "/collections/customers:find",
    headers=headers,
    data=body
)

print(response.text)
const payload = {"index": "search_name", "limit": 10, "match": "grace"};

const response = await fetch("https://inceptiondb.hola.cloud/v1/databases/" + process.env.INCEPTIONDB_DATABASE + "/collections/customers:find", {
  method: "POST",
  headers: {
    "Api-Key": process.env.INCEPTIONDB_API_KEY,
    "Api-Secret": process.env.INCEPTIONDB_API_SECRET
  },
  body: JSON.stringify(payload)
});

console.log(await response.text());
// Fill in your values. Never put an API secret in code that runs in a public web page.
const INCEPTIONDB_DATABASE = "your-inceptiondb-database";
const INCEPTIONDB_API_KEY = "your-inceptiondb-api-key";
const INCEPTIONDB_API_SECRET = "your-inceptiondb-api-secret";

const payload = {"index": "search_name", "limit": 10, "match": "grace"};

const response = await fetch("https://inceptiondb.hola.cloud/v1/databases/" + INCEPTIONDB_DATABASE + "/collections/customers:find", {
  method: "POST",
  headers: {
    "Api-Key": INCEPTIONDB_API_KEY,
    "Api-Secret": INCEPTIONDB_API_SECRET
  },
  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("index", "search_name", "limit", 10, "match", "grace");
        var body = new ObjectMapper().writeValueAsString(payload);

        var request = HttpRequest.newBuilder()
            .uri(URI.create("https://inceptiondb.hola.cloud/v1/databases/" + System.getenv("INCEPTIONDB_DATABASE") + "/collections/customers:find"))
            .method("POST", HttpRequest.BodyPublishers.ofString(body))
            .header("Api-Key", System.getenv("INCEPTIONDB_API_KEY"))
            .header("Api-Secret", System.getenv("INCEPTIONDB_API_SECRET"))
            .build();

        var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
    }
}
1{"id":"grace","name":"Grace Hopper","email":"grace@example.com","plan":"team","country":"US","mrr":480,"tags":["vip"],"address":{"city":"New York","zip":"10001"}}

没有该字段的文档

没有被索引字段的文档会正常保存,但不属于该索引:使用该索引的查询不会返回它们。不带 index 的查询仍能找到它们。创建索引时设置 "sparse": true 可以明确表达这一点:

curl -X POST "https://inceptiondb.hola.cloud/v1/databases/$INCEPTIONDB_DATABASE/collections/customers:createIndex" \
  -H "Api-Key: $INCEPTIONDB_API_KEY" \
  -H "Api-Secret: $INCEPTIONDB_API_SECRET" \
  -d '{"name": "by_company", "type": "btree", "fields": ["company"], "sparse": true}'
POST /v1/databases/$INCEPTIONDB_DATABASE/collections/customers:createIndex HTTP/1.1
Host: inceptiondb.hola.cloud
Api-Key: $INCEPTIONDB_API_KEY
Api-Secret: $INCEPTIONDB_API_SECRET

{"name": "by_company", "type": "btree", "fields": ["company"], "sparse": true}
package main

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

func main() {
	payload := map[string]any{"fields": []any{"company"}, "name": "by_company", "sparse": true, "type": "btree"}
	bodyBytes, err := json.Marshal(payload)
	if err != nil {
		panic(err)
	}
	body := string(bodyBytes)

	req, err := http.NewRequest("POST", "https://inceptiondb.hola.cloud/v1/databases/" + os.Getenv("INCEPTIONDB_DATABASE") + "/collections/customers:createIndex", strings.NewReader(body))
	if err != nil {
		panic(err)
	}
	req.Header.Set("Api-Key", os.Getenv("INCEPTIONDB_API_KEY"))
	req.Header.Set("Api-Secret", os.Getenv("INCEPTIONDB_API_SECRET"))

	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 = ['fields' => ['company'], 'name' => 'by_company', 'sparse' => true, 'type' => 'btree'];
$body = json_encode($payload);

$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => 'https://inceptiondb.hola.cloud/v1/databases/' . getenv('INCEPTIONDB_DATABASE') . '/collections/customers:createIndex',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => $body,
    CURLOPT_HTTPHEADER => [
        'Api-Key: ' . getenv('INCEPTIONDB_API_KEY'),
        'Api-Secret: ' . getenv('INCEPTIONDB_API_SECRET'),
    ],
]);

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

echo $response;
import os
import requests

import json

headers = {
    "Api-Key": os.environ["INCEPTIONDB_API_KEY"],
    "Api-Secret": os.environ["INCEPTIONDB_API_SECRET"],
}

payload = {"fields": ["company"], "name": "by_company", "sparse": True, "type": "btree"}
body = json.dumps(payload)

response = requests.request(
    "POST",
    "https://inceptiondb.hola.cloud/v1/databases/" + os.environ["INCEPTIONDB_DATABASE"] + "/collections/customers:createIndex",
    headers=headers,
    data=body
)

print(response.text)
const payload = {"fields": ["company"], "name": "by_company", "sparse": true, "type": "btree"};

const response = await fetch("https://inceptiondb.hola.cloud/v1/databases/" + process.env.INCEPTIONDB_DATABASE + "/collections/customers:createIndex", {
  method: "POST",
  headers: {
    "Api-Key": process.env.INCEPTIONDB_API_KEY,
    "Api-Secret": process.env.INCEPTIONDB_API_SECRET
  },
  body: JSON.stringify(payload)
});

console.log(await response.text());
// Fill in your values. Never put an API secret in code that runs in a public web page.
const INCEPTIONDB_DATABASE = "your-inceptiondb-database";
const INCEPTIONDB_API_KEY = "your-inceptiondb-api-key";
const INCEPTIONDB_API_SECRET = "your-inceptiondb-api-secret";

const payload = {"fields": ["company"], "name": "by_company", "sparse": true, "type": "btree"};

const response = await fetch("https://inceptiondb.hola.cloud/v1/databases/" + INCEPTIONDB_DATABASE + "/collections/customers:createIndex", {
  method: "POST",
  headers: {
    "Api-Key": INCEPTIONDB_API_KEY,
    "Api-Secret": INCEPTIONDB_API_SECRET
  },
  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("fields", List.of("company"), "name", "by_company", "sparse", true, "type", "btree");
        var body = new ObjectMapper().writeValueAsString(payload);

        var request = HttpRequest.newBuilder()
            .uri(URI.create("https://inceptiondb.hola.cloud/v1/databases/" + System.getenv("INCEPTIONDB_DATABASE") + "/collections/customers:createIndex"))
            .method("POST", HttpRequest.BodyPublishers.ofString(body))
            .header("Api-Key", System.getenv("INCEPTIONDB_API_KEY"))
            .header("Api-Secret", System.getenv("INCEPTIONDB_API_SECRET"))
            .build();

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

列出和删除索引

curl -X POST "https://inceptiondb.hola.cloud/v1/databases/$INCEPTIONDB_DATABASE/collections/customers:listIndexes" \
  -H "Api-Key: $INCEPTIONDB_API_KEY" \
  -H "Api-Secret: $INCEPTIONDB_API_SECRET"
POST /v1/databases/$INCEPTIONDB_DATABASE/collections/customers:listIndexes HTTP/1.1
Host: inceptiondb.hola.cloud
Api-Key: $INCEPTIONDB_API_KEY
Api-Secret: $INCEPTIONDB_API_SECRET
package main

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

func main() {
	req, err := http.NewRequest("POST", "https://inceptiondb.hola.cloud/v1/databases/" + os.Getenv("INCEPTIONDB_DATABASE") + "/collections/customers:listIndexes", nil)
	if err != nil {
		panic(err)
	}
	req.Header.Set("Api-Key", os.Getenv("INCEPTIONDB_API_KEY"))
	req.Header.Set("Api-Secret", os.Getenv("INCEPTIONDB_API_SECRET"))

	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://inceptiondb.hola.cloud/v1/databases/' . getenv('INCEPTIONDB_DATABASE') . '/collections/customers:listIndexes',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_HTTPHEADER => [
        'Api-Key: ' . getenv('INCEPTIONDB_API_KEY'),
        'Api-Secret: ' . getenv('INCEPTIONDB_API_SECRET'),
    ],
]);

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

echo $response;
import os
import requests

headers = {
    "Api-Key": os.environ["INCEPTIONDB_API_KEY"],
    "Api-Secret": os.environ["INCEPTIONDB_API_SECRET"],
}

response = requests.request(
    "POST",
    "https://inceptiondb.hola.cloud/v1/databases/" + os.environ["INCEPTIONDB_DATABASE"] + "/collections/customers:listIndexes",
    headers=headers
)

print(response.text)
const response = await fetch("https://inceptiondb.hola.cloud/v1/databases/" + process.env.INCEPTIONDB_DATABASE + "/collections/customers:listIndexes", {
  method: "POST",
  headers: {
    "Api-Key": process.env.INCEPTIONDB_API_KEY,
    "Api-Secret": process.env.INCEPTIONDB_API_SECRET
  }
});

console.log(await response.text());
// Fill in your values. Never put an API secret in code that runs in a public web page.
const INCEPTIONDB_DATABASE = "your-inceptiondb-database";
const INCEPTIONDB_API_KEY = "your-inceptiondb-api-key";
const INCEPTIONDB_API_SECRET = "your-inceptiondb-api-secret";

const response = await fetch("https://inceptiondb.hola.cloud/v1/databases/" + INCEPTIONDB_DATABASE + "/collections/customers:listIndexes", {
  method: "POST",
  headers: {
    "Api-Key": INCEPTIONDB_API_KEY,
    "Api-Secret": INCEPTIONDB_API_SECRET
  }
});

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://inceptiondb.hola.cloud/v1/databases/" + System.getenv("INCEPTIONDB_DATABASE") + "/collections/customers:listIndexes"))
            .method("POST", HttpRequest.BodyPublishers.noBody())
            .header("Api-Key", System.getenv("INCEPTIONDB_API_KEY"))
            .header("Api-Secret", System.getenv("INCEPTIONDB_API_SECRET"))
            .build();

        var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
    }
}
1[{"fields":["company"],"name":"by_company","sparse":true,"type":"btree","unique":false},{"field":"email","name":"by_email","sparse":false,"type":"map"},{"fields":["mrr"],"name":"by_mrr","sparse":false,"type":"btree","unique":false},{"field":"name","name":"search_name","type":"fts"}]

删除索引不会影响文档(204 No Content):

curl -X POST "https://inceptiondb.hola.cloud/v1/databases/$INCEPTIONDB_DATABASE/collections/customers:dropIndex" \
  -H "Api-Key: $INCEPTIONDB_API_KEY" \
  -H "Api-Secret: $INCEPTIONDB_API_SECRET" \
  -d '{"name": "by_mrr"}'
POST /v1/databases/$INCEPTIONDB_DATABASE/collections/customers:dropIndex HTTP/1.1
Host: inceptiondb.hola.cloud
Api-Key: $INCEPTIONDB_API_KEY
Api-Secret: $INCEPTIONDB_API_SECRET

{"name": "by_mrr"}
package main

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

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

	req, err := http.NewRequest("POST", "https://inceptiondb.hola.cloud/v1/databases/" + os.Getenv("INCEPTIONDB_DATABASE") + "/collections/customers:dropIndex", strings.NewReader(body))
	if err != nil {
		panic(err)
	}
	req.Header.Set("Api-Key", os.Getenv("INCEPTIONDB_API_KEY"))
	req.Header.Set("Api-Secret", os.Getenv("INCEPTIONDB_API_SECRET"))

	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_mrr'];
$body = json_encode($payload);

$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => 'https://inceptiondb.hola.cloud/v1/databases/' . getenv('INCEPTIONDB_DATABASE') . '/collections/customers:dropIndex',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => $body,
    CURLOPT_HTTPHEADER => [
        'Api-Key: ' . getenv('INCEPTIONDB_API_KEY'),
        'Api-Secret: ' . getenv('INCEPTIONDB_API_SECRET'),
    ],
]);

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

echo $response;
import os
import requests

import json

headers = {
    "Api-Key": os.environ["INCEPTIONDB_API_KEY"],
    "Api-Secret": os.environ["INCEPTIONDB_API_SECRET"],
}

payload = {"name": "by_mrr"}
body = json.dumps(payload)

response = requests.request(
    "POST",
    "https://inceptiondb.hola.cloud/v1/databases/" + os.environ["INCEPTIONDB_DATABASE"] + "/collections/customers:dropIndex",
    headers=headers,
    data=body
)

print(response.text)
const payload = {"name": "by_mrr"};

const response = await fetch("https://inceptiondb.hola.cloud/v1/databases/" + process.env.INCEPTIONDB_DATABASE + "/collections/customers:dropIndex", {
  method: "POST",
  headers: {
    "Api-Key": process.env.INCEPTIONDB_API_KEY,
    "Api-Secret": process.env.INCEPTIONDB_API_SECRET
  },
  body: JSON.stringify(payload)
});

console.log(await response.text());
// Fill in your values. Never put an API secret in code that runs in a public web page.
const INCEPTIONDB_DATABASE = "your-inceptiondb-database";
const INCEPTIONDB_API_KEY = "your-inceptiondb-api-key";
const INCEPTIONDB_API_SECRET = "your-inceptiondb-api-secret";

const payload = {"name": "by_mrr"};

const response = await fetch("https://inceptiondb.hola.cloud/v1/databases/" + INCEPTIONDB_DATABASE + "/collections/customers:dropIndex", {
  method: "POST",
  headers: {
    "Api-Key": INCEPTIONDB_API_KEY,
    "Api-Secret": INCEPTIONDB_API_SECRET
  },
  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_mrr");
        var body = new ObjectMapper().writeValueAsString(payload);

        var request = HttpRequest.newBuilder()
            .uri(URI.create("https://inceptiondb.hola.cloud/v1/databases/" + System.getenv("INCEPTIONDB_DATABASE") + "/collections/customers:dropIndex"))
            .method("POST", HttpRequest.BodyPublishers.ofString(body))
            .header("Api-Key", System.getenv("INCEPTIONDB_API_KEY"))
            .header("Api-Secret", System.getenv("INCEPTIONDB_API_SECRET"))
            .build();

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

在 控制台 中,Schema 标签页可以一键在任意字段上创建索引。

评论

发表评论