Skip to content

Commit

Permalink
add solr search using HTTP post instead of GET for large queries
Browse files Browse the repository at this point in the history
  • Loading branch information
syphax-bouazzouni committed Mar 2, 2024
1 parent 10b90c1 commit 84aa3db
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/goo/search/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ def search(q, params = {}, connection_name = search_collection_name)
search_client(connection_name).search(q, params)
end

def submit_search_query(query, params = {}, connection_name = search_collection_name)
search_client(connection_name).submit_search_query(query, params)
end

def indexBatch(collection, connection_name = search_collection_name)
docs = collection.map(&:indexable_object)
search_client(connection_name).index_document(docs)
Expand Down
23 changes: 22 additions & 1 deletion lib/goo/search/solr/solr_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def index_document_attr(key, type, is_list, fuzzy_search)
end

def delete_by_id(document_id, commit: true)
return if document_id.nil?
return if document_id.nil?

@solr.delete_by_id(document_id)
@solr.commit if commit
Expand All @@ -37,12 +37,33 @@ def delete_by_query(query)
@solr.delete_by_query(query)
@solr.commit
end

def search(query, params = {})
params[:q] = query
@solr.get('select', params: params)
end

def submit_search_query(query, params = {})
uri = ::URI.parse("#{collection_url}/select")

http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri)

params[:q] = query
request.set_form_data(params)

response = http.request(request)

if response.is_a?(Net::HTTPSuccess)
JSON.parse(response.body)
else
puts "Error: #{response.code} - #{response.message}"

Check warning on line 60 in lib/goo/search/solr/solr_query.rb

View check run for this annotation

Codecov / codecov/patch

lib/goo/search/solr/solr_query.rb#L60

Added line #L60 was not covered by tests
nil
end
end

private

def dynamic_field(type:, is_list:, is_fuzzy_search: false)
return is_list ? '*_texts' : '*_text' if is_fuzzy_search

Expand Down
4 changes: 2 additions & 2 deletions test/test_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,8 @@ def test_index_on_save_delete
term2.delete
term.delete

indexed_term = TermSearch2.search("id:#{term.id.to_s.gsub(":", "\\:")}")["response"]["docs"].first
indexed_term2 = TermSearch3.search("id:#{term2.id.to_s.gsub(":", "\\:")}")["response"]["docs"].first
indexed_term = TermSearch2.submit_search_query("id:#{term.id.to_s.gsub(":", "\\:")}")["response"]["docs"].first
indexed_term2 = TermSearch3.submit_search_query("id:#{term2.id.to_s.gsub(":", "\\:")}")["response"]["docs"].first

assert_nil indexed_term
assert_nil indexed_term2
Expand Down

0 comments on commit 84aa3db

Please sign in to comment.