IBM Cloud Docs
电影演示数据库示例

电影演示数据库示例

以下示例使用 HTTP 为样本数据库创建 text 索引。

POST /my-movies/_index HTTP/1.1
Host: $SERVICE_URL
Content-Type: application/json
{
	"index": {},
	"type": "text"
}

请参阅使用命令行为样本数据库创建 text 索引的以下示例:

curl "$SERVICE_URL/my-movies/_index" \
	-X POST \
	-H "Content-Type: application/json" \
	-d '{"index": {}, "type": "text"}'
import com.ibm.cloud.cloudant.v1.Cloudant;
import com.ibm.cloud.cloudant.v1.model.IndexDefinition;
import com.ibm.cloud.cloudant.v1.model.IndexResult;
import com.ibm.cloud.cloudant.v1.model.PostIndexOptions;

Cloudant service = Cloudant.newInstance();

IndexDefinition indexDefinition = new IndexDefinition.Builder()
   .build();

PostIndexOptions indexOptions = new PostIndexOptions.Builder()
    .db("my-movies")
    .index(indexDefinition)
    .type("text")
    .build();

IndexResult response =
    service.postIndex(indexOptions).execute()
        .getResult();

System.out.println(response);
import { CloudantV1 } from '@ibm-cloud/cloudant';

const service = CloudantV1.newInstance({});

let index: CloudantV1.IndexDefinition = {}

service.postIndex({
  db: 'my-movies',
  index: index,
  type: 'text'
}).then(response => {
  console.log(response.result);
});
from ibmcloudant.cloudant_v1 import CloudantV1, IndexDefinition

service = CloudantV1.new_instance()

index = IndexDefinition()

response = service.post_index(
  db='my-movies',
  index=index,
  type='text'
).get_result()

print(response)
postIndexOptions := service.NewPostIndexOptions(
    "my-movies",
    &cloudantv1.IndexDefinition{},
)
postIndexOptions.SetType("text")

indexResult, _, err := service.PostIndex(postIndexOptions)
if err != nil {
    panic(err)
}

b, _ := json.MarshalIndent(indexResult, "", "  ")
fmt.Println(string(b))

先前的 Go 示例需要以下导入块:

import (
   "encoding/json"
   "fmt"
   "github.com/IBM/cloudant-go-sdk/cloudantv1"
)

成功创建 text 索引后,请参阅以下示例响应:

{
    "id": "_design/bf936ad6eb87f03c2b42ae153377462844575e40",
    "name": "bf936ad6eb87f03c2b42ae153377462844575e40",
	"result": "created"
}

使用全文本索引时获得的结果中最明显的差异是包含一个大的 bookmark 字段。 原因是 text 索引与基于视图的索引不同。 为了更灵活,当您处理从全文本查询获取的结果时,可以提供 bookmark 值作为请求主体的一部分。 使用 bookmark 来指定您需要的结果页面。

实际 bookmark 值很长,因此此处的示例具有为清晰起见而截断的值。

请参阅使用 HTTP 在数据库中搜索特定文档的以下示例:

POST /my-movies/_find HTTP/1.1
Host: $SERVICE_URL
Content-Type: application/json
{
  "selector": {
    "Person_name":"Zoe Saldana"
  }
}

请参阅使用命令行在数据库中搜索特定文档的以下示例:

curl -X POST -H "Content-Type: application/json" \
	"$SERVICE_URL/my-movies/_find" \
	-d '{"selector": {"Person_name":"Zoe Saldana"}}'
import com.ibm.cloud.cloudant.v1.Cloudant;
import com.ibm.cloud.cloudant.v1.model.FindResult;
import com.ibm.cloud.cloudant.v1.model.PostFindOptions;

import java.util.Collections;
import java.util.Map;

Cloudant service = Cloudant.newInstance();

Map<String, Object> selector = Collections.singletonMap(
    "Person_name", "Zoe Saldana");

PostFindOptions findOptions = new PostFindOptions.Builder()
    .db("my-movies")
    .selector(selector)
    .build();

FindResult response =
    service.postFind(findOptions).execute()
        .getResult();

System.out.println(response);
import { CloudantV1 } from '@ibm-cloud/cloudant';

const service = CloudantV1.newInstance({});

let selector: CloudantV1.JsonObject = {
  Person_name: 'Zoe Saldana'
};

service.postFind({
  db: 'my-movies',
  selector: selector,
}).then(response => {
  console.log(response.result);
});
from ibmcloudant.cloudant_v1 import CloudantV1

service = CloudantV1.new_instance()

response = service.post_find(
  db='my-movies',
  selector={'Person_name': 'Zoe Saldana'}
).get_result()

print(response)
postFindOptions := service.NewPostFindOptions(
    "my-movies",
    map[string]interface{}{
        "Person_name": "Zoe Saldana",
    },
)

findResult, _, err := service.PostFind(postFindOptions)
if err != nil {
    panic(err)
}

b, _ := json.MarshalIndent(findResult, "", "  ")
fmt.Println(string(b))

先前的 Go 示例需要以下导入块:

import (
   "encoding/json"
   "fmt"
   "github.com/IBM/cloudant-go-sdk/cloudantv1"
)

请参阅搜索的以下示例结果:

{
	"docs": [
		{
			"_id": "d9e6a7ae2363d6cfe81af75a3941110b",
			"_rev": "1-556aec0e89fa13769fbf59d651411528",
			"Movie_runtime": 162,
			"Movie_rating": "PG-13",
			"Person_name": "Zoe Saldana",
			"Movie_genre": "AVYS",
			"Movie_name": "Avatar",
			"Movie_earnings_rank": "1",
			"Person_pob": "New Jersey, USA",
			"Movie_year": 2009,
			"Person_dob": "1978-06-19"
		}
	],
	"bookmark": "g2wA ... Omo"
}

请参阅以下使用 HTTP 进行稍微复杂的搜索的示例:

POST /my-movies/_find HTTP/1.1
Host: $SERVICE_URL
Content-Type: application/json
{
	"selector": {
		"Person_name":"Robert De Niro",
		"Movie_year": 1978
	}
}

请参阅以下使用命令行的示例,以获取稍微复杂的搜索:

curl -X POST -H "Content-Type: application/json" \
	"$SERVICE_URL/my-movies/_find" \
	-d '{"selector": {"Person_name":"Robert De Niro", "Movie_year": 1978}}'
import com.ibm.cloud.cloudant.v1.Cloudant;
import com.ibm.cloud.cloudant.v1.model.FindResult;
import com.ibm.cloud.cloudant.v1.model.PostFindOptions;

import java.util.HashMap;
import java.util.Map;

Cloudant service = Cloudant.newInstance();

Map<String, Object> selector = new HashMap<>();
selector.put("Person_name", "Robert De Niro");
selector.put("Movie_year", 1978);

PostFindOptions findOptions = new PostFindOptions.Builder()
    .db("my-movies")
    .selector(selector)
    .build();

FindResult response =
    service.postFind(findOptions).execute()
        .getResult();

System.out.println(response);
import { CloudantV1 } from '@ibm-cloud/cloudant';

const service = CloudantV1.newInstance({});

let selector: CloudantV1.JsonObject = {
  Person_name: 'Robert De Niro',
  Movie_year: 1978
};

service.postFind({
  db: 'my-movies',
  selector: selector,
}).then(response => {
  console.log(response.result);
});
from ibmcloudant.cloudant_v1 import CloudantV1

service = CloudantV1.new_instance()

response = service.post_find(
  db='my-movies',
  selector={'Person_name': 'Robert De Niro', 'Movie_year': 1978}
).get_result()

print(response)
postFindOptions := service.NewPostFindOptions(
    "my-movies",
    map[string]interface{}{
        "Person_name": "Robert De Niro",
        "Movie_year": 1978,
    },
)

findResult, _, err := service.PostFind(postFindOptions)
if err != nil {
    panic(err)
}

b, _ := json.MarshalIndent(findResult, "", "  ")
fmt.Println(string(b))

先前的 Go 示例需要以下导入块:

import (
   "encoding/json"
   "fmt"
   "github.com/IBM/cloudant-go-sdk/cloudantv1"
)

请参阅搜索的以下示例结果:

{
	"docs": [
		{
			"_id": "d9e6a7ae2363d6cfe81af75a392eb9f2",
			"_rev": "1-9faa75d7ea524448b1456a6c69a4391a",
			"Movie_runtime": 183,
			"Movie_rating": "R",
			"Person_name": "Robert De Niro",
			"Movie_genre": "DW",
			"Movie_name": "Deer Hunter, The",
			"Person_pob": "New York, New York, USA",
			"Movie_year": 1978,
			"Person_dob": "1943-08-17"
		}
	],
	"bookmark": "g2w ... c2o"
}

请参阅以下使用 HTTP 在范围内进行搜索的示例:

POST /my-movies/_find HTTP/1.1
Host: $SERVICE_URL
Content-Type: application/json
{
  "selector": {
    "Person_name":"Robert De Niro",
    "Movie_year": {
      "$in": [1974, 2009]
    }
  }
}

请参阅以下使用命令行在范围内进行搜索的示例:

curl -X POST -H "Content-Type: application/json" \
	"$SERVICE_URL/my-movies/_find" \
	-d '{"selector": {"Person_name":"Robert De Niro", "Movie_year": { "$in": [1974, 2009]}}}'
import com.ibm.cloud.cloudant.v1.Cloudant;
import com.ibm.cloud.cloudant.v1.model.ExplainResult;
import com.ibm.cloud.cloudant.v1.model.PostExplainOptions;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

Cloudant service = Cloudant.newInstance();

Map<String, Object> selector = new HashMap<>();
selector.put("Person_name", "Robert De Niro");
selector.put("Movie_year",
    Collections.singletonMap("$in", Arrays.asList(1978, 2009)));

PostExplainOptions explainOptions =
    new PostExplainOptions.Builder()
        .db("my-movies")
        .selector(selector)
        .build();

ExplainResult response =
    service.postExplain(explainOptions).execute()
        .getResult();

System.out.println(response);
import { CloudantV1 } from '@ibm-cloud/cloudant';

const service = CloudantV1.newInstance({});

let selector: CloudantV1.JsonObject = {
  Person_name: 'Robert De Niro',
  Movie_year: {'$in': [1978, 2009]}
};

service.postFind({
  db: 'my-movies',
  selector: selector,
}).then(response => {
  console.log(response.result);
});
from ibmcloudant.cloudant_v1 import CloudantV1

service = CloudantV1.new_instance()

response = service.post_find(
  db='my-movies',
  selector={'Person_name': 'Robert De Niro',
    'Movie_year': {'$in': (1978, 2009)}}
).get_result()

print(response)
postFindOptions := service.NewPostFindOptions(
    "my-movies",
    map[string]interface{}{
        "Person_name": "Robert De Niro",
        "Movie_year": map[string][]interface{}{
            "$in": []interface{}{1978, 2009},
        },
    },
)

findResult, _, err := service.PostFind(postFindOptions)
if err != nil {
    panic(err)
}

b, _ := json.MarshalIndent(findResult, "", "  ")
fmt.Println(string(b))

先前的 Go 示例需要以下导入块:

import (
   "encoding/json"
   "fmt"
   "github.com/IBM/cloudant-go-sdk/cloudantv1"
)

请参阅搜索的以下示例结果:

{
	"docs": [
		{
			"_id": "d9e6a7ae2363d6cfe81af75a392eb9f2",
			"_rev": "1-9faa75d7ea524448b1456a6c69a4391a",
			"Movie_runtime": 183,
			"Movie_rating": "R",
			"Person_name": "Robert De Niro",
			"Movie_genre": "DW",
			"Movie_name": "Deer Hunter, The",
			"Person_pob": "New York, New York, USA",
			"Movie_year": 1978,
			"Person_dob": "1943-08-17"
		}
	],
	"bookmark": "g2w ... c2o"
}