我正在尝试为弹性搜索编写规范,我的生产堆栈是:Ruby 2.3.1,rails 5.0.3,托管弹性搜索集群(cloud.elastic.co)。
情况
在托管集群中索引文档时,会添加一些索引属性,例如,如果我索引我的 Building(Rails 模型)实例的名称,则索引文档中会有一个名称和一个“name.keyword”。
问题
我使用https://github.com/elastic/elasticsearch-ruby/tree/master/elasticsearch-extensions#testcluster在我的 rspec 套件之前创建了一个测试集群,并brew install elasticsearch
在我的 OSX 上使用 Homebrew ( ) 安装了 elasticsearch 6.2.4。然后我开始编写规范,在我的控制器操作中,我排序,name.keyword
以便查询看起来像
{
query: {
sort: {
name: { order: :asc}
}
}
}
在这一点上我得到一个错误。
错误
...
Failure/Error: response.headers["count"] = buildings.response.hits.total
Elasticsearch::Transport::Transport::Errors::BadRequest:
[400] {"error":{"root_cause":[{"type":"query_shard_exception","reason":"No mapping found for [name.keyword] in order to sort on","index_uuid":"XXqX1-GCQbaHDkhHeTZS5Q","index":"buildings"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"buildings","node":"INMaKXXNT5S26JBjhTHUqw","reason":{"type":"query_shard_exception","reason":"No mapping found for [name.keyword] in order to sort on","index_uuid":"XXqX1-GCQbaHDkhHeTZS5Q","index":"buildings"}}]},"status":400}
Shared Example Group: "authentication" called from ./spec/controllers/user_api/v1/buildings_controller_spec.rb:5
# /Users/Thomas/.rvm/gems/ruby-2.3.1/gems/elasticsearch-transport-5.0.5/lib/elasticsearch/transport/transport/base.rb:202:in `__raise_transport_error'
# /Users/Thomas/.rvm/gems/ruby-2.3.1/gems/elasticsearch-transport-5.0.5/lib/elasticsearch/transport/transport/base.rb:319:in `perform_request'
# /Users/Thomas/.rvm/gems/ruby-2.3.1/gems/elasticsearch-transport-5.0.5/lib/elasticsearch/transport/transport/http/faraday.rb:20:in `perform_request'
# /Users/Thomas/.rvm/gems/ruby-2.3.1/gems/elasticsearch-transport-5.0.5/lib/elasticsearch/transport/client.rb:131:in `perform_request'
...
错误的主要部分:"reason":"No mapping found for [name.keyword] in order to sort on"
假设
- 1/ 我需要自己编写映射,并提供字段 name.keyword,但我不确定如何执行此操作
- 2/ 我应该使用托管测试集群来复制相同的行为
我认为 1/ 可能是要走的路,但是弹性搜索指南在谈论他们的宝石时很难阅读,而我一直无法这样做。2/ 当然更容易,但我想知道从长远来看它是否不会导致更多问题(性能等)
文档
https://medium.com/@rowanoulton/testing-elasticsearch-in-rails-22a3296d989 https://github.com/elastic/elasticsearch-rails/issues/69
欢迎任何想法!