unexist.dev

Changeset 59:7b78bd28ad05

Added fulltext search
author unexist
date Wed, 15 Aug 2018 13:36:49 +0200
parents a53b371271fc
children 745dadf097f3
files irc/cinch.rb
diffstat 1 files changed, 32 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/irc/cinch.rb	Tue Aug 14 09:46:19 2018 +0200
+++ b/irc/cinch.rb	Wed Aug 15 13:36:49 2018 +0200
@@ -607,9 +607,15 @@
       raise
     end # }}}
 
-    match /^!find (.+)/, method: :find_phrase, use_prefix: false # {{{
-    def find_phrase(m, key)
-      likes = find_alike(key)
+    match /^([!~])find (.+)/, method: :find_phrase, use_prefix: false # {{{
+    def find_phrase(m, op, key)
+      case op
+        when "!"
+          likes = find_alike(key)
+        when "~"
+          likes = find_fulltext(key)
+      end
+     
       m.reply("Matches for %s: %s" % [ key, likes ], true) unless likes.empty?
     rescue
       m.reply("Oops something went wrong..", true)
@@ -806,6 +812,29 @@
       result
     end # }}}
 
+def find_fulltext(key, limit = 10) # {{{
+  result  = ""
+  lookup  = key.downcase.strip
+  where   = {
+    :value.like => "%#{lookup}%",
+    :limit      => limit
+  }
+
+  versions = Version.all(where)
+
+  unless versions.nil?
+    matches = []
+
+    versions.each do |v|
+      matches << "%s[%d]" % [ v.phrase.name, v.version ]
+    end
+
+    result = matches.join(", ") unless matches.empty?
+  end
+
+  result
+end #  }}}
+
   end # }}}
 
   class Actions # {{{