我想从我的rails
应用程序访问Elastic Search Cluster
中的数据。假设服务器运行在http://localhost:9200
,我想访问端点http://localhost:9200/location/type
。
下面的这个文档,遇到了这个例子:
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>