pg_search icon indicating copy to clipboard operation
pg_search copied to clipboard

Multisearch with multiple search terms

Open Joel-Mercier opened this issue 3 years ago • 0 comments

Thanks for this gem it's really useful ! I have an issue I'm stuck with. I'm using multisearch method and I would like to have the search term "American psycho" for example to only return records where the title contains both terms (at the moment it's returning records with "american" and "psycho" in the title. I haven't found any answers in the github.

include PgSearch::Model
multisearchable against: %i[title body],
                  additional_attributes: ->(r) { { model_type: r.class.name } },
                  if: :published?
PgSearch.multisearch(params[:term]).where(...)

Here is the SQL query it's producing

SELECT COUNT(*) FROM "pg_search_documents" INNER JOIN (SELECT "pg_search_documents"."id" AS pg_search_id, (ts_rank((to_tsvector('simple', coalesce("pg_search_documents"."content"::text, ''))), (to_tsquery('simple', ''' ' || 'american' || ' ''' || ':*') || to_tsquery('simple', ''' ' || 'psycho' || ' ''' || ':*')), 2)) AS rank FROM "pg_search_documents" WHERE ((to_tsvector('simple', coalesce("pg_search_documents"."content"::text, ''))) @@ (to_tsquery('simple', ''' ' || 'american' || ' ''' || ':*') || to_tsquery('simple', ''' ' || 'psycho' || ' ''' || ':*')))) AS pg_search_ce9b9dd18c5c0023f2116f ON "pg_search_documents"."id" = pg_search_ce9b9dd18c5c0023f2116f.pg_search_id WHERE ("pg_search_documents"."model_type" = $1) AND ("pg_search_documents"."searchable_type" = $2)

Is there a way to change these || to && ?

Joel-Mercier avatar Jul 22 '22 07:07 Joel-Mercier