如何用Rspec编写elasticsearch的规范

时间:2018-04-30 12:56:20

标签: ruby-on-rails elasticsearch rspec rspec-rails elastic-cloud

我正在尝试为弹性搜索编写规范,我的生产堆栈是: Ruby 2.3.1,rails 5.0.3,托管弹性搜索集群(cloud.elastic.co)。

情况

在托管集群中索引文档时,会添加一些索引属性,例如,如果我索引建筑物(Rails模型)实例的名称,则索引文档中将有一个名称和一个“name.keyword”

问题

我使用https://github.com/elastic/elasticsearch-ruby/tree/master/elasticsearch-extensions#testcluster在我的rspec套件之前创建了一个测试集群,并在我的OSX上使用Homebrew(brew install elasticsearch)安装了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

欢迎任何想法!

0 个答案:

没有答案