示例编辑

以下是一些使用 Ruby 客户端调用最常用 API 的示例。

索引文档编辑

让我们索引一个包含以下字段的文档:nameauthorrelease_datepage_count

body = {
  name: "The Hitchhiker's Guide to the Galaxy",
  author: "Douglas Adams",
  release_date: "1979-10-12",
  page_count: 180
}

client.index(index: 'books', body: body)

获取文档编辑

您可以通过 ID 获取文档

id = 'l7LRjXgB2_ry9btNEv_V'
client.get(index: 'books', id: id)

更新文档编辑

假设您有以下文档

book = {"name": "Foundation", "author": "Isaac Asimov", "release_date": "1951-06-01", "page_count": 224}

您可以使用以下脚本更新文档

response = client.index(index: 'books', body: book)
id = response['_id']
client.update(index: 'books', id: id, body: { doc: { page_count: 225 } })

删除文档编辑

您可以通过 ID 删除文档

id = 'l7LRjXgB2_ry9btNEv_V'
client.delete(index: 'books', id: id)

批量请求编辑

客户端的 bulk 操作支持多种不同的有效负载格式:字符串数组、头/数据对,或者将数据与头一起传递到自定义 :data 键中的单个项目中的组合格式。

在一个请求中索引多个文档

body = [
  { index: { _index: 'books', _id: '42' } },
  { name: "The Hitchhiker's Guide to the Galaxy", author: 'Douglas Adams', release_date: '1979-10-12', page_count: 180},
  { index: { _index: 'books', _id: '43' } },
  { name: 'Snow Crash', author: 'Neal Stephenson', release_date: '1992-06-01', page_count: 470 },
  { index: { _index: 'books', _id: '44' } },
  { name: 'Starship Troopers', author: 'Robert A. Heinlein', release_date: '1959-12-01', page_count: 335}
]
client.bulk(body: body)

删除多个文档

body = [
  { delete: { _index: 'books', _id: '42' } },
  { delete: { _index: 'books', _id: '43' } },
]
client.bulk(body: body)

如上所述,您可以使用组合格式在一个请求中执行多个操作,将数据传递到 :data 选项中

body = [
  { index:  { _index: 'books', _id: 45, data: { name: '1984', author: 'George Orwell', release_date: '1985-06-01', page_count: 328 } } },
  { update: { _index: 'books', _id: 43, data: { doc: { page_count: 471 } } } },
  { delete: { _index: 'books', _id: 44  } }
]
client.bulk(body: body)

搜索文档编辑

此示例使用与 查找结构 API 文档 中相同的示例数据。

首先,批量索引它

body = [
  { index: { _index: 'books', data: {name: "Leviathan Wakes", "author": "James S.A. Corey", "release_date": "2011-06-02", "page_count": 561} } },
  { index: { _index: 'books', data: {name: "Hyperion", "author": "Dan Simmons", "release_date": "1989-05-26", "page_count": 482} } },
  { index: { _index: 'books', data: {name: "Dune", "author": "Frank Herbert", "release_date": "1965-06-01", "page_count": 604} } },
  { index: { _index: 'books', data: {name: "Dune Messiah", "author": "Frank Herbert", "release_date": "1969-10-15", "page_count": 331} } },
  { index: { _index: 'books', data: {name: "Children of Dune", "author": "Frank Herbert", "release_date": "1976-04-21", "page_count": 408} } },
  { index: { _index: 'books', data: {name: "God Emperor of Dune", "author": "Frank Herbert", "release_date": "1981-05-28", "page_count": 454} } },
  { index: { _index: 'books', data: {name: "Consider Phlebas", "author": "Iain M. Banks", "release_date": "1987-04-23", "page_count": 471} } },
  { index: { _index: 'books', data: {name: "Pandora's Star", "author": "Peter F. Hamilton", "release_date": "2004-03-02", "page_count": 768} } },
  { index: { _index: 'books', data: {name: "Revelation Space", "author": "Alastair Reynolds", "release_date": "2000-03-15", "page_count": 585} } },
  { index: { _index: 'books', data: {name: "A Fire Upon the Deep", "author": "Vernor Vinge", "release_date": "1992-06-01", "page_count": 613} } },
  { index: { _index: 'books', data: {name: "Ender's Game", "author": "Orson Scott Card", "release_date": "1985-06-01", "page_count": 324} } },
  { index: { _index: 'books', data: {name: "1984", "author": "George Orwell", "release_date": "1985-06-01", "page_count": 328} } },
  { index: { _index: 'books', data: {name: "Fahrenheit 451", "author": "Ray Bradbury", "release_date": "1953-10-15", "page_count": 227} } },
  { index: { _index: 'books', data: {name: "Brave New World", "author": "Aldous Huxley", "release_date": "1932-06-01", "page_count": 268} } },
  { index: { _index: 'books', data: {name: "Foundation", "author": "Isaac Asimov", "release_date": "1951-06-01", "page_count": 224} } },
  { index: { _index: 'books', data: {name: "The Giver", "author": "Lois Lowry", "release_date": "1993-04-26", "page_count": 208} } },
  { index: { _index: 'books', data: {name: "Slaughterhouse-Five", "author": "Kurt Vonnegut", "release_date": "1969-06-01", "page_count": 275} } },
  { index: { _index: 'books', data: {name: "The Hitchhiker's Guide to the Galaxy", "author": "Douglas Adams", "release_date": "1979-10-12", "page_count": 180} } },
  { index: { _index: 'books', data: {name: "Snow Crash", "author": "Neal Stephenson", "release_date": "1992-06-01", "page_count": 470} } },
  { index: { _index: 'books', data: {name: "Neuromancer", "author": "William Gibson", "release_date": "1984-07-01", "page_count": 271} } },
  { index: { _index: 'books', data: {name: "The Handmaid's Tale", "author": "Margaret Atwood", "release_date": "1985-06-01", "page_count": 311} } },
  { index: { _index: 'books', data: {name: "Starship Troopers", "author": "Robert A. Heinlein", "release_date": "1959-12-01", "page_count": 335} } },
  { index: { _index: 'books', data: {name: "The Left Hand of Darkness", "author": "Ursula K. Le Guin", "release_date": "1969-06-01", "page_count": 304} } },
  { index: { _index: 'books', data: {name: "The Moon is a Harsh Mistress", "author": "Robert A. Heinlein", "release_date": "1966-04-01", "page_count": 288 } } }
]
client.bulk(body: body)

field 参数是一个常用参数,因此可以以以下方式直接传递:

client.search(index: 'books', q: 'dune')

您可以使用主体请求参数执行相同的操作

client.search(index: 'books', q: 'dune', body: { fields: [{ field: 'name' }] })
response = client.search(index: 'books', body: { size: 15 })
response['hits']['hits'].count # => 15

多重搜索编辑

以下示例展示了如何在 books 索引上执行多重搜索 API 调用

body = [
  {},
  {query: {range: {page_count: {gte: 100}}}},
  {},
  {query: {range: {page_count: {lte: 100}}}}
]
client.msearch(index:'books', body: body)

滚动编辑

提交一个包含滚动查询参数的搜索 API 请求,保存搜索 ID,然后打印出您找到的书籍名称

# Search request with a scroll argument:
response = client.search(index: 'books', scroll: '10m')

# Save the scroll id:
scroll_id = response['_scroll_id']

# Print out the names of all the books we find:
while response['hits']['hits'].size.positive?
  response = client.scroll(scroll: '5m', body: { scroll_id: scroll_id })
  puts(response['hits']['hits'].map { |r| r['_source']['name'] })
end

重新索引编辑

以下示例展示了如何将 books 索引重新索引到名为 books-reindexed 的新索引中

client.reindex(body: {source: { index: 'books'}, dest: {index: 'books-reindexed' } })