/documents 路由允许您创建、管理和删除文档。 了解有关文档的更多信息。

使用 POST 获取文档

POST
/indexes/{index_uid}/documents/fetch
获取一组文档。 使用 offsetlimit 浏览文档。
如果不首先将属性明确添加到 filterableAttributes 列表中,filter 将无法工作。 在我们的专用指南中了解有关筛选器的更多信息。

路径参数

名称类型描述
index_uid *字符串请求索引的 uid

请求体

名称类型默认值描述
offset整数0要跳过的文档数量
limit整数20要返回的文档数量
fields字符串数组/null*要显示的文档属性(区分大小写,逗号分隔)
筛选字符串/字符串数组的数组/null不适用根据 filterableAttributes 列表中的属性细化结果
retrieveVectors布尔值false在搜索结果中返回文档向量数据
sortnull以数组或逗号分隔字符串形式编写的属性列表
| ids | 主键数组 | null | 根据文档的主键返回文档 |
发送空负载 (--data-binary '{}') 将返回索引中的所有文档。

响应

名称类型描述
results数组文档数组
offset整数跳过的文档数量
limit整数返回的文档数量
总数整数索引中的文档总数

返回的文档顺序

/indexes/{index_uid}/documents/fetch/indexes/{index_uid}/documents 响应不按照主键的顺序返回文档。

示例

curl \
  -X POST MEILISEARCH_URL/indexes/books/documents/fetch \
  -H 'Content-Type: application/json' \
  --data-binary '{
    "filter": "(rating > 3 AND (genres = Adventure OR genres = Fiction)) AND language = English",
    "fields": ["title", "genres", "rating", "language"],
    "limit": 3
  }'

响应:200 Ok

{
  "results": [
    {
      "title": "The Travels of Ibn Battuta",
      "genres": [
        "Travel",
        "Adventure"
      ],
      "language": "English",
      "rating": 4.5
    },
    {
      "title": "Pride and Prejudice",
      "genres": [
        "Classics",
        "Fiction",
        "Romance",
        "Literature"
      ],
      "language": "English",
      "rating": 4
    },

  ],
  "offset": 0,
  "limit": 3,
  "total": 5
}

使用 GET 获取文档

此端点将在不久的将来弃用。请考虑使用 POST /indexes/{index_uid}/documents/fetch 代替。
GET
/indexes/{index_uid}/documents
获取一组文档。 使用查询参数 offsetlimit,您可以浏览所有文档。filter 必须是字符串。要创建筛选表达式,请使用 ANDOR
如果不首先将属性明确添加到 filterableAttributes 列表中,filter 将无法工作。 在我们的专用指南中了解有关筛选器的更多信息。

路径参数

名称类型描述
index_uid *字符串请求索引的 uid

查询参数

查询参数默认值描述
offset0要跳过的文档数量
limit20要返回的文档数量
fields*要显示的文档属性(区分大小写,逗号分隔)
筛选不适用根据 filterableAttributes 列表中的属性细化结果
retrieveVectorsfalse在搜索结果中返回文档向量数据
sortnull逗号分隔的属性列表
idsnull根据文档的主键返回文档

响应

名称类型描述
results数组文档数组
offset整数跳过的文档数量
limit整数返回的文档数量
总数整数索引中的文档总数

返回的文档顺序

/indexes/{index_uid}/documents/fetch/indexes/{index_uid}/documents 响应不按照主键的顺序返回文档。

示例

curl \
  -X GET 'MEILISEARCH_URL/indexes/movies/documents?limit=2&filter=genres=action'

响应:200 Ok

{
  "results": [
    {
      "id": 364,
      "title": "Batman Returns",
      "overview": "While Batman deals with a deformed man calling himself the Penguin, an employee of a corrupt businessman transforms into the Catwoman.",
      "genres": [
        "Action",
        "Fantasy"
      ],
      "poster": "https://image.tmdb.org/t/p/w500/jKBjeXM7iBBV9UkUcOXx3m7FSHY.jpg",
      "release_date": 708912000
    },
    {
      "id": 13851,
      "title": " Batman: Gotham Knight",
      "overview": "A collection of key events mark Bruce Wayne's life as he journeys from beginner to Dark Knight.",
      "genres": [
        "Animation",
        "Action",
        "Adventure"
      ],
      "poster": "https://image.tmdb.org/t/p/w500/f3xUrqo7yEiU0guk2Ua3Znqiw6S.jpg",
      "release_date": 1215475200
    }
  ],
  "offset": 0,
  "limit": 2,
  "total": 5403
}

获取单个文档

GET
/indexes/{index_uid}/documents/{document_id}
使用其唯一 ID 获取单个文档。

路径参数

名称类型描述
index_uid *字符串请求索引的 uid
document_id *字符串/整数请求文档的文档 ID

查询参数

查询参数默认值描述
fields*要显示的文档属性(区分大小写,逗号分隔)
retrieveVectorsfalse在搜索结果中返回文档向量数据

示例

curl \
  -X GET 'MEILISEARCH_URL/indexes/movies/documents/25684?fields=id,title,poster,release_date'

响应:200 Ok

{
  "id": 25684,
  "title": "American Ninja 5",
  "poster": "https://image.tmdb.org/t/p/w1280/iuAQVI4mvjI83wnirpD8GVNRVuY.jpg",
  "release_date": "1993-01-01"
}

添加或替换文档

POST
/indexes/{index_uid}/documents
添加文档数组或在它们已存在时替换它们。如果提供的索引不存在,它将被创建。 如果您发送一个已存在的文档(相同的文档 ID),则整个现有文档将被新文档覆盖。新文档中不再存在的字段将被删除。要对文档进行部分更新,请参阅添加或更新文档端点。 此端点接受以下内容类型:
  • application/json
  • application/x-ndjson
  • text/csv

路径参数

名称类型描述
index_uid *字符串请求索引的 uid

查询参数

查询参数默认值描述
primaryKeynull索引的主键
csvDelimiter","配置分隔 CSV 字段的字符。必须是包含一个 ASCII 字符的字符串。
配置 csvDelimiter 并发送内容类型非 CSV 的数据将导致 Meilisearch 抛出错误。
如果您想在添加文档时设置索引的主键,只能在第一次向索引添加文档时完成。此后,如果给定 primaryKey 参数,它将被忽略。

请求体

文档数组。每个文档都表示为一个 JSON 对象。
[
  {
    "id": 287947,
    "title": "Shazam",
    "poster": "https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg",
    "overview": "A boy is given the ability to become an adult superhero in times of need with a single magic word.",
    "release_date": "2019-03-23"
  }
]

_vectors

_vectors 是一个特殊的文档属性,包含一个带有用于 AI 驱动搜索的向量嵌入的对象。 _vectors 对象的每个键都必须是已配置的嵌入器名称,并对应于一个包含两个字段 embeddingsregenerate 的嵌套对象:
[
  {
    "id": 452,
    "title": "Female Trouble",
    "overview": "Delinquent high school student Dawn Davenport runs away from home and embarks upon a life of crime.",
    "_vectors": {
      "default": {
        "embeddings": [0.1, 0.2, 0.3],
        "regenerate": false
      },
      "ollama": {
        "embeddings": [0.4, 0.12, 0.6],
        "regenerate": true
      }
    }
  }
]
embeddings 是一个可选字段。它必须是一个数字数组,代表该文档的单个嵌入。它也可以是数字数组的数组,代表该文档的多个嵌入。embeddings 默认为 null regenerate 是一个强制字段。它必须是布尔值。如果 regeneratetrue,Meilisearch 会立即并每次更新文档时自动为该文档生成嵌入。如果 regeneratefalse,Meilisearch 会在文档更新时保留嵌入的最后一个值。 您也可以使用数组简写向文档添加嵌入:
"_vectors": {
  "default": [0.003, 0.1, 0.75]
}
使用简写添加的向量嵌入在 Meilisearch 生成新嵌入时不会被替换。以上示例等同于
"default": {
  "embeddings": [0.003, 0.1, 0.75],
  "regenerate": false
}
如果 _vectors 内嵌入器的键为空或 null,Meilisearch 会将该文档视为不包含该嵌入器的任何嵌入。在 AI 驱动的搜索中,此文档将最后返回。

示例

curl \
  -X POST 'MEILISEARCH_URL/indexes/movies/documents' \
  -H 'Content-Type: application/json' \
  --data-binary '[
    {
      "id": 287947,
      "title": "Shazam",
      "poster": "https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg",
      "overview": "A boy is given the ability to become an adult superhero in times of need with a single magic word.",
      "release_date": "2019-03-23"
    }
  ]'

响应:202 Accepted

{
    "taskUid": 1,
    "indexUid": "movies",
    "status": "enqueued",
    "type": "documentAdditionOrUpdate",
    "enqueuedAt": "2021-08-11T09:25:53.000000Z"
}
您可以使用此 taskUid 获取有关任务状态的更多详细信息。

添加或更新文档

PUT
/indexes/{index_uid}/documents
添加文档列表或在它们已存在时更新它们。如果提供的索引不存在,它将被创建。 如果您发送一个已存在的文档(相同的文档 ID),则旧文档将根据新文档的字段进行部分更新。因此,新文档中不存在的任何字段都将被保留且保持不变。 要完全覆盖文档,请查看添加或替换文档路由 如果您想通过此路由设置索引的主键,则只能在第一次向索引添加文档时执行此操作。如果您尝试在向索引添加文档后设置主键,则任务将返回错误。 此端点接受以下内容类型:
  • application/json
  • application/x-ndjson
  • text/csv

路径参数

名称类型描述
index_uid *字符串请求索引的 uid

查询参数

查询参数默认值描述
primaryKeynull文档的主键
csvDelimiter","配置分隔 CSV 字段的字符。必须是包含一个 ASCII 字符的字符串。
配置 csvDelimiter 并发送内容类型非 CSV 的数据将导致 Meilisearch 抛出错误。

请求体

文档数组。每个文档都表示为一个 JSON 对象。
[
  {
    "id": 287947,
    "title": "Shazam ⚡️"
  }
]

示例

curl \
  -X PUT 'MEILISEARCH_URL/indexes/movies/documents' \
  -H 'Content-Type: application/json' \
  --data-binary '[
    {
      "id": 287947,
      "title": "Shazam ⚡️",
      "genres": "comedy"
    }
  ]'
此文档是添加或替换文档中找到的文档的更新。 文档匹配是因为它们具有相同主键id: 287947。此路由将更新 title 字段,因为它从 Shazam 更改为 Shazam ⚡️,并将新的 genres 字段添加到该文档。文档的其余部分将保持不变。

响应:202 Accepted

{
    "taskUid": 1,
    "indexUid": "movies",
    "status": "enqueued",
    "type": "documentAdditionOrUpdate",
    "enqueuedAt": "2021-08-11T09:25:53.000000Z"
}
您可以使用此 taskUid 获取有关任务状态的更多详细信息。

使用函数更新文档 实验性

POST
/indexes/{index_uid}/documents/edit
使用 RHAI 函数直接在 Meilisearch 中编辑一个或多个文档。
这是一个实验性功能。使用实验性功能端点激活它。
curl \
  -X PATCH 'MEILISEARCH_URL/experimental-features/' \
  -H 'Content-Type: application/json' \
  --data-binary '{
    "editDocumentsByFunction": true
  }'

路径参数

名称类型描述
index_uid *字符串请求索引的 uid

查询参数

查询参数默认值描述
函数null包含 RHAI 函数的字符串
筛选null包含筛选表达式的字符串
上下文null一个包含 Meilisearch 应可用于编辑函数的数据的对象

函数

function 必须是包含 Meilisearch 将应用于所有选定文档的 RHAI 函数的字符串。默认情况下,此函数可以访问一个变量 doc,它表示您当前正在编辑的文档。这是一个必填字段。

筛选

filter 必须是包含筛选表达式的字符串。当您只想将 function 应用于数据库中文档的子集时,请使用 filter

上下文

使用 context 将数据传递到 function 范围。默认情况下,函数只能访问它正在编辑的文档。

示例

curl \
-X POST 'MEILISEARCH_URL/indexes/INDEX_NAME/documents/edit' \
-H 'Content-Type: application/json' \
--data-binary '{
  "function": "doc.title = `${doc.title.to_upper()}`"
}'

删除所有文档

DELETE
/indexes/{index_uid}/documents
删除指定索引中的所有文档。

路径参数

名称类型描述
index_uid *字符串请求索引的 uid

示例

curl \
  -X DELETE 'MEILISEARCH_URL/indexes/movies/documents'

响应:202 Accepted

{
    "taskUid": 1,
    "indexUid": "movies",
    "status": "enqueued",
    "type": "documentDeletion",
    "enqueuedAt": "2021-08-11T09:25:53.000000Z"
}
您可以使用此 taskUid 获取有关任务状态的更多详细信息。

删除单个文档

DELETE
/indexes/{index_uid}/documents/{document_id}
根据其唯一 ID 删除一个文档。

路径参数

名称类型描述
index_uid *字符串请求索引的 uid
document_id *字符串/整数请求文档的文档 ID

示例

curl \
  -X DELETE 'MEILISEARCH_URL/indexes/movies/documents/25684'

响应:202 Accepted

{
    "taskUid": 1,
    "indexUid": "movies",
    "status": "enqueued",
    "type": "documentDeletion",
    "enqueuedAt": "2021-08-11T09:25:53.000000Z"
}
您可以使用此 taskUid 获取有关任务状态的更多详细信息。

按筛选删除文档

POST
/indexes/{index_uid}/documents/delete
根据筛选条件删除一组文档。

路径参数

名称类型描述
index_uid *字符串请求索引的 uid

请求体

以字符串或字符串数组的数组形式编写的要删除文档的筛选表达式。
如果不首先将属性明确添加到 filterableAttributes 列表中,filter 将无法工作。 在我们的专用指南中了解有关筛选器的更多信息。
 "filter": "rating > 3.5"
发送空负载 (--data-binary '{}') 将返回 bad_request 错误。

示例

curl \
  -X POST MEILISEARCH_URL/indexes/movies/documents/delete \
  -H 'Content-Type: application/json' \
  --data-binary '{
    "filter": "genres = action OR genres = adventure"
  }'

响应:202 Accepted

{
    "taskUid": 1,
    "indexUid": "movies",
    "status": "enqueued",
    "type": "documentDeletion",
    "enqueuedAt": "2023-05-15T08:38:48.024551Z"
}
您可以使用此 taskUid 获取有关任务状态的更多详细信息。

按批次删除文档

POST
/indexes/{index_uid}/documents/delete-batch
根据文档 ID 数组删除一组文档。

路径参数

名称类型描述
index_uid *字符串请求索引的 uid

请求体

包含要删除文档的唯一 ID 的数字数组。
[23488, 153738, 437035, 363869]

示例

curl \
  -X POST 'MEILISEARCH_URL/indexes/movies/documents/delete-batch' \
  -H 'Content-Type: application/json' \
  --data-binary '[
    23488,
    153738,
    437035,
    363869
  ]'

响应:202 Accepted

{
    "taskUid": 1,
    "indexUid": "movies",
    "status": "enqueued",
    "type": "documentDeletion",
    "enqueuedAt": "2021-08-11T09:25:53.000000Z"
}
您可以使用此 taskUid 获取有关任务状态的更多详细信息。
© . This site is unofficial and not affiliated with Meilisearch.