vendor/riddle/lib/riddle/client.rb in ultrasphinx-1.8 vs vendor/riddle/lib/riddle/client.rb in ultrasphinx-1.9
- old
+ new
@@ -55,19 +55,21 @@
}
RankModes = {
:proximity_bm25 => 0, # SPH_RANK_PROXIMITY_BM25
:bm25 => 1, # SPH_RANK_BM25
- :none => 2 # SPH_RANK_NONE
+ :none => 2, # SPH_RANK_NONE
+ :wordcount => 3 # SPH_RANK_WORDCOUNT
}
SortModes = {
:relevance => 0, # SPH_SORT_RELEVANCE
:attr_desc => 1, # SPH_SORT_ATTR_DESC
:attr_asc => 2, # SPH_SORT_ATTR_ASC
:time_segments => 3, # SPH_SORT_TIME_SEGMENTS
- :extended => 4 # SPH_SORT_EXTENDED
+ :extended => 4, # SPH_SORT_EXTENDED
+ :expr => 5 # SPH_SORT_EXPR
}
AttributeTypes = {
:integer => 1, # SPH_ATTR_INTEGER
:timestamp => 2, # SPH_ATTR_TIMESTAMP
@@ -133,21 +135,26 @@
@queue = []
end
# Set the geo-anchor point - with the names of the attributes that contain
- # the latitude and longtitude, and the reference position.
+ # the latitude and longitude (in radians), and the reference position.
+ # Note that for geocoding to work properly, you must also set
+ # match_mode to :extended. To sort results by distance, you will
+ # need to set sort_mode to '@geodist asc' for example. Sphinx
+ # expects latitude and longitude to be returned from you SQL source
+ # in radians.
#
# Example:
- # client.set_anchor('lat', -37.767899, 'lon', 145.002451)
+ # client.set_anchor('lat', -0.6591741, 'long', 2.530770)
#
def set_anchor(lat_attr, lat, long_attr, long)
@anchor = {
:latitude_attribute => lat_attr,
:latitude => lat,
- :longtitude_attribute => long_attr,
- :longtitude => long
+ :longitude_attribute => long_attr,
+ :longitude => long
}
end
# Append a query to the queue. This uses the same parameters as the query
# method.
@@ -271,11 +278,14 @@
def query(search, index = '*')
@queue << query_message(search, index)
self.run.first
end
- # Grab excerpts from the indexes. As part of the options, you will need to
+ # Build excerpts from search terms (the +words+) and the text of documents. Excerpts are bodies of text that have the +words+ highlighted.
+ # They may also be abbreviated to fit within a word limit.
+ #
+ # As part of the options hash, you will need to
# define:
# * :docs
# * :words
# * :index
#
@@ -288,10 +298,35 @@
# * :exact_phrase (defaults to false)
# * :single_passage (defaults to false)
#
# The defaults differ from the official PHP client, as I've opted for
# semantic HTML markup.
+ #
+ # Example:
+ #
+ # client.excerpts(:docs => ["Pat Allan, Pat Cash"], :words => 'Pat', :index => 'pats')
+ # #=> ["<span class=\"match\">Pat</span> Allan, <span class=\"match\">Pat</span> Cash"]
+ #
+ # lorem_lipsum = "Lorem ipsum dolor..."
+ #
+ # client.excerpts(:docs => ["Pat Allan, #{lorem_lipsum} Pat Cash"], :words => 'Pat', :index => 'pats')
+ # #=> ["<span class=\"match\">Pat</span> Allan, Lorem ipsum dolor sit amet, consectetur adipisicing
+ # elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua … . Excepteur
+ # sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est
+ # laborum. <span class=\"match\">Pat</span> Cash"]
+ #
+ # Workflow:
+ #
+ # Excerpt creation is completely isolated from searching the index. The nominated index is only used to
+ # discover encoding and charset information.
+ #
+ # Therefore, the workflow goes:
+ #
+ # 1. Do the sphinx query.
+ # 2. Fetch the documents found by sphinx from their repositories.
+ # 3. Pass the documents' text to +excerpts+ for marking up of matched terms.
+ #
def excerpts(options = {})
options[:index] ||= '*'
options[:before_match] ||= '<span class="match">'
options[:after_match] ||= '</span>'
options[:chunk_separator] ||= ' … ' # ellipsis
@@ -442,12 +477,12 @@
if @anchor.empty?
message.append_int 0
else
message.append_int 1
message.append_string @anchor[:latitude_attribute]
- message.append_string @anchor[:longtitude_attribute]
- message.append_floats @anchor[:latitude], @anchor[:longtitude]
+ message.append_string @anchor[:longitude_attribute]
+ message.append_floats @anchor[:latitude], @anchor[:longitude]
end
# Per Index Weights
message.append_int @index_weights.length
@index_weights.each do |key,val|
@@ -508,6 +543,6 @@
end
message.to_s
end
end
-end
\ No newline at end of file
+end