Search code examples
ruby-on-railsrubypostgresqlpg-search

How to postgres search with two search text parameter, Search FIlter text 1 && search filter text 2


How to Postgres search with two search text parameter , pg-search + Search FIlter 1 + search filter 2 .

How to search in pg_Search output I want union of "filter 1" && "filter 1"

 @output = PgSearch.multisearch("filter 1") +  PgSearch.multisearch("filter 2")

Solution

  • I'm the author and maintainer of pg_search.

    Search terms are joined by AND by default in PostgreSQL full text search. Thus, you could simply do this:

     @output = PgSearch.multisearch("filter 1 filter 2")
    

    Assuming you have the query terms in string variables, it would look something like one of these:

     @output = PgSearch.multisearch("#{query1} #{query2}") # if query1 and query2 are strings
     @output = PgSearch.multisearch(queries.join(" "))     # if queries is an array of strings
    

    Joining by OR is a lot more complicated. I don't yet have an easy solution for it, although it should be possible.