0

I've looked through everything and can't seem to find an answer or whats wrong. Ive run my migrations .I have bundle installed with the gems "pg_trgm" and "pg_search". I can see " enable_extension "pg_trgm" " has been added to my schema.

Model and Controller seen below

When I have :tsearch I get results, but I comment it out and try using only Trigram I get nothing. I've added the server output below. For the search "harness" where there should be at least 5 exact matches in the title column.

I'm frustrated because tsearch matches "har" (pulls up harnesses) but then finds some jackets as you type "harnes" and then when you search "harness" it of course matches. I'd like trigram to match things like 'harne' to 'harness'

Do I need to add an index on something in the migration as a requirement? Or require something in the model aside from Include ?

Or installing it locally and not just by running the rails command and bundle install?

This is my first post on here, forgive the formatting - I'll take advice on this as well?.

EDIT: formatting this post and using Rails 7.0.3.1

model:

    include PgSearch::Model

  pg_search_scope :search_item, against: {name: 
'A', description: 'B' },  using: {
    # :tsearch => { prefix: true , any_word: m. 
true, dictionary: 'english'},
      :trigram => { threshold: 0.1}
  }

ActiveRecord::Schema[7.0].define(version: 2022_10_06_002757) do

  enable_extension "pg_trgm"

  create_table "pg_search_documents", force: :cascade do |t|
    t.text "content"
    t.string "searchable_type"
    t.bigint "searchable_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["searchable_type", "searchable_id"], name: "index_pg_search_documents_on_searchable"
  end


class InstallPgTrgm < ActiveRecord::Migration[7.0]

  def up
    execute "CREATE EXTENSION IF NOT EXISTS pg_trgm;"
  end

  def down
    execute "DROP EXTENSION IF EXISTS pg_trgm;"
  end
  
end

class Api::SearchController < ApplicationController

  def search5

    @items = Item.search_item(params[:query]).limit(5)
    render :results
  end
end



Started GET "/api/search5/harness" for 127.0.0.1 at 2022-10-06 10:14:35 -0700
Processing by Api::SearchController#search5 as JSON
  Parameters: {"query"=>"harness"}
  Rendering api/search/results.json.jbuilder
  Item Load (7.0ms)  SELECT "items".* FROM "items" INNER JOIN (SELECT "items"."id" AS pg_search_id, (ts_rank((setweight(to_tsvector('simple', coalesce("items"."name"::text, '')), 'A') || setweight(to_tsvector('simple', coalesce("items"."description"::text, '')), 'B')), (to_tsquery('simple', ''' ' || 'harness' || ' ''')), 0)) AS rank FROM "items" WHERE (similarity('harness', (coalesce("items"."name"::text, '') || ' ' || coalesce("items"."description"::text, ''))) >= 0.1)) AS pg_search_5f3c4f8580d392e422e7c2 ON "items"."id" = pg_search_5f3c4f8580d392e422e7c2.pg_search_id ORDER BY pg_search_5f3c4f8580d392e422e7c2.rank DESC, "items"."id" ASC LIMIT $1  [["LIMIT", 5]]
  ↳ app/views/api/search/results.json.jbuilder:3
  Rendered api/search/results.json.jbuilder (Duration: 8.4ms | Allocations: 652)
Completed 200 OK in 10ms (Views: 1.8ms | ActiveRecord: 7.0ms | Allocations: 1514)

0 Answers0