4523: slightly modified full text search condition.
[arvados.git] / services / api / lib / record_filters.rb
index 9408dcfade120e5b68235f952eb980ef7c443c89..51153fe081267162139a3ec0dbadbe49bfd1b1eb 100644 (file)
@@ -22,7 +22,7 @@ module RecordFilters
     ar_table_name = model_class.table_name
     filters.each do |filter|
       attrs_in, operator, operand = filter
-      if attrs_in == 'any'
+      if attrs_in == 'any' && operator != '@@'
         attrs = model_class.searchable_columns(operator)
       elsif attrs_in.is_a? Array
         attrs = attrs_in
@@ -34,8 +34,22 @@ module RecordFilters
       elsif !operator.is_a? String
         raise ArgumentError.new("Invalid operator '#{operator}' (#{operator.class}) in filter")
       end
+
       cond_out = []
-      attrs.each do |attr|
+
+      if operator == '@@' # full-text-search
+        if attrs_in != 'any'
+          raise ArgumentError.new("Full text search on individual columns is not supported")
+        end
+        attrs = [] #  skip the generic per-column operator loop below
+        # Use to_tsquery since plainto_tsquery does not support prefix search.
+        # Instead split operand and join the words with ' & ' and add ':*' to the last word
+        # Thus when searched for "some str", objects containing "some" and "str:*" are found.
+        cond_out << model_class.full_text_tsvector+" @@ to_tsquery(?)"
+        operand << ':*'
+        param_out << operand.split.join(' & ')
+      else
+       attrs.each do |attr|
         if !model_class.searchable_columns(operator).index attr.to_s
           raise ArgumentError.new("Invalid attribute '#{attr}' in filter")
         end
@@ -104,6 +118,7 @@ module RecordFilters
           end
           cond_out << cond.join(' OR ')
         end
+       end
       end
       conds_out << cond_out.join(' OR ') if cond_out.any?
     end