Back to Repositories

Testing Hybrid Search Implementation in Searchkick

This test suite validates hybrid search functionality in Searchkick, combining keyword-based and semantic vector search capabilities. It ensures proper implementation of multi-search features and reciprocal rank fusion (RRF) for result ranking.

Test Coverage Overview

The test suite covers essential hybrid search functionality in Searchkick, focusing on the integration of keyword and semantic vector searches.

  • Validates error handling for improper hybrid search attempts
  • Tests multi-search implementation with combined keyword and KNN vector searches
  • Verifies reciprocal rank fusion (RRF) scoring and result ordering

Implementation Analysis

The testing approach employs Minitest framework with systematic validation of hybrid search scenarios.

Key patterns include:
  • Setup skip conditions for KNN support verification
  • Error case validation for direct hybrid searches
  • Vector embedding integration testing with sample data
  • Score verification using assert_in_delta for floating-point comparisons

Technical Details

Testing infrastructure includes:
  • Minitest as the testing framework
  • Searchkick’s multi_search and reranking capabilities
  • KNN vector search support verification
  • Sample data with embedded vectors for search testing

Best Practices Demonstrated

The test suite exemplifies robust testing practices through comprehensive validation of hybrid search functionality.

  • Proper error handling verification
  • Precise floating-point comparisons for search scores
  • Clear test data organization with meaningful examples
  • Modular test structure with setup and individual test cases

ankane/searchkick

test/hybrid_test.rb

            
require_relative "test_helper"

class HybridTest < Minitest::Test
  def setup
    skip unless Searchkick.knn_support?
    super
  end

  def test_search
    error = assert_raises(ArgumentError) do
      Product.search("product", knn: {field: :embedding, vector: [1, 2, 3]})
    end
    assert_equal "Use Searchkick.multi_search for hybrid search", error.message
  end

  def test_multi_search
    store [
      {name: "The dog is barking", embedding: [1, 2, 0]},
      {name: "The cat is purring", embedding: [1, 0, 0]},
      {name: "The bear is growling", embedding: [1, 2, 3]}
    ]

    keyword_search = Product.search("growling bear")
    semantic_search = Product.search(knn: {field: :embedding, vector: [1, 2, 3]})
    Searchkick.multi_search([keyword_search, semantic_search])

    results = Searchkick::Reranking.rrf(keyword_search, semantic_search)
    expected = ["The bear is growling", "The dog is barking", "The cat is purring"]
    assert_equal expected, results.map { |v| v[:result].name }
    assert_in_delta 0.03279, results[0][:score]
    assert_in_delta 0.01612, results[1][:score]
    assert_in_delta 0.01587, results[2][:score]
  end
end