在 macOS 上构建 Elasticsearch 7 并进行全文搜索

总结

    • Homebrew で Elasticsearch 7 を macOS にインストールする

 

    • Elasticsearch にデータを入れて全文検索する

 

    今回の環境: macOS Catalina + Elasticsearch 7.8

安裝

按照“Install Elasticsearch on macOS with Homebrew | Elasticsearch Reference [7.8] | Elastic”上所述的步骤,在macOS上使用Homebrew安装Elasticsearch。

$ brew tap elastic/tap
$ brew install elastic/tap/elasticsearch-full

准备配置文件

這次我們將設置檔案的目錄準備在任意位置。

$ mkdir elasticsearch
$ cd elasticsearch

从默认设置文件中复制最基本所需的内容。

    • elasticsearch.yml: Elasticsearch の設定ファイル

 

    • jvm.options: JVM の設定ファイル

 

    log4j2.properties: ログ出力の設定ファイル
$ cp /usr/local/etc/elasticsearch/elasticsearch.yml .
$ cp /usr/local/etc/elasticsearch/jvm.options .
$ cp /usr/local/etc/elasticsearch/log4j2.properties .

将elasticsearch.yml文件的内容修改如下。

cluster:
  name: mycluster
node:
  name: mynode
network:
  host: localhost
path:
  data: /usr/local/var/lib/elasticsearch/
  logs: /usr/local/var/log/elasticsearch/

启动 Elasticsearch

将存放设置文件的目录指定为环境变量 ES_PATH_CONF。

$ export ES_PATH_CONF=/Users/foo/elasticsearch

使用 Elasticsearch 命令启动。

$ elasticsearch

从另一个终端使用curl来访问Elasticsearch并确认其已启动。

$ curl http://localhost:9200/
{
  "name" : "mynode",
  "cluster_name" : "mycluster",
  "cluster_uuid" : "XXXXXXXXXXXXXXXXXXXXXX",
  "version" : {
    "number" : "7.8.0",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "757314695644ea9a1dc2fecd26d1a43856725e65",
    "build_date" : "2020-06-14T19:35:50.234439Z",
    "build_snapshot" : false,
    "lucene_version" : "8.5.1",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

Elasticsearch 的 REST API

Elasticsearch 提供了用于管理和操作的 REST API。
本次使用 curl 调用 REST API 来进行数据添加和搜索。

REST APIs | Elasticsearch 参考文档 [7.8] | Elastic

创建索引

创建一个作为存放数据的容器的索引。
索引类似于关系型数据库中的表。

可以使用HTTP PUT方法创建索引。

创建索引API | Elasticsearch参考[7.8] | Elastic

PUT /<index>

通过添加 pretty 参数,可以使响应的 JSON 格式更易读,同时在此处创建一个名为 foo_index 的索引。

$ curl --include -XPUT "http://localhost:9200/foo_index?pretty"
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 85

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "foo_index"
}

可以通过HTTP GET请求来确认已创建的索引。

获取索引 API | Elasticsearch 参考文档 [7.8] | Elastic

GET /<index>

获取foo_index的索引信息。

$ curl --include -XGET "http://localhost:9200/foo_index?pretty"
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 379

{
  "foo_index" : {
    "aliases" : { },
    "mappings" : { },
    "settings" : {
      "index" : {
        "creation_date" : "1595771960816",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "D7t0dUBPSqKKyvw5Z7eujw",
        "version" : {
          "created" : "7080099"
        },
        "provided_name" : "foo_index"
      }
    }
  }
}

添加文件

文档在关系型数据库中类似于记录的概念。
文档使用JSON格式表示。

可以使用HTTP POST方法添加文档。

索引 API | Elasticsearch 参考[7.8] | Elastic

POST /<index>/_doc/

在这里注册一个具有foo_user和foo_message字段的文档。
文档的ID将会自动生成(也可以指定ID)。这次生成了一个ID为“9xNyi3MBhHaU2qi75ayU”的文档。

$ curl --include -XPOST "http://localhost:9200/foo_index/_doc?pretty" \
-H 'Content-Type: application/json' \
-d '{
  "foo_user": "Alice",
  "foo_message": "The night was young, and so was he. But the night was sweet, and he was sour."
}'
HTTP/1.1 201 Created
Location: /foo_index/_doc/9xNyi3MBhHaU2qi75ayU
content-type: application/json; charset=UTF-8
content-length: 242

{
  "_index" : "foo_index",
  "_type" : "_doc",
  "_id" : "9xNyi3MBhHaU2qi75ayU",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 2,
  "_primary_term" : 1
}

可以使用HTTP GET方法来查看添加的文档。

获取API | Elasticsearch 参考文档 [7.8] | Elastic

GET <index>/_doc/<_id>

使用指定的标识符来获取文档的信息。

$ curl --include -XGET "http://localhost:9200/foo_index/_doc/9xNyi3MBhHaU2qi75ayU?pretty"
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 306

{
  "_index" : "foo_index",
  "_type" : "_doc",
  "_id" : "9xNyi3MBhHaU2qi75ayU",
  "_version" : 1,
  "_seq_no" : 2,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "foo_user" : "Alice",
    "foo_message" : "The night was young, and so was he. But the night was sweet, and he was sour."
  }
}

请将第二个文件也添加进去。

$ curl --include -XPOST "http://localhost:9200/foo_index/_doc?pretty" \
-H 'Content-Type: application/json' \
-d '{
  "foo_user": "ボブ",
  "foo_message": "夜は若く、彼も若かった。が、夜の空気は甘いのに、彼の気分は苦かった。"
}'
HTTP/1.1 201 Created
Location: /foo_index/_doc/-BNzi3MBhHaU2qi7EazP
content-type: application/json; charset=UTF-8
content-length: 242

{
  "_index" : "foo_index",
  "_type" : "_doc",
  "_id" : "-BNzi3MBhHaU2qi7EazP",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 3,
  "_primary_term" : 1
}

数据的全文搜索

你可以使用Elasticsearch的查询DSL来搜索文档。

查询DSL | Elasticsearch 参考手册 [7.8] | Elastic

在查询 DSL 中,使用 JSON 格式来指定搜索条件。

查询匹配 | Elasticsearch参考 [7.8] | Elastic

GET /_search
{
  "query": {
    "match": {
      "message": {
        "query": "this is a test"
      }
    }
  }
}

在这里执行一个查询,来搜索包含”夜的空气”的文档,其中包括 foo_message 字段。

$ curl --include -XGET "http://localhost:9200/foo_index/_search?pretty" \
-H 'Content-Type: application/json' \
-d '
{
  "query": {
    "match": {
      "foo_message": "夜の空気"
    }
  }
}'
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 611

{
  "took" : 16,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 3.4337347,
    "hits" : [
      {
        "_index" : "foo_index",
        "_type" : "_doc",
        "_id" : "-BNzi3MBhHaU2qi7EazP",
        "_score" : 3.4337347,
        "_source" : {
          "foo_user" : "ボブ",
          "foo_message" : "夜は若く、彼も若かった。が、夜の空気は甘いのに、彼の気分は苦かった。"
        }
      }
    ]
  }
}

使用 REST API 进行索引操作的示例。

索引API | Elasticsearch参考[7.8] | Elastic

给出使用 REST API 在命令行工具 curl 中操作索引的示例。

创建索引

创建索引API | Elasticsearch参考[7.8] | 弹性搜索

$ curl -XPUT http://localhost:9200/foo_index

删除索引

在Elasticsearch参考文档[7.8]中,删除索引的API功能。

$ curl -XDELETE http://localhost:9200/foo_index

获取索引列表

猫 API | Elasticsearch 参考手册 [7.8] | Elastic

$ curl -XGET "http://localhost:9200/_cat/indices?format=json"

使用REST API操作文档的示例。

文档 APIs | Elasticsearch 参考 [7.8] | Elastic

提供一个使用 REST API 和 curl 操作文档的示例。

添加文件

索引 API | Elasticsearch 参考文档 [7.8] | 弹性搜索

$ curl -XPOST http://localhost:9200/foo_index/_doc \
-H 'Content-Type: application/json' \
-d '{"foo_user": "Carol", "foo_message": "Hello, world."}'

获取文档

获取API | Elasticsearch 参考文档 [7.8] | Elastic

$ curl -XGET http://localhost:9200/foo_index/_doc/XXXXXXXXXXXXXXXXXXXX

更新文件

更新API | Elasticsearch参考 [7.8] | 弹性搜索

$ curl -XPUT http://localhost:9200/foo_index/_doc/XXXXXXXXXXXXXXXXXXXX \
-H 'Content-Type: application/json' \
-d '{"foo_user": "Carol", "foo_message": "Goodbye, world."}'

删除文档

删除API | Elasticsearch 参考 [7.8] | Elastic

$ curl -XDELETE http://localhost:9200/foo_index/_doc/XXXXXXXXXXXXXXXXXXXX

获取文档数量

计数API | Elasticsearch 参考资料 [7.8] | Elastic

$ curl -XGET http://localhost:9200/foo_index/_count

搜索文档

查询DSL | Elasticsearch参考【7.8】|弹性
匹配查询| Elasticsearch参考【7.8】|弹性

$ curl -XGET http://localhost:9200/foo_index/_search \
-H 'Content-Type: application/json' \
-d '
{
  "query": {
    "match": {
      "foo_message": "夜"
    }
  }
}'

请参考相关资料。

通过Homebrew安装Elasticsearch。

    • macOS向けに、オフィシャル版Elastic Homebrewタップをリリース | Elastic Blog

 

    • Install Elasticsearch on macOS with Homebrew | Elasticsearch Reference [7.8] | Elastic

 

    • Configuring Elasticsearch | Elasticsearch Reference [7.8] | Elastic

 

    • GitHub – elastic/homebrew-tap: Homebrew tap for the Elastic Stack

 

    elasticsearch — Homebrew Formulae

Elasticsearch 的 REST API

    • 基本概念 | Elasticsearchリファレンス [5.4] | Elastic

 

    • タイプレスAPIに移行する:Elasticsearch 7.0の変更点 | Elastic Blog

 

    REST APIs | Elasticsearch Reference [7.8] | Elastic
广告
将在 10 秒后关闭
bannerAds