genki / acts_as_searchable_with_similarity
- Source
- Commits
- Network (0)
- Issues (0)
- Downloads (0)
- Wiki (1)
- Graphs
-
Tree:
ab585fd
takiuchi (author)
Thu Nov 29 06:01:21 -0800 2007
acts_as_searchable_with_similarity / with_similarity.diff
| ab585fdb » | takiuchi | 2007-11-29 | 1 | diff -r acts_as_searchable/CHANGELOG aas_ws/CHANGELOG | |
| 2 | 19a20,25 | ||||
| 3 | > | ||||
| 4 | > *0.1.1* 29 Nov 2007 (Genki Takiuchi <genki@s21g.com>) | ||||
| 5 | > | ||||
| 6 | > * Added similarity search feature. | ||||
| 7 | > | ||||
| 8 | > Article.similarity_search(target_article, :limit => 5) | ||||
| 9 | Only in acts_as_searchable: install.rb | ||||
| 10 | diff -r acts_as_searchable/lib/acts_as_searchable.rb aas_ws/lib/acts_as_searchable.rb | ||||
| 11 | 203a204,216 | ||||
| 12 | > | ||||
| 13 | > # suppress exception | ||||
| 14 | > return [] if matches.blank? | ||||
| 15 | > conditions = " AND (#{sanitize_sql(find_options[:conditions])})" if | ||||
| 16 | > find_options[:conditions] | ||||
| 17 | > ids = matches.collect { |m| m.attr('db_id') } | ||||
| 18 | > ids_list = ids.map do |id| | ||||
| 19 | > quote_value(id, columns_hash[primary_key]) | ||||
| 20 | > end.join(',') | ||||
| 21 | > find_options.update :conditions => "#{quoted_table_name}." + | ||||
| 22 | > "#{connection.quote_column_name(primary_key)} IN " + | ||||
| 23 | > "(#{ids_list})#{conditions}" | ||||
| 24 | > find_every(find_options) | ||||
| 25 | 205c218,232 | ||||
| 26 | < matches.blank? ? [] : find(matches.collect { |m| m.attr('db_id') }, find_options) | ||||
| 27 | --- | ||||
| 28 | > #matches.blank? ? [] : find(matches.collect { |m| m.attr('db_id') }, find_options) | ||||
| 29 | > end | ||||
| 30 | > | ||||
| 31 | > # Similarity search | ||||
| 32 | > def similarity_search(model, options = {}) | ||||
| 33 | > return [] unless doc = model.estraier_doc | ||||
| 34 | > doc_id = doc.attr('@id') | ||||
| 35 | > keywords = estraier_connection.etch_doc(doc_id) | ||||
| 36 | > config = options.delete(:config) || '16 1024 4096' | ||||
| 37 | > phrase = keywords.inject("[SIMILAR] #{config}") do |r,(k,v)| | ||||
| 38 | > r << " WITH #{v} #{k}" | ||||
| 39 | > end | ||||
| 40 | > options[:attributes] = [options[:attributes]].flatten.compact | ||||
| 41 | > options[:attributes] << "db_id NUMNE #{model.id}" | ||||
| 42 | > fulltext_search phrase, options | ||||
| 43 | 371c398 | ||||
| 44 | < end | ||||
| 45 | \ No newline at end of file | ||||
| 46 | --- | ||||
| 47 | > end | ||||
| 48 | diff -r acts_as_searchable/lib/vendor/estraierpure.rb aas_ws/lib/vendor/estraierpure.rb | ||||
| 49 | 239a240,245 | ||||
| 50 | > # Set the similar id and node URI. | ||||
| 51 | > def set_similar(id, uri) | ||||
| 52 | > @similar = id | ||||
| 53 | > @simnode = uri | ||||
| 54 | > nil | ||||
| 55 | > end | ||||
| 56 | 295a302,309 | ||||
| 57 | > # Get similar id | ||||
| 58 | > def similar() | ||||
| 59 | > @similar | ||||
| 60 | > end | ||||
| 61 | > # Get simnode URI | ||||
| 62 | > def simnode() | ||||
| 63 | > @simnode | ||||
| 64 | > end | ||||
| 65 | 327a342,343 | ||||
| 66 | > @similar = nil | ||||
| 67 | > @simnode = nil | ||||
| 68 | 441a458,461 | ||||
| 69 | > # Get the URL of a node server. | ||||
| 70 | > def get_url | ||||
| 71 | > @url | ||||
| 72 | > end | ||||
| 73 | 986a1007,1011 | ||||
| 74 | > if cond.similar | ||||
| 75 | > buf.write("&") if buf.length > 0 | ||||
| 76 | > buf.write("simid=" + cond.similar.to_s) | ||||
| 77 | > buf.write("&simnode=" + CGI::escape(cond.simnode.to_s)) | ||||
| 78 | > end | ||||
| 79 | Only in acts_as_searchable: test | ||||
