make-text-search 0.1 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +37 -2
- data/lib/make-text-search.rb +1 -2
- data/lib/make-text-search/adapters/postgresql_ts.rb +18 -3
- data/lib/make-text-search/models.rb +1 -1
- metadata +4 -4
- data/lib/make-text-search/query.rb +0 -8
data/README.rdoc
CHANGED
@@ -45,14 +45,13 @@ The content added to the index can be filtered. Right now there are two filters:
|
|
45
45
|
|
46
46
|
class Post < ActiveRecord::Base
|
47
47
|
has_text_search :title, :filter => :substrings
|
48
|
-
has_text_search :content, :intro :filter => [:strip_html, :substrings]
|
48
|
+
has_text_search :content, :intro, :filter => [:strip_html, :substrings]
|
49
49
|
end
|
50
50
|
|
51
51
|
You can use several filters using an array. The order is important. If you use both +:substrings+ and +:strip_html+, +:strip_html+ should be the first.
|
52
52
|
|
53
53
|
+:substrings+ let you search inside the words. For example, the word +knowledge+ can be found with +owled+ if you filter the content with +:substrings+.
|
54
54
|
|
55
|
-
+:strip_html+ elimina las etiquetas HTML y traduce las entities a su versión en UTF-8. Por ejemplo
|
56
55
|
+:strip_html+ removes the HTML tags and it translates HTML entities to its equivalent in UTF-8:
|
57
56
|
|
58
57
|
Ir a <a href="http://www.google.es">Google España</a>
|
@@ -61,6 +60,35 @@ will be
|
|
61
60
|
|
62
61
|
Ir a Google España
|
63
62
|
|
63
|
+
=== Language
|
64
|
+
|
65
|
+
The documents can be parsed using a language. You can set the default language with +config.make_text_search.default_language+. The initial value is nil, which means that the documents are parsed in a agnostic way.
|
66
|
+
|
67
|
+
If you want to set the default language add this line to the +config/application.rb+ file.
|
68
|
+
|
69
|
+
config.make_text_search.default_language = "spanish"
|
70
|
+
|
71
|
+
If you want to have a different language for every record you have to implement the +text_search_language+ instance method. For example
|
72
|
+
|
73
|
+
class Post < ActiveRecord::Base
|
74
|
+
has_text_search :title, :filter => :substrings
|
75
|
+
has_text_search :content, :intro, :filter => [:strip_html, :substrings]
|
76
|
+
|
77
|
+
def text_search_language
|
78
|
+
case locale
|
79
|
+
when "es" "spanish"
|
80
|
+
when "en" "english"
|
81
|
+
when "de" "german"
|
82
|
+
when "it" "italian"
|
83
|
+
else
|
84
|
+
Rails.application.config.make_text_search.default_language
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
You can get the available languages of your PostgreSQL server with
|
90
|
+
|
91
|
+
select * from pg_ts_dict;
|
64
92
|
|
65
93
|
== Search
|
66
94
|
|
@@ -71,6 +99,13 @@ To perform search you have to use the scope #search_text
|
|
71
99
|
|
72
100
|
Post.published.search_text("foo & bar").paginate(:page => params[:page])
|
73
101
|
|
102
|
+
The query language is the same used by PostgreSQL. See http://www.postgresql.org/docs/8.4/static/datatype-textsearch.html#DATATYPE-TSQUERY
|
103
|
+
|
104
|
+
= Resources
|
105
|
+
|
106
|
+
* http://www.slideshare.net/billkarwin/full-text-search-in-postgresql
|
107
|
+
* http://www.sai.msu.su/~megera/postgres/fts/fts.pdf
|
108
|
+
|
74
109
|
== TODO
|
75
110
|
|
76
111
|
* Query builder. Add & and | operators
|
data/lib/make-text-search.rb
CHANGED
@@ -2,7 +2,6 @@ module MakeTextSearch
|
|
2
2
|
|
3
3
|
ActiveSupport.on_load(:active_record) do
|
4
4
|
require 'make-text-search/models'
|
5
|
-
require 'make-text-search/query'
|
6
5
|
require 'make-text-search/schema'
|
7
6
|
require 'make-text-search/filters'
|
8
7
|
|
@@ -14,7 +13,7 @@ module MakeTextSearch
|
|
14
13
|
class Railtie < ::Rails::Railtie
|
15
14
|
config.make_text_search = ActiveSupport::OrderedOptions.new
|
16
15
|
config.make_text_search.table_name = "text_search_documents"
|
17
|
-
config.make_text_search.default_language =
|
16
|
+
config.make_text_search.default_language = nil
|
18
17
|
|
19
18
|
generators do
|
20
19
|
load "generators/migration.rb"
|
@@ -38,15 +38,17 @@ module MakeTextSearch
|
|
38
38
|
table_name = Rails.application.config.make_text_search.table_name
|
39
39
|
record_type = quote record_class.name
|
40
40
|
record_id = quote record.id
|
41
|
-
language =
|
41
|
+
language = record.text_search_language
|
42
42
|
|
43
|
-
|
43
|
+
# If language is nil the tsvector will be generated without language
|
44
|
+
document = "to_tsvector(#{language ? quote(language.to_s) + ", " : ""}#{quote record.text_search_build_document})"
|
44
45
|
|
46
|
+
# Reduce the number of operations in the index using SELECT+UPDATE instead of DELETE+INSERT
|
45
47
|
if connection.select_value("SELECT count(*) FROM #{table_name} WHERE #{_where_record record}").to_i == 0
|
46
48
|
connection.insert(%[INSERT INTO #{table_name}
|
47
49
|
(record_type, record_id, language, document)
|
48
50
|
VALUES
|
49
|
-
(#{record_type}, #{record_id}, #{language}, #{document})])
|
51
|
+
(#{record_type}, #{record_id}, #{quote language}, #{document})])
|
50
52
|
else
|
51
53
|
connection.update(%[UPDATE #{table_name} SET document = #{document} WHERE #{_where_record record}])
|
52
54
|
end
|
@@ -60,6 +62,19 @@ module MakeTextSearch
|
|
60
62
|
"record_type = #{quote record.class.name} AND record_id = #{quote record.id}"
|
61
63
|
end
|
62
64
|
|
65
|
+
|
66
|
+
# Query actions
|
67
|
+
def scope_search_text(model, query, language = Rails.application.config.make_text_search.default_language)
|
68
|
+
db_connection = model.connection
|
69
|
+
|
70
|
+
if language
|
71
|
+
query = "to_tsquery(#{db_connection.quote language}, #{db_connection.quote query})"
|
72
|
+
else
|
73
|
+
query = "to_tsquery(#{db_connection.quote query})"
|
74
|
+
end
|
75
|
+
model.where %[#{model.table_name}.id IN (SELECT record_id FROM #{Rails.application.config.make_text_search.table_name} WHERE record_type = #{db_connection.quote model.name} AND document @@ #{query})]
|
76
|
+
end
|
77
|
+
|
63
78
|
end
|
64
79
|
end
|
65
80
|
end
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: make-text-search
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Ayose Cazorla
|
@@ -30,10 +31,9 @@ files:
|
|
30
31
|
- lib/make-text-search.rb
|
31
32
|
- lib/generators/templates/migration.rb
|
32
33
|
- lib/generators/migration.rb
|
33
|
-
- lib/make-text-search/query.rb
|
34
34
|
- lib/make-text-search/html_entities.dat
|
35
|
-
- lib/make-text-search/models.rb
|
36
35
|
- lib/make-text-search/filters.rb
|
36
|
+
- lib/make-text-search/models.rb
|
37
37
|
- lib/make-text-search/adapters/postgresql_ts.rb
|
38
38
|
- lib/make-text-search/schema.rb
|
39
39
|
- test/schema_test.rb
|
@@ -1,8 +0,0 @@
|
|
1
|
-
class <<MakeTextSearch
|
2
|
-
def build_condition(model, query)
|
3
|
-
# TODO use has_text_search_for_postgresql? to implement all the backends
|
4
|
-
|
5
|
-
db_connection = model.connection
|
6
|
-
%[#{model.table_name}.id IN (SELECT record_id FROM #{Rails.application.config.make_text_search.table_name} WHERE record_type = #{db_connection.quote model.name} AND document @@ to_tsquery(#{db_connection.quote query}))]
|
7
|
-
end
|
8
|
-
end
|