> For the complete documentation index, see [llms.txt](https://hezhiqiang.gitbook.io/elasticsearch/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hezhiqiang.gitbook.io/elasticsearch/data/create.md).

# 创建

当我们索引一个文档时，如何确定我们是创建了一个新的文档还是覆盖了一个已经存在的文档呢？

请牢记`_index`,`_type`以及`_id`组成了唯一的文档标记，所以为了确定我们创建的是全新的内容，最简单的方法就是使用`POST`方法，让Elasticsearch自动创建不同的`_id`：

```javascript
POST /website/blog/
{ ... }
```

然而，我们可能已经决定好了`_id`，所以需要告诉Elasticsearch只有当`_index`，`_type`以及`_id`这3个属性全部相同的文档不存在时才接受我们的请求。实现这个目的有两种方法，他们实质上是一样的，你可以选择你认为方便的那种：

第一种是在查询中添加`op_type`参数：

```javascript
PUT /website/blog/123?op_type=create
{ ... }
```

或者在请求最后添加 `/_create`:

```javascript
PUT /website/blog/123/_create
{ ... }
```

如果成功创建了新的文档，Elasticsearch将会返回常见的元数据以及`201 Created`的HTTP反馈码。

而如果存在同名文件，Elasticsearch将会返回一个`409 Conflict`的HTTP反馈码，以及如下方的错误信息：

```javascript
{
  "error" : "DocumentAlreadyExistsException[[website][4] [blog][123]:
             document already exists]",
  "status" : 409
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://hezhiqiang.gitbook.io/elasticsearch/data/create.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
