rom-rb/rom-elasticsearch
View on GitHubCreate command issues a full search query before fetching inserted document
Open
#28 opened on Feb 2, 2026
bughelp wanted
Repository metrics
- Stars
- (35 stars)
- PR merge metrics
- (PR metrics pending)
Description
Describe the bug
The create command issues a _search with no parameters (ie. a search for all documents belonging to an index) before fetching the actual inserted document by id.
I observed this behaviour in production on v0.3.0 and confirmed the issue also exists on current main and all released versions in-between.
To Reproduce
Add this logging monkeypatch to spec_helper.rb:
module LogES
def perform_request(method, path, params = {}, body = nil, headers = nil)
$stderr.puts("\e[34;1mES Request:\e[0m")
$stderr.puts(" #{method} #{path} - #{params.inspect}")
$stderr.puts(" Headers: #{headers.inspect}")
$stderr.puts(" Body: #{body.inspect}")
resp = super
$stderr.puts(" Response: #{resp.status}")
$stderr.puts(" Headers: #{resp.headers.inspect}")
$stderr.puts(" Body: #{resp.body.inspect}")
caller.each do |c|
$stderr.puts(" #{c}")
break if c.include?("/spec/")
end
$stderr.puts
sleep 1
resp
end
end
Elasticsearch::Transport::Client.prepend(LogES)
Then run rspec spec/integration/rom/elasticsearch/relation/command_spec.rb:18
Observe the following output:
ES Request:
PUT users/user/1 - {}
Headers: nil
Body: {:id=>1, :name=>"Jane"}
Response: 201
Headers: {"location"=>"/users/user/1", "warning"=>"299 Elasticsearch-6.8.23-4f67856 \"the default number of shards will change from [5] to [1] in 7.0.0; if you wish to continue using the default of [5] shards, you must manage this on the create index request or with an index template\"", "content-type"=>"application/json; charset=UTF-8", "content-length"=>"137"}
Body: {"_index"=>"users", "_type"=>"user", "_id"=>"1", "_version"=>1, "result"=>"created", "_shards"=>{"total"=>2, "successful"=>1, "failed"=>0}, "_seq_no"=>0, "_primary_term"=>1}
/home/hailey/code/rom-elasticsearch/vendor/bundle/ruby/2.7.0/gems/elasticsearch-api-6.8.3/lib/elasticsearch/api/actions/index.rb:86:in `index'
/home/hailey/code/rom-elasticsearch/lib/rom/elasticsearch/dataset.rb:81:in `put'
/home/hailey/code/rom-elasticsearch/lib/rom/elasticsearch/commands.rb:21:in `execute'
/home/hailey/code/rom-elasticsearch/vendor/bundle/ruby/2.7.0/gems/rom-core-5.2.6/lib/rom/command.rb:294:in `call'
/home/hailey/code/rom-elasticsearch/vendor/bundle/ruby/2.7.0/gems/rom-core-5.2.6/lib/rom/commands/composite.rb:19:in `call'
/home/hailey/code/rom-elasticsearch/spec/integration/rom/elasticsearch/relation/command_spec.rb:19:in `block (3 levels) in <top (required)>'
ES Request:
GET users/user/_search - {}
Headers: nil
Body: {}
Response: 200
Headers: {"content-type"=>"application/json; charset=UTF-8", "content-length"=>"172"}
Body: {"took"=>1, "timed_out"=>false, "_shards"=>{"total"=>5, "successful"=>5, "skipped"=>0, "failed"=>0}, "hits"=>{"total"=>1, "max_score"=>1.0, "hits"=>[{"_index"=>"users", "_type"=>"user", "_id"=>"1", "_score"=>1.0, "_source"=>{"id"=>1, "name"=>"Jane"}}]}}
/home/hailey/code/rom-elasticsearch/vendor/bundle/ruby/2.7.0/gems/elasticsearch-api-6.8.3/lib/elasticsearch/api/actions/search.rb:134:in `search'
/home/hailey/code/rom-elasticsearch/lib/rom/elasticsearch/dataset.rb:292:in `response'
/home/hailey/code/rom-elasticsearch/lib/rom/elasticsearch/dataset.rb:270:in `call'
/home/hailey/code/rom-elasticsearch/lib/rom/elasticsearch/relation.rb:163:in `call'
/home/hailey/code/rom-elasticsearch/vendor/bundle/ruby/2.7.0/gems/rom-core-5.2.6/lib/rom/relation/materializable.rb:38:in `one'
/home/hailey/code/rom-elasticsearch/lib/rom/elasticsearch/commands.rb:25:in `execute'
/home/hailey/code/rom-elasticsearch/vendor/bundle/ruby/2.7.0/gems/rom-core-5.2.6/lib/rom/command.rb:294:in `call'
/home/hailey/code/rom-elasticsearch/vendor/bundle/ruby/2.7.0/gems/rom-core-5.2.6/lib/rom/commands/composite.rb:19:in `call'
/home/hailey/code/rom-elasticsearch/spec/integration/rom/elasticsearch/relation/command_spec.rb:19:in `block (3 levels) in <top (required)>'
ES Request:
GET users/user/1 - {}
Headers: nil
Body: nil
Response: 200
Headers: {"content-type"=>"application/json; charset=UTF-8", "content-length"=>"123"}
Body: {"_index"=>"users", "_type"=>"user", "_id"=>"1", "_version"=>1, "_seq_no"=>0, "_primary_term"=>1, "found"=>true, "_source"=>{"id"=>1, "name"=>"Jane"}}
/home/hailey/code/rom-elasticsearch/vendor/bundle/ruby/2.7.0/gems/elasticsearch-api-6.8.3/lib/elasticsearch/api/actions/get.rb:53:in `get'
/home/hailey/code/rom-elasticsearch/lib/rom/elasticsearch/dataset.rb:282:in `view'
/home/hailey/code/rom-elasticsearch/lib/rom/elasticsearch/dataset.rb:137:in `each'
/home/hailey/code/rom-elasticsearch/lib/rom/elasticsearch/dataset.rb:125:in `each'
/home/hailey/code/rom-elasticsearch/lib/rom/elasticsearch/dataset.rb:125:in `to_a'
/home/hailey/code/rom-elasticsearch/lib/rom/elasticsearch/dataset.rb:125:in `to_a'
/home/hailey/code/rom-elasticsearch/lib/rom/elasticsearch/dataset.rb:150:in `map'
/home/hailey/code/rom-elasticsearch/vendor/bundle/ruby/2.7.0/gems/rom-core-5.2.6/lib/rom/relation.rb:223:in `each'
/home/hailey/code/rom-elasticsearch/vendor/bundle/ruby/2.7.0/gems/rom-core-5.2.6/lib/rom/relation.rb:363:in `each'
/home/hailey/code/rom-elasticsearch/vendor/bundle/ruby/2.7.0/gems/rom-core-5.2.6/lib/rom/relation.rb:363:in `to_a'
/home/hailey/code/rom-elasticsearch/vendor/bundle/ruby/2.7.0/gems/rom-core-5.2.6/lib/rom/relation.rb:363:in `to_a'
/home/hailey/code/rom-elasticsearch/vendor/bundle/ruby/2.7.0/gems/rom-core-5.2.6/lib/rom/relation/loaded.rb:38:in `initialize'
/home/hailey/code/rom-elasticsearch/lib/rom/elasticsearch/relation.rb:163:in `new'
/home/hailey/code/rom-elasticsearch/lib/rom/elasticsearch/relation.rb:163:in `call'
/home/hailey/code/rom-elasticsearch/vendor/bundle/ruby/2.7.0/gems/rom-core-5.2.6/lib/rom/relation/materializable.rb:38:in `one'
/home/hailey/code/rom-elasticsearch/lib/rom/elasticsearch/commands.rb:25:in `execute'
/home/hailey/code/rom-elasticsearch/vendor/bundle/ruby/2.7.0/gems/rom-core-5.2.6/lib/rom/command.rb:294:in `call'
/home/hailey/code/rom-elasticsearch/vendor/bundle/ruby/2.7.0/gems/rom-core-5.2.6/lib/rom/commands/composite.rb:19:in `call'
/home/hailey/code/rom-elasticsearch/spec/integration/rom/elasticsearch/relation/command_spec.rb:19:in `block (3 levels) in <top (required)>'
Expected behavior
The _search request should not be issued.
My environment
- Affects my production application: YES
- Ruby version: 2.7.8
- OS: Linux