Ruby 客户端
OpenSearch Ruby 客户端允许你通过 Ruby 方法与 OpenSearch 集群交互,而不是使用 HTTP 方法和原始 JSON。有关客户端的完整 API 文档和其他示例,请参阅 opensearch-transport、opensearch-api、opensearch-dsl 和 opensearch-ruby gem 文档。
本入门指南说明了如何连接到 OpenSearch、索引文档和运行查询。有关客户端源代码,请参阅 opensearch-ruby 仓库。
安装 Ruby 客户端
要安装 Ruby 客户端的 Ruby gem,请运行以下命令:
gem install opensearch-ruby要使用客户端,请将其作为模块导入:
require 'opensearch'连接到 OpenSearch
要连接到默认的 OpenSearch 主机,请创建一个客户端对象,在构造函数中传递默认主机地址:
client = OpenSearch::Client.new(host: 'http://localhost:9200')以下示例创建一个具有自定义 URL 且 log 选项设置为 true 的客户端对象。它将 retry_on_failure 参数设置为在失败时重试请求五次,而不是默认的三次。最后,它通过将 request_timeout 参数设置为 120 秒来增加超时时间。然后返回基本的集群健康信息:
client = OpenSearch::Client.new(
url: "http://localhost:9200",
retry_on_failure: 5,
request_timeout: 120,
log: true
)
client.cluster.health输出如下:
2022-08-25 14:24:52 -0400: GET http://localhost:9200/ [status:200, request:0.048s, query:n/a]
2022-08-25 14:24:52 -0400: < {
"name" : "opensearch",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "Aw0F5Pt9QF6XO9vXQHIs_w",
"version" : {
"distribution" : "opensearch",
"number" : "2.2.0",
"build_type" : "tar",
"build_hash" : "b1017fa3b9a1c781d4f34ecee411e0cdf930a515",
"build_date" : "2022-08-09T02:27:25.256769336Z",
"build_snapshot" : false,
"lucene_version" : "9.3.0",
"minimum_wire_compatibility_version" : "7.10.0",
"minimum_index_compatibility_version" : "7.0.0"
},
"tagline" : "The OpenSearch Project: https://opensearch.org/"
}
2022-08-25 14:24:52 -0400: GET http://localhost:9200/_cluster/health [status:200, request:0.018s, query:n/a]
2022-08-25 14:24:52 -0400: < {"cluster_name":"docker-cluster","status":"yellow","timed_out":false,"number_of_nodes":1,"number_of_data_nodes":1,"discovered_master":true,"discovered_cluster_manager":true,"active_primary_shards":10,"active_shards":10,"relocating_shards":0,"initializing_shards":0,"unassigned_shards":8,"delayed_unassigned_shards":0,"number_of_pending_tasks":0,"number_of_in_flight_fetch":0,"task_max_waiting_in_queue_millis":0,"active_shards_percent_as_number":55.55555555555556}连接到 Amazon OpenSearch Service
要连接到 Amazon OpenSearch Service,首先安装 opensearch-aws-sigv4 gem:
gem install opensearch-aws-sigv4require 'opensearch-aws-sigv4'
require 'aws-sigv4'
signer = Aws::Sigv4::Signer.new(service: 'es',
region: 'us-west-2', # 签名服务区域
access_key_id: 'key_id',
secret_access_key: 'secret')
client = OpenSearch::Aws::Sigv4Client.new({
host: 'https://your.amz-managed-opensearch.domain',
log: true
}, signer)
# 创建索引和文档
index = 'prime'
client.indices.create(index: index)
client.index(index: index, id: '1', body: { name: 'Amazon Echo',
msrp: '5999',
year: 2011 })
# 搜索文档
client.search(body: { query: { match: { name: 'Echo' } } })
# 删除文档
client.delete(index: index, id: '1')
# 删除索引
client.indices.delete(index: index)连接到 Amazon OpenSearch Serverless
要连接到 Amazon OpenSearch Serverless 服务,首先安装 opensearch-aws-sigv4 gem:
gem install opensearch-aws-sigv4require 'opensearch-aws-sigv4'
require 'aws-sigv4'
signer = Aws::Sigv4::Signer.new(service: 'aoss',
region: 'us-west-2', # 签名服务区域
access_key_id: 'key_id',
secret_access_key: 'secret')
client = OpenSearch::Aws::Sigv4Client.new({
host: 'https://your.amz-managed-opensearch.domain', # OpenSearch Serverless 的 serverless 端点
log: true
}, signer)
# 创建索引和文档
index = 'prime'
client.indices.create(index: index)
client.index(index: index, id: '1', body: { name: 'Amazon Echo',
msrp: '5999',
year: 2011 })
# 搜索文档
client.search(body: { query: { match: { name: 'Echo' } } })
# 删除文档
client.delete(index: index, id: '1')
# 删除索引
client.indices.delete(index: index)创建索引
你不需要在 OpenSearch 中显式创建索引。一旦你将文档上传到不存在的索引中,OpenSearch 会自动创建该索引。或者,你可以显式创建索引以指定主分片和副本分片数量等设置。要创建具有非默认设置的索引,请创建一个包含这些设置的索引体哈希:
index_body = {
'settings': {
'index': {
'number_of_shards': 1,
'number_of_replicas': 2
}
}
}
client.indices.create(
index: 'students',
body: index_body
)映射
OpenSearch 使用动态映射来推断被索引文档的字段类型。但是,为了对文档的模式有更多控制,你可以向 OpenSearch 传递显式映射。你可以在此映射中为文档的部分或全部字段定义数据类型。要为索引创建映射,请使用 put_mapping 方法:
client.indices.put_mapping(
index: 'students',
body: {
properties: {
first_name: { type: 'keyword' },
last_name: { type: 'keyword' }
}
}
)默认情况下,字符串字段被映射为 text,但在上面的映射中,first_name 和 last_name 字段被映射为 keyword。此映射向 OpenSearch 表明这些字段不应被分析,只应支持完全区分大小写的匹配。
你可以使用 get_mapping 方法验证索引的映射:
response = client.indices.get_mapping(index: 'students')如果你事先知道文档的映射并希望避免映射错误(例如,字段名拼写错误),你可以将 dynamic 参数设置为 strict:
client.indices.put_mapping(
index: 'students',
body: {
dynamic: 'strict',
properties: {
first_name: { type: 'keyword' },
last_name: { type: 'keyword' },
gpa: { type: 'float'},
grad_year: { type: 'integer'}
}
}
)使用严格映射,你可以索引缺少字段的文档,但不能索引具有新字段的文档。例如,索引以下拼写错误 grad_yea 字段的文档会失败:
document = {
first_name: 'Connor',
last_name: 'James',
gpa: 3.93,
grad_yea: 2021
}
client.index(
index: 'students',
body: document,
id: 100,
refresh: true
)OpenSearch 返回映射错误:
{"error":{"root_cause":[{"type":"strict_dynamic_mapping_exception","reason":"mapping set to strict, dynamic introduction of [grad_yea] within [_doc] is not allowed"}],"type":"strict_dynamic_mapping_exception","reason":"mapping set to strict, dynamic introduction of [grad_yea] within [_doc] is not allowed"},"status":400}索引单个文档
要索引单个文档,请使用 index 方法:
document = {
first_name: 'Connor',
last_name: 'James',
gpa: 3.93,
grad_year: 2021
}
client.index(
index: 'students',
body: document,
id: 100,
refresh: true
)更新文档
要更新文档,请使用 update 方法:
client.update(index: 'students',
id: 100,
body: { doc: { gpa: 3.25 } },
refresh: true)删除文档
要删除文档,请使用 delete 方法:
client.delete(
index: 'students',
id: 100,
refresh: true
)批量操作
你可以使用 bulk 方法同时执行多个操作。这些操作可以是相同类型或不同类型。
你可以使用 bulk 方法索引多个文档:
actions = [
{ index: { _index: 'students', _id: '200' } },
{ first_name: 'James', last_name: 'Rodriguez', gpa: 3.91, grad_year: 2019 },
{ index: { _index: 'students', _id: '300' } },
{ first_name: 'Nikki', last_name: 'Wolf', gpa: 3.87, grad_year: 2020 }
]
client.bulk(body: actions, refresh: true)你可以按以下方式删除多个文档:
# 删除多个文档。
actions = [
{ delete: { _index: 'students', _id: 200 } },
{ delete: { _index: 'students', _id: 300 } }
]
client.bulk(body: actions, refresh: true)你可以在使用 bulk 时执行不同的操作:
actions = [
{ index: { _index: 'students', _id: 100, data: { first_name: 'Paulo', last_name: 'Santos', gpa: 3.29, grad_year: 2022 } } },
{ index: { _index: 'students', _id: 200, data: { first_name: 'Shirley', last_name: 'Rodriguez', gpa: 3.92, grad_year: 2020 } } },
{ index: { _index: 'students', _id: 300, data: { first_name: 'Akua', last_name: 'Mansa', gpa: 3.95, grad_year: 2022 } } },
{ index: { _index: 'students', _id: 400, data: { first_name: 'John', last_name: 'Stiles', gpa: 3.72, grad_year: 2019 } } },
{ index: { _index: 'students', _id: 500, data: { first_name: 'Li', last_name: 'Juan', gpa: 3.94, grad_year: 2022 } } },
{ index: { _index: 'students', _id: 600, data: { first_name: 'Richard', last_name: 'Roe', gpa: 3.04, grad_year: 2020 } } },
{ update: { _index: 'students', _id: 100, data: { doc: { gpa: 3.73 } } } },
{ delete: { _index: 'students', _id: 200 } }
]
client.bulk(body: actions, refresh: true)在上面的示例中,你将数据和头部一起传递,并使用 data: 键标记数据。
搜索文档
要搜索文档,请使用 search 方法。以下示例搜索名字或姓氏为 "James" 的学生。它使用 multi_match 查询搜索两个字段(first_name 和 last_name),并使用脱字符号(last_name^2)提升 last_name 字段的相关性。
q = 'James'
query = {
'size': 5,
'query': {
'multi_match': {
'query': q,
'fields': ['first_name', 'last_name^2']
}
}
}
response = client.search(
body: query,
index: 'students'
)如果你在 search 方法中省略请求体,你的查询将变为 match_all 查询并返回索引中的所有文档:
client.search(index: 'students')布尔查询
Ruby 客户端公开了完整的 OpenSearch 查询功能。除了使用 match 查询的简单搜索外,你还可以创建更复杂的布尔查询来搜索 2022 年毕业的学生并按姓氏排序。在下面的示例中,搜索限制为 10 个文档。
query = {
'query': {
'bool': {
'filter': {
'term': {
'grad_year': 2022
}
}
}
},
'sort': {
'last_name': {
'order': 'asc'
}
}
}
response = client.search(index: 'students', from: 0, size: 10, body: query)多重搜索
你可以使用 msearch 方法将多个查询批量组合在一起执行多重搜索。以下代码搜索 GPA 在 3.1–3.9 范围之外的学生:
actions = [
{},
{query: {range: {gpa: {gt: 3.9}}}},
{},
{query: {range: {gpa: {lt: 3.1}}}}
]
response = client.msearch(index: 'students', body: actions)滚动
你可以使用 Scroll API 对搜索结果进行分页:
response = client.search(index: index_name, scroll: '2m', size: 2)
while response['hits']['hits'].size.positive?
scroll_id = response['_scroll_id']
puts(response['hits']['hits'].map { |doc| [doc['_source']['first_name'] + ' ' + doc['_source']['last_name']] })
response = client.scroll(scroll: '1m', body: { scroll_id: scroll_id })
end首先,你发出搜索查询,指定 scroll 和 size 参数。scroll 参数告诉 OpenSearch 保持搜索上下文的时间。在本例中,它设置为两分钟。size 参数指定每个请求中要返回多少文档。
初始搜索查询的响应包含一个 _scroll_id,你可以使用它来获取下一组文档。为此,你使用 scroll 方法,再次指定 scroll 参数并在 body 中传递 _scroll_id。你不需要向 scroll 方法指定查询或索引。scroll 方法返回下一组文档和 _scroll_id。重要的是,在请求下一批文档时使用最新的 _scroll_id,因为 _scroll_id 可能会在请求之间发生变化。
删除索引
你可以使用 delete 方法删除索引:
response = client.indices.delete(index: index_name)示例程序
以下是一个完整的示例程序,说明了前面各节中描述的所有概念。Ruby 客户端的方法将响应作为 Ruby 哈希返回,这很难阅读。为了以漂亮的格式显示 JSON 响应,示例程序使用 MultiJson.dump 方法。
require 'opensearch'
client = OpenSearch::Client.new(host: 'http://localhost:9200')
# 创建具有非默认设置的索引
index_name = 'students'
index_body = {
'settings': {
'index': {
'number_of_shards': 1,
'number_of_replicas': 2
}
}
}
client.indices.create(
index: index_name,
body: index_body
)
# 创建映射
client.indices.put_mapping(
index: index_name,
body: {
properties: {
first_name: { type: 'keyword' },
last_name: { type: 'keyword' }
}
}
)
# 获取映射
response = client.indices.get_mapping(index: index_name)
puts 'Mappings for the students index:'
puts MultiJson.dump(response, pretty: "true")
# 向索引添加一个文档
puts 'Adding one document:'
document = {
first_name: 'Connor',
last_name: 'James',
gpa: 3.93,
grad_year: 2021
}
id = 100
client.index(
index: index_name,
body: document,
id: id,
refresh: true
)
response = client.search(index: index_name)
puts MultiJson.dump(response, pretty: "true")
# 更新文档
puts 'Updating a document:'
client.update(index: index_name, id: id, body: { doc: { gpa: 3.25 } }, refresh: true)
response = client.search(index: index_name)
puts MultiJson.dump(response, pretty: "true")
print 'The updated gpa is '
puts response['hits']['hits'].map { |doc| doc['_source']['gpa'] }
# 批量添加多个文档
documents = [
{ index: { _index: index_name, _id: '200' } },
{ first_name: 'James', last_name: 'Rodriguez', gpa: 3.91, grad_year: 2019},
{ index: { _index: index_name, _id: '300' } },
{ first_name: 'Nikki', last_name: 'Wolf', gpa: 3.87, grad_year: 2020}
]
client.bulk(body: documents, refresh: true)
# 获取索引中的所有文档
response = client.search(index: index_name)
puts 'All documents in the index after bulk upload:'
puts MultiJson.dump(response, pretty: "true")
# 使用 multi_match 查询搜索文档
puts 'Searching for documents that match "James":'
q = 'James'
query = {
'size': 5,
'query': {
'multi_match': {
'query': q,
'fields': ['first_name', 'last_name^2']
}
}
}
response = client.search(
body: query,
index: index_name
)
puts MultiJson.dump(response, pretty: "true")
# 删除文档
response = client.delete(
index: index_name,
id: id,
refresh: true
)
response = client.search(index: index_name)
puts 'Documents in the index after one document was deleted:'
puts MultiJson.dump(response, pretty: "true")
# 删除多个文档
actions = [
{ delete: { _index: index_name, _id: 200 } },
{ delete: { _index: index_name, _id: 300 } }
]
client.bulk(body: actions, refresh: true)
response = client.search(index: index_name)
puts 'Documents in the index after all documents were deleted:'
puts MultiJson.dump(response, pretty: "true")
# 批量执行多个操作
actions = [
{ index: { _index: index_name, _id: 100, data: { first_name: 'Paulo', last_name: 'Santos', gpa: 3.29, grad_year: 2022 } } },
{ index: { _index: index_name, _id: 200, data: { first_name: 'Shirley', last_name: 'Rodriguez', gpa: 3.92, grad_year: 2020 } } },
{ index: { _index: index_name, _id: 300, data: { first_name: 'Akua', last_name: 'Mansa', gpa: 3.95, grad_year: 2022 } } },
{ index: { _index: index_name, _id: 400, data: { first_name: 'John', last_name: 'Stiles', gpa: 3.72, grad_year: 2019 } } },
{ index: { _index: index_name, _id: 500, data: { first_name: 'Li', last_name: 'Juan', gpa: 3.94, grad_year: 2022 } } },
{ index: { _index: index_name, _id: 600, data: { first_name: 'Richard', last_name: 'Roe', gpa: 3.04, grad_year: 2020 } } },
{ update: { _index: index_name, _id: 100, data: { doc: { gpa: 3.73 } } } },
{ delete: { _index: index_name, _id: 200 } }
]
client.bulk(body: actions, refresh: true)
puts 'All documents in the index after bulk operations with scrolling:'
response = client.search(index: index_name, scroll: '2m', size: 2)
while response['hits']['hits'].size.positive?
scroll_id = response['_scroll_id']
puts(response['hits']['hits'].map { |doc| [doc['_source']['first_name'] + ' ' + doc['_source']['last_name']] })
response = client.scroll(scroll: '1m', body: { scroll_id: scroll_id })
end
# 多重搜索
actions = [
{},
{query: {range: {gpa: {gt: 3.9}}}},
{},
{query: {range: {gpa: {lt: 3.1}}}}
]
response = client.msearch(index: index_name, body: actions)
puts 'Multi search results:'
puts MultiJson.dump(response, pretty: "true")
# 布尔查询
query = {
'query': {
'bool': {
'filter': {
'term': {
'grad_year': 2022
}
}
}
},
'sort': {
'last_name': {
'order': 'asc'
}
}
}
response = client.search(index: index_name, from: 0, size: 10, body: query)
puts 'Boolean query search results:'
puts MultiJson.dump(response, pretty: "true")
# 删除索引
puts 'Deleting the index:'
response = client.indices.delete(index: index_name)
puts MultiJson.dump(response, pretty: "true")Ruby AWS Sigv4 客户端
opensearch-aws-sigv4 gem 提供 OpenSearch::Aws::Sigv4Client 类,它具有 OpenSearch::Client 的所有功能。这两个客户端之间唯一的区别是 OpenSearch::Aws::Sigv4Client 在实例化时需要一个 Aws::Sigv4::Signer 实例来与 AWS 进行身份验证:
require 'opensearch-aws-sigv4'
require 'aws-sigv4'
signer = Aws::Sigv4::Signer.new(service: 'es',
region: 'us-west-2',
access_key_id: 'key_id',
secret_access_key: 'secret')
client = OpenSearch::Aws::Sigv4Client.new({ log: true }, signer)
client.cluster.health
client.transport.reload_connections!
client.search q: 'test'