gpt4 book ai didi

ruby-on-rails - 在Ruby中使用 Elasticsearch gem时在哪里指定集群详细信息

转载 作者:行者123 更新时间:2023-12-03 01:52:12 25 4
gpt4 key购买 nike

我想从Elastic Search Cluster应用程序访问rails中的数据。可以说服务器正在http://localhost:9200上运行,我想访问端点http://localhost:9200/location/type

以下是documentation,并遇到了此示例:

require 'elasticsearch'

client = Elasticsearch::Client.new log: true

client.cluster.health

client.index index: 'my-index', type: 'my-document', id: 1, body: { title: 'Test' }

client.indices.refresh index: 'my-index'

client.search index: 'my-index', body: { query: { match: { title: 'test' } } }

问题:
  • 我将在代码中定义elasticsearch cluster的详细信息吗?集群运行在http://localhost:9200/
  • 最佳答案

    作为文档的具体说明,elasticsearch gem包装了用于连接到集群的elasticsearch-transport和用于访问elasticsearch API的elasticsearch-api。从elasticsearch-transport的文档中,

    以最简单的形式,无需任何配置即可连接到在http://localhost:9200上运行的Elasticsearch:

    因此,基本上,client = Elasticsearch::Client.new log: true将默认连接到在localhost:9200(与Rails应用程序位于同一台机器)上运行的集群。

    继续,尝试在建立连接后执行client.cluster.health,您将知道连接是否成功。

    此外,如果您的群集在其他服务器上运行,则可以使用以下命令连接到该服务器:

    es = Elasticsearch::Client.new host: http(s)://<path-to-cluster-server>

    关于ruby-on-rails - 在Ruby中使用 Elasticsearch gem时在哪里指定集群详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39473243/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com