1 year ago

#327596

test-img

panuza

How can I search record stored in jsonb field using search kick on rails

I have jsonb field ocr_extract in products, and I have a search input that should query the products through ocr_extract[:text] field.

Data stored on ocr_extract field in this format:

ocr_extract:
   "[{\"text\": \"raa\", \"x\": 550, \"y\": 195, \"w\": 19, \"h\": 3}, \n                       {\"text\": \"ee\", \"x\": 570, \"y\": 194, \"w\": 134, \"h\": 8}, \n                       {\"text\": \"sinter\", \"x\": 752, \"y\": 193, \"w\": 76, \"h\": 9}, \n                       {\"text\": \"aoe\", \"x\": 806, \"y\": 180, \"w\": 17, \"h\": 6}, \n                       {\"text\": \"3se\", \"x\": 853, \"y\": 195, \"w\": 137, \"h\": 3}, \n                       {\"text\": \"mr\", \"x\": 991, \"y\": 195, \"w\": 52, \"h\": 3}, \n                       {\"text\": \"sea\", \"x\": 636, \"y\": 216, \"w\": 17, \"h\": 6}, \n                       {\"text\": \"2\", \"x\": 763, \"y\": 208, \"w\": 13, \"h\": 6}, \n                       {\"text\": \"=\", \"x\": 804, \"y\": 216, \"w\": 10, \"h\": 6}, \n                       {\"text\": \"16\", \"x\": 962, \"y\": 216, \"w\": 17, \"h\": 6}]" 

This is how my code looks like:

product.rb

searchkick index_name: "products"

  def search_data
    {
      id: self.id,
      ocr_extract: self.ocr_extract,
      created_at: self.created_at
    }
  end

products_controller.rb

Product.search(query, fields: [:ocr_extract], match: :word_middle)

ruby-on-rails

elasticsearch

searchkick

0 Answers

Your Answer

Accepted video resources