| | 7 | |
|---|
| | 8 | # Returns an array of strings with the matches highlighted. The +query+ can |
|---|
| | 9 | # either a query String or a Ferret::Search::Query object. |
|---|
| | 10 | # |
|---|
| | 11 | # === Options |
|---|
| | 12 | # |
|---|
| | 13 | # field:: field to take the content from. This field has |
|---|
| | 14 | # to have it's content stored in the index |
|---|
| | 15 | # (:store => :yes in your call to aaf). If not |
|---|
| | 16 | # given, all stored fields are searched, and the |
|---|
| | 17 | # highlighted content found in all of them is returned. |
|---|
| | 18 | # set :highlight => :no in the field options to |
|---|
| | 19 | # avoid highlighting of contents from a :stored field. |
|---|
| | 20 | # excerpt_length:: Default: 150. Length of excerpt to show. Highlighted |
|---|
| | 21 | # terms will be in the centre of the excerpt. |
|---|
| | 22 | # num_excerpts:: Default: 2. Number of excerpts to return. |
|---|
| | 23 | # pre_tag:: Default: "<em>". Tag to place to the left of the |
|---|
| | 24 | # match. |
|---|
| | 25 | # post_tag:: Default: "</em>". This tag should close the |
|---|
| | 26 | # +:pre_tag+. |
|---|
| | 27 | # ellipsis:: Default: "...". This is the string that is appended |
|---|
| | 28 | # at the beginning and end of excerpts (unless the |
|---|
| | 29 | # excerpt hits the start or end of the field. You'll |
|---|
| | 30 | # probably want to change this so a Unicode elipsis |
|---|
| | 31 | # character. |
|---|
| | 32 | def highlight(query, options = {}) |
|---|
| | 33 | options = { :num_excerpts => 2, :pre_tag => '<em>', :post_tag => '</em>' }.update(options) |
|---|
| | 34 | i = self.class.ferret_index |
|---|
| | 35 | highlights = [] |
|---|
| | 36 | i.synchronize do |
|---|
| | 37 | doc_num = self.document_number |
|---|
| | 38 | if options[:field] |
|---|
| | 39 | highlights << i.highlight(query, doc_num, options) |
|---|
| | 40 | else |
|---|
| | 41 | fields_for_ferret.each_pair do |field, config| |
|---|
| | 42 | next if config[:store] == :no || config[:highlight] == :no |
|---|
| | 43 | options[:field] = field |
|---|
| | 44 | highlights << i.highlight(query, doc_num, options) |
|---|
| | 45 | end |
|---|
| | 46 | end |
|---|
| | 47 | end |
|---|
| | 48 | return highlights.compact.flatten[0..options[:num_excerpts]-1] |
|---|
| | 49 | end |
|---|
| 55 | | query = Ferret::Search::TermQuery.new(:id, self.id.to_s) |
|---|
| 56 | | if self.class.configuration[:single_index] |
|---|
| 57 | | bq = Ferret::Search::BooleanQuery.new |
|---|
| 58 | | bq.add_query(query, :must) |
|---|
| 59 | | bq.add_query(Ferret::Search::TermQuery.new(:class_name, self.class.name), :must) |
|---|
| 60 | | query = bq |
|---|
| 61 | | end |
|---|
| 62 | | self.class.ferret_index.query_delete(query) |
|---|
| | 99 | self.class.ferret_index.query_delete(query_for_self) |
|---|
| | 126 | |
|---|
| | 127 | # returns the ferret document number this record has. |
|---|
| | 128 | def document_number |
|---|
| | 129 | hits = self.class.ferret_index.search(query_for_self) |
|---|
| | 130 | return hits.hits.first.doc if hits.total_hits == 1 |
|---|
| | 131 | raise "cannot determine document number from primary key: #{self}" |
|---|
| | 132 | end |
|---|
| | 133 | |
|---|
| | 134 | protected |
|---|
| | 135 | |
|---|
| | 136 | # build a ferret query matching only this record |
|---|
| | 137 | def query_for_self |
|---|
| | 138 | query = Ferret::Search::TermQuery.new(:id, self.id.to_s) |
|---|
| | 139 | if self.class.configuration[:single_index] |
|---|
| | 140 | bq = Ferret::Search::BooleanQuery.new |
|---|
| | 141 | bq.add_query(query, :must) |
|---|
| | 142 | bq.add_query(Ferret::Search::TermQuery.new(:class_name, self.class.name), :must) |
|---|
| | 143 | return bq |
|---|
| | 144 | end |
|---|
| | 145 | return query |
|---|
| | 146 | end |
|---|
| | 147 | |
|---|