# 创建

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

请牢记`_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: 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:

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

The question should be specific, self-contained, and written in natural language.
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.
