How can I do a full text search with a phrase of more than one word, using Sequel full_text_search?

StackOverflow https://stackoverflow.com/questions/21773130

  •  11-10-2022
  •  | 
  •  

Question

I am using ruby, Sequel and postgres. I have implemented full text search as follows:

get '/full_text_search' do
search_term = params[:search_term] 

result = DB[:candidates].full_text_search(:experience, search_term).map do |row|
 { :id => row[:id], :first => row[:first], :last => row[:last], :designation => row[:designation], :company => row[:company], :email => row[:email], :phone => row[:phone], :city => row[:city], :country => row[:country], :industry => row[:industry], :status => row[:status], :remarks => row[:remarks], :experience => row[:experience] }
    end
  halt 200, result.to_json
end

This is executed by passing a search term from a text box, via an ajax call, so:

$(document).ready(function(){
$("#full_text_search").click(function(){ 
var search_term = $("#search_box").val();
    $.getJSON("/full_text_search?search_term="+search_term, function(data) {
        $.each( data, function( key, value ) {  
        //do something with returned data here
         });//end each
          });//end getJSON
     });//end click
});//end doc

If my search term is just one word, "private" for example, I get the results back no problem but if I try a search term of 2 words (or more) "private equity", for example, I get an error and no data is returned.

So my question is how can I execute full text search (as above) with a phrase of 2 or more words?

I have already tried encapsulating the passed parameter with '' and "" unsuccessfully.

All help gratefully received. Thank you.

Was it helpful?

Solution

PostgreSQL's full text searching does not handle phrase searching natively. The best you can do is look for entries containing both words, using &: full_text_search(:experience, 'term1 & term2'), followed by using LIKE '%term1 term2%' if you want to do actual phrase searching.

I'll look into adding an :phrase=>true option to full_text_search that will make it operate as a phrase search.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top