使用Elasticsearch API索引和搜索数据

edit

使用Elasticsearch API索引和搜索数据

edit

本快速入门指南是关于Elasticsearch基本概念的实践介绍:索引、文档和字段类型映射

您将学习如何创建索引、将数据作为文档添加、处理动态和显式映射,以及执行您的第一个基本搜索。

本教程中的代码示例默认使用控制台语法。 您可以在控制台界面中转换为其他编程语言

先决条件

edit

在开始之前,您需要有一个正在运行的 Elasticsearch 集群。 最快入门的方法是使用 本地开发环境。 请参阅 运行 Elasticsearch 以获取其他部署选项。

步骤1:创建索引

edit

创建一个名为 books 的新索引:

PUT /books

以下响应表示索引已成功创建。

示例响应
{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "books"
}

步骤 2:向索引中添加数据

edit

本教程使用Elasticsearch API,但还有许多其他方法可以将数据添加到Elasticsearch

您将数据作为称为文档的JSON对象添加到Elasticsearch中。 Elasticsearch将这些文档存储在可搜索的索引中。

添加单个文档

edit

提交以下索引请求以将单个文档添加到books索引中。

如果索引不存在,此请求将自动创建它。

POST books/_doc
{
  "name": "Snow Crash",
  "author": "Neal Stephenson",
  "release_date": "1992-06-01",
  "page_count": 470
}

响应包括Elasticsearch为文档生成的元数据,包括索引中文档的唯一_id

示例响应
{
  "_index": "books", 
  "_id": "O0lG2IsBaSa7VYx_rEia", 
  "_version": 1, 
  "result": "created", 
  "_shards": { 
    "total": 2, 
    "successful": 2, 
    "failed": 0 
  },
  "_seq_no": 0, 
  "_primary_term": 1 
}

The _index 字段表示文档被添加到的索引。

字段 _id 是文档的唯一标识符。

The _version 字段表示文档的版本。

字段 result 表示索引操作的结果。

The _shards 字段包含有关索引操作执行的分片数量以及成功数量的信息。

字段 total 表示索引的总分片数。

The successful 字段表示索引操作成功执行的分片数量。

The failed field indicates the number of shards that failed during the indexing operation. 0 indicates no failures.

The _seq_no 字段保存了一个单调递增的数字,每次在分片上的索引操作都会递增。

The _primary_term 字段是一个单调递增的数字,每次主分片被分配到不同的节点时都会递增。

添加多个文档

edit

使用_bulk 端点在一次请求中添加多个文档。批量数据必须格式化为换行符分隔的JSON(NDJSON)。

POST /_bulk
{ "index" : { "_index" : "books" } }
{"name": "Revelation Space", "author": "Alastair Reynolds", "release_date": "2000-03-15", "page_count": 585}
{ "index" : { "_index" : "books" } }
{"name": "1984", "author": "George Orwell", "release_date": "1985-06-01", "page_count": 328}
{ "index" : { "_index" : "books" } }
{"name": "Fahrenheit 451", "author": "Ray Bradbury", "release_date": "1953-10-15", "page_count": 227}
{ "index" : { "_index" : "books" } }
{"name": "Brave New World", "author": "Aldous Huxley", "release_date": "1932-06-01", "page_count": 268}
{ "index" : { "_index" : "books" } }
{"name": "The Handmaids Tale", "author": "Margaret Atwood", "release_date": "1985-06-01", "page_count": 311}

您应该会收到一个没有错误的响应。

示例响应
{
  "errors": false,
  "took": 29,
  "items": [
    {
      "index": {
        "_index": "books",
        "_id": "QklI2IsBaSa7VYx_Qkh-",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 2,
          "failed": 0
        },
        "_seq_no": 1,
        "_primary_term": 1,
        "status": 201
      }
    },
    {
      "index": {
        "_index": "books",
        "_id": "Q0lI2IsBaSa7VYx_Qkh-",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 2,
          "failed": 0
        },
        "_seq_no": 2,
        "_primary_term": 1,
        "status": 201
      }
    },
    {
      "index": {
        "_index": "books",
        "_id": "RElI2IsBaSa7VYx_Qkh-",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 2,
          "failed": 0
        },
        "_seq_no": 3,
        "_primary_term": 1,
        "status": 201
      }
    },
    {
      "index": {
        "_index": "books",
        "_id": "RUlI2IsBaSa7VYx_Qkh-",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 2,
          "failed": 0
        },
        "_seq_no": 4,
        "_primary_term": 1,
        "status": 201
      }
    },
    {
      "index": {
        "_index": "books",
        "_id": "RklI2IsBaSa7VYx_Qkh-",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 2,
          "failed": 0
        },
        "_seq_no": 5,
        "_primary_term": 1,
        "status": 201
      }
    }
  ]
}

步骤 3:定义映射和数据类型

edit

映射定义了数据在Elasticsearch中如何存储和索引,类似于关系数据库中的模式。

使用动态映射

edit

当使用动态映射时,Elasticsearch 默认会自动为新字段创建映射。 我们目前添加的文档使用了动态映射,因为我们没有在创建索引时指定映射。

要了解动态映射的工作原理,请向 books 索引添加一个新文档,该文档包含现有文档中未出现的新字段。

POST /books/_doc
{
  "name": "The Great Gatsby",
  "author": "F. Scott Fitzgerald",
  "release_date": "1925-04-10",
  "page_count": 180,
  "language": "EN" 
}

新字段。

使用Get mapping API查看books索引的映射。新字段new_field已添加到映射中,数据类型为text

GET /books/_mapping
示例响应
{
  "books": {
    "mappings": {
      "properties": {
        "author": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "name": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "new_field": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "page_count": {
          "type": "long"
        },
        "release_date": {
          "type": "date"
        }
      }
    }
  }
}

定义显式映射

edit

创建一个名为 my-explicit-mappings-books 的索引,并使用显式映射。 将每个字段的属性作为 JSON 对象传递。该对象应包含 字段数据类型 和任何其他 映射参数

PUT /my-explicit-mappings-books
{
  "mappings": {
    "dynamic": false,  
    "properties": {  
      "name": { "type": "text" },
      "author": { "type": "text" },
      "release_date": { "type": "date", "format": "yyyy-MM-dd" },
      "page_count": { "type": "integer" }
    }
  }
}

禁用索引的动态映射。包含未在映射中定义的字段的文档将被拒绝。

The properties 对象定义了此索引中文档的字段及其数据类型。

示例响应
{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "my-explicit-mappings-books"
}

结合动态和显式映射

edit

显式映射在索引创建时定义,文档必须符合这些映射。 你也可以使用更新映射 API。 当索引的dynamic标志设置为true时,你可以在不更新映射的情况下向文档添加新字段。

这使您能够结合显式和动态映射。 了解更多关于管理和更新映射的信息。

步骤 4:搜索您的索引

edit

索引文档几乎可以实时用于搜索,使用_search API

搜索所有文档

edit

运行以下命令以搜索 books 索引中的所有文档:

GET books/_search
示例响应
{
  "took": 2, 
  "timed_out": false, 
  "_shards": { 
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": { 
    "total": { 
      "value": 7,
      "relation": "eq"
    },
    "max_score": 1, 
    "hits": [
      {
        "_index": "books", 
        "_id": "CwICQpIBO6vvGGiC_3Ls", 
        "_score": 1, 
        "_source": { 
          "name": "Brave New World",
          "author": "Aldous Huxley",
          "release_date": "1932-06-01",
          "page_count": 268
        }
      },
      ... (truncated)
    ]
  }
}

The took 字段表示 Elasticsearch 执行搜索所花费的时间(以毫秒为单位)

The timed_out 字段表示搜索是否超时

The _shards field contains information about the number of 分片 that the search was executed on and the number that succeeded

The hits 对象包含搜索结果

The total 对象提供了关于匹配文档总数的详细信息

The max_score 字段表示所有匹配文档中最高的相关性分数

The _index 字段表示文档所属的索引

The _id 字段是文档的唯一标识符

The _score 字段表示文档的相关性得分

The _source 字段包含在索引期间提交的原始 JSON 对象

匹配查询

edit

您可以使用match查询来搜索在特定字段中包含特定值的文档。 这是全文搜索的标准查询。

运行以下命令以在 books 索引中搜索在 name 字段中包含 brave 的文档:

GET books/_search
{
  "query": {
    "match": {
      "name": "brave"
    }
  }
}
示例响应
{
  "took": 9,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 0.6931471, 
    "hits": [
      {
        "_index": "books",
        "_id": "CwICQpIBO6vvGGiC_3Ls",
        "_score": 0.6931471,
        "_source": {
          "name": "Brave New World",
          "author": "Aldous Huxley",
          "release_date": "1932-06-01",
          "page_count": 268
        }
      }
    ]
  }
}

The max_score 是结果中得分最高的文档的分数。在这种情况下,只有一个匹配的文档,所以 max_score 就是这个文档的分数。

步骤 5:删除您的索引(可选)

edit

当跟随示例操作时,您可能希望删除索引以从头开始。 您可以使用删除索引 API来删除索引。

例如,运行以下命令以删除在本教程中创建的索引:

DELETE /books
DELETE /my-explicit-mappings-books

删除索引将永久删除其文档、分片和元数据。