Skip to content

OpenSearch

Elasticsearch (ES) 搜索引擎存储的母公司 Elastic NV 在 2021 年改变了新版本的授权许可不再开源, 美国 Amazon 云计算厂商在同年对 ES 7.x 版代码克隆命名为 OpenSearch (OS),并主导开源开发。

OS 的功能和交互接口和 ES 类似,适合个人和中小企业作为 ES/ELK(Elasticsearch, Logstash, and Kibana) 套件的替代使用。 OS Dashboard(后用 dash 简称) 相当于 ELK 的 Kibana ,提供易用网页查询界面。

OS 本身基于文本分词和索引库 Apache Lucene 和 Java 语言实现,它提供 HTTP 交互接口可跨编程语言和平台使用。

后面如无特别说明,以 OS 1.3.x 版本为例。

安装服务

启动单个节点

设置并启动 OS 和 OS dashboard

方式一:通过 docker,使用组合配置文件 docker-compose.yml 同时启动 OS 和 OS dashboard,OS 默认监听地址为 http://localhost:9200 ,无 SSL 和 HTTP basic 认证; dashboard 默认监听地址为 http://localhost:5601 ,可按需修改。

docker compose -f docker-compose.yml -p myos up

docker-compose.yml

方式二:通过 docker,分别设置并启动 OS,默认监听地址为 http://localhost:9200,可按需修改

sh
docker run -d \
--restart unless-stopped \
--name myos \
-p 9200:9200 \
-p 9600:9600 \
-v /data/os:/usr/share/opensearch/data \
-e discovery.type=single-node \
-e DISABLE_SECURITY_PLUGIN=true \
opensearchproject/opensearch:latest

(可选)启动 OS dashboard,默认监听地址为 http://localhost:5601 ,可按需修改

sh
docker run -d \
--restart unless-stopped \
--name myosdash \
-p 5601:5601 \
-e OPENSEARCH_HOSTS=http://host.docker.internal:9200 \
-e DISABLE_SECURITY_DASHBOARDS_PLUGIN=true \
opensearchproject/opensearch-dashboards:latest

注:上面启动 dashboard 时指定了 host.docker.internal 由于上一步 OS 指定了映射 localhost:9200 可认为两个地址都指向容器宿主机(host of container)自身。

搭建多个节点集群

https://opensearch.org/docs/latest/tuning-your-cluster/cluster/

TBD.

常用配置

OS 绑定两个内网 IP 地址

修改 config/opensearch.yml 中选项 network.host,值示例 [10.0.0.1, 100.1.2.3]

OS dashboard 禁用认证

  • 删除安全插件 ./bin/opensearch-dashboards-plugin remove securityDashboards
  • 修改配置文件 config/opensearch_dashboards.yml ,注释掉 opensearch_security 前缀相关选项,
  • 重启 OS 。

基本概念

document: 名词含义:文档,相当于数据库表中某一条记录;
index: 名词,索引,相当于数据库的库和表组合概念,同一类数据的逻辑分类;
index: 动词,索引,添加一个文档到存储并让它可以搜索;
indices: 名词,索引(index)的复数;
mapping: 名词,文档字段的映射设置,用于定义文档字段如何存储和建立索引; type: 名词,ElasticSearch 7.x 开始已经废弃概念;
shard: 分片,TBD;
replica:副本,TBD;

一个 OS 实例节点,可以有多个索引(indices),一个索引(index)里面有多个文档(document);

index 命名规则:

  • 不能包含除了全小写字母以外的特殊字符;
  • 不能以减号或下划线开头。

基本查询

OS 提供 index 和 _bulk 两类 REST HTTP API 读写数据和设置,index 用于操作单个文档, _bulk 批量操作;

index API 示例:创建一个文档

http
PUT book/_doc/123
{ "title": "The Bible" }

bulk API 等效示例

http
POST _bulk
{ "index": { "_index": "book", "_id": "123" } }
{ "book": "The Bible" }

_bulk API 语法每行一个 JSON ,每行以换行符 \n 结束:

http
Action and metadata 指令和元数据\n
Optional document 第二行可选,文档数据\n

用户读写数据可以使用命令行工具 cURL ,也可使用 dash ,因都是 HTTP 协议,后面描述均以 HTTP 协议语法作为说明。

cURL 请求示例 curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>' ; query-string 带上 pretty 会自动美化 JSON 格式化输出;

查看文档总数,在 dash - management - Dev Tools - Console 通过 GET _count?pretty 查询,等效于命令行 cURL 查询 curl localhost:9200/_count?pretty

查看所有索引状态 GET _cat/indices?pretty&v=true

查看指定索引文档总数 GET myidx/_count?pretty


查改增删文档

创建文档

语法:

http
POST <index 索引名称>/_doc/<id 文档主键>
<document in JSON>

示例:

http
POST book/_doc/123?pretty
{"title":"The Bible"}

注:和 cURL 有差异,后者需要带头 curl -XPOST -H 'Content-Type: application/json' localhost:9200/book/_doc/123?pretty -d '{"title":"The Bible"}'

查询文档 GET <index 索引名称>/_doc/<id 文档主键>

批量查询文档

http
GET _mget
{
  "docs": [
    {
      "_index": "<index>",
      "_id": "<id>"
    },
    {
      "_index": "<index>",
      "_id": "<id>"
    }
  ]
}

更新文档,覆盖已有

http
POST book/_doc/123?pretty
{"title":"The Bible"}

更新文档部分字段,注:请求体需要包一层 doc

http
POST book/_update/123?pretty
{"doc":{"year":2008}}

删除文档 DELETE book/_doc/123

重命名索引

http
POST /_reindex
{
  "source": {
    "index": "old_index_name"
  },
  "dest": {
    "index": "new_index_name"
  }
}

全文搜索

mapping: 名词,文档字段的映射设置,用于定义文档字段如何存储和建立索引;
不设置 index 的 mapping 时,默认是自动动态 mapping ,创建文档时自动推断文档字段类型并生成对应 mapping 设置。

动态 mapping 类型

  • null:不能被索引或搜索
  • boolean:true 或 false,空字符等同 false
  • float:32 位浮点型
  • float:64 位浮点型
  • integer:有符号 32 位整形
  • object:标准 JSON object 类型,可设置独立子字段和子 mapping
  • array:只能存同一种类型的子项的数组,如字符串数组,或整形数组
  • text:可全文索引字符序列(A string sequence of characters that represent full-text values);全文搜索时时分词匹配?
  • keyword:结构字符序列(A string sequence of structured characters),如电子邮件地址,或邮政编码;全文搜索时是完整匹配?
  • date detection string:按日期解释的字符串
  • numeric detection string:按数值解释的字符串

mapping 支持字段类型

  • alias
  • binary
  • numeric
  • boolean
  • date
  • ip
  • range
  • object
  • string
  • autocomplete
  • geographic
  • rank
  • percolator

简单全文匹配 GET _search?q=bible&pretty

指定单个字段匹配

http
GET book/_search
{
    "query": {
        "match": {"book_title": "bible"}
    }
}

指定多个字段匹配

http
GET _search
{
    "query": {
        "multi_match": {
            "query": "the bible",
            "fields": [
                "book_title",
                "book_keywords"
            ]
        }
    }
}

指定多个字段多个值匹配

http
GET book/_search
{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                    "book_title": "bible"
                    }
                },
                {
                    "match": {
                        "book_keywords": "bible"
                    }
                }
            ]
        }
    },
    "size": 10
}

限制返回 1 条记录 /_search?q=keyword&&size=1

OpenSearch vs ElasticSearch

FeatureElasticsearchOpenSearch
LicensingElastic License, Server Side Public License (SSPL)Apache 2.0
CommunityProprietary, with some community contributionsCommunity-driven, open-source contributions
PricingPaid for enterprise featuresFree, with optional managed service by AWS
Use CasesEnterprise search, log analytics, full-text searchLog analytics, enterprise search, basic full-text search
SizesVersion 8.16.1
Docker image 854 MB
Version 2
Docker image 1.11 GB

Released under the CC-BY-NC-4.0