postgres-fulltext-search-helper 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in postgres-fulltext-search-helper.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Justin Fitzsimmons
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # PostgresFulltextSearchHelper
2
+
3
+ This is just a quick helper gem for Nulogy's extremely naive full text search powered autocomplete. It abstracts away a lot of the flexibility of the postgres full text search API in favour of a very simple API that fulfills our simple needs.
4
+
5
+ This gem can be used to sanitize user input for safe queries into postgres's tsquery language, and will generate somewhat predictable results. All characters that are meaningful to the tsquery language are simply filtered out and interpreted as a boolean '&', which we find to be effective enough for the purposes of autocomplete.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'postgres-fulltext-search-helper'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install postgres-fulltext-search-helper
20
+
21
+ ## Usage
22
+
23
+ ```PostgresFulltextSearchHelper.format_query_for_fulltext("my query")``` will return a string that is suitable for insertion into a tsquery string for postgres.
24
+
25
+ If you're using Active Record or another library that implements ```where``` on a scope in a similar manner to Active Record, you can pass the relevant scope, field name, and query into the ```search``` method, like this:
26
+
27
+ ```ruby
28
+ filtered_scope = PostgresFulltextSearchHelper.search(scope, "name", "jus fit")
29
+ ```
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,9 @@
1
+ module Postgres
2
+ module Fulltext
3
+ module Search
4
+ module Helper
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,16 @@
1
+ require "postgres-fulltext-search-helper/version"
2
+
3
+ module PostgresFulltextSearchHelper
4
+ extend self
5
+
6
+ POSTGRES_TSQUERY_SENSITIVE_CHARACTERS = /[|&!():\* ]/
7
+
8
+ def format_query_for_fulltext(query)
9
+ parts = query.split(POSTGRES_TSQUERY_SENSITIVE_CHARACTERS).delete_if{|x| x.strip.empty? }
10
+ return parts.map{|token| token + ':*'}.join(' & ')
11
+ end
12
+
13
+ def search(scope, field_name, query)
14
+ return scope.where("to_tsvector('simple', #{field_name}::text) @@ to_tsquery('simple', ?)", [format_query_for_fulltext(query)])
15
+ end
16
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'postgres-fulltext-search-helper/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "postgres-fulltext-search-helper"
8
+ gem.version = Postgres::Fulltext::Search::Helper::VERSION
9
+ gem.authors = ["Justin Fitzsimmons", "Arturo Pie"]
10
+ gem.email = ["justinf@nulogy.com", "arturop@nulogy.com"]
11
+ gem.description = %q{Use postgres' fulltext search with ease}
12
+ gem.summary = ""
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency "rspec"
21
+ end
@@ -0,0 +1,30 @@
1
+ require_relative "../lib/postgres-fulltext-search-helper"
2
+
3
+ Helper = PostgresFulltextSearchHelper
4
+
5
+ describe PostgresFulltextSearchHelper do
6
+ it "returns a string that can be used by postgres" do
7
+ query = "hello"
8
+
9
+ expect(Helper.format_query_for_fulltext(query)).to eq("hello:*")
10
+ end
11
+
12
+ it "whitespace is interpreted as &" do
13
+ query = "hello world"
14
+
15
+ expect(Helper.format_query_for_fulltext(query)).to eq("hello:* & world:*")
16
+ end
17
+
18
+ it "handles sensitive characters" do
19
+ query = "*:hello &(world)!|"
20
+ expect(Helper.format_query_for_fulltext(query)).to eq("hello:* & world:*")
21
+ end
22
+
23
+ it "adds a filter to a activerecord-style scope" do
24
+ scope = stub("scope").as_null_object
25
+
26
+ scope.should_receive(:where).with("to_tsvector('simple', name::text) @@ to_tsquery('simple', ?)", ["hello:* & world:*"])
27
+
28
+ Helper.search(scope, "name", "hello world")
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: postgres-fulltext-search-helper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Justin Fitzsimmons
9
+ - Arturo Pie
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-06-27 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
31
+ description: Use postgres' fulltext search with ease
32
+ email:
33
+ - justinf@nulogy.com
34
+ - arturop@nulogy.com
35
+ executables: []
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - .gitignore
40
+ - Gemfile
41
+ - LICENSE.txt
42
+ - README.md
43
+ - Rakefile
44
+ - lib/postgres-fulltext-search-helper.rb
45
+ - lib/postgres-fulltext-search-helper/version.rb
46
+ - postgres-fulltext-search-helper.gemspec
47
+ - spec/postgres-fulltext-search-helper_spec.rb
48
+ homepage: ''
49
+ licenses: []
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 1.8.24
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: ''
72
+ test_files:
73
+ - spec/postgres-fulltext-search-helper_spec.rb