Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Most relevant search results #142

Open
IanTrudel opened this issue Feb 5, 2015 · 4 comments
Open

Most relevant search results #142

IanTrudel opened this issue Feb 5, 2015 · 4 comments

Comments

@IanTrudel
Copy link

How would you implement most relevant search results? The follow code is using sort_by with some success but is somehow limited. Trying to use sort to have two elements to compare is difficult because it returns two Allocation containing terms. No idea what to do with those.

Perhaps Picky already has something for that? In any case, I would like to sort results by most relevant, then uri, then body. It would be useful to be able to do some kind of sort { |a, b| ... } to compared two elements.

The code below prioritize the uri containing the term. Could be an interesting example for the manual #140.

require("picky")
require("open-uri")
require("net/https")
require("pp")

Picky.logger = Picky::Loggers::Silent.new

doc = open(
      "https://raw.githubusercontent.com/Shoes3/shoes3/master/static/manual-en.txt", 
      {ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE}
   ).read

Entry = Struct.new :id, :uri, :body

entry = nil
entries = []
doc.each_line { |n|
   if n =~ /^=+ (.*) =+$/
      entry = Entry.new(entries.size, n.strip, "")
      entries << entry
   elsif n.strip.size > 0 and not entry.nil?
      entry.body += n
   end
}

index = Picky::Index.new :terms do
   indexing removes_characters: %r{[^a-z0-9\s\/\-\_\:\"\&\.]}i,
      splits_text_on:     %r{[\s/\-\_\:\"\&/\.]}
   category :uri, :from => lambda { |doc| doc.uri.dup }
   category :body, :from => lambda { |doc| doc.body.dup }
end
search = Picky::Search.new index do
   searching removes_characters: %r{[^a-z0-9\s\/\-\_\:\"\&\.]}i,
      splits_text_on:     %r{[\s/\-\_\:\"\&/\.]}
end

puts "total entries #{entries.size}"
entries.each { |n| index.add n }

term = "image"

retval = []
results = search.search(term, entries.size, 0)
results.sort_by { |id| entries.detect { |n| n.id == id }.uri =~ /#{term}/ ? 0 : id }
results.ids.each do |id|
   entry = entries.detect { |n| n.id == id }
   retval << entry.uri unless entry.nil?
end
puts "total retval #{retval.uniq.size}"
pp retval.uniq
@floere
Copy link
Owner

floere commented Mar 31, 2016

Ugh! I completely missed this… 😭

@backorder I assume it's not relevant anymore?

@IanTrudel
Copy link
Author

@floere any help is always welcomed, even a year or so later! 😃

@floere
Copy link
Owner

floere commented Apr 5, 2016

@backorder It's not a full answer, but for now… Picky primarily does boosting of certain result category combinations. Here are the docs for it: http://florianhanke.com/picky/documentation.html#search-options-boost (in your case, you'd boost one category more than the other).

@IanTrudel
Copy link
Author

Both boost and Picky::Weights::Logarithmic.new(+4) aren't working in the sample above. Individually or together but without the sort_by above. For example, I would like to give greater weight to :uri in such way that when you use a search term, say oval, it would list entries with oval in the :uri first, then those who are not subsequently.

There are only two :uri containing oval. All other oval are in :body.

M Slots: Art: oval(left, top, radius) » Shoes::Shape
M Slots: Art: oval(styles) » Shoes::Shape

The current sort_by method does a relatively close to what is expected and outputs the following:

total entries 423
total retval 24
["=== oval(styles) \u00BB Shoes::Shape ===",
 "=== oval(left, top, radius) \u00BB Shoes::Shape ===",
 "==== Windows ====",
 "==== Image and Shape Blocks ====",
 "=== confirm(question: a string, :title => a string or nil) \u00BB true or false ===",
 "=== gradient(color1, color2) \u00BB Shoes::Pattern ===",
 "=== gray(the numbers: darkness, alpha) \u00BB Shoes::Color ===",
 "=== Shoes.app(styles) { ... } \u00BB Shoes::App ===",
 "=== :angle \u00BB a number ===",
 "=== :center \u00BB true or false ===",
 "=== :click \u00BB a proc ===",
 "=== :fill \u00BB a hex code, a Shoes::Color or a range of either ===",
 "=== :radius \u00BB a number ===",
 "=== :stroke \u00BB a hex code, a Shoes::Color or a range of either ===",
 "=== :strokewidth \u00BB a number ===",
 "== Art for Slots ==",
 "=== arc(left, top, width, height, angle1, angle2) \u00BB Shoes::Shape ===",
 "=== shape(left, top) { ... } \u00BB Shoes::Shape ===",
 "=== motion { |left, top| ... } \u00BB self ===",
 "= Elements =",
 "== Background ==",
 "==== Image effects on image blocks ====",
 "== Shape ==",
 "=== snapshot(:format => a type of image, :filename => a path) {...} \u00BB snapshot raw data ==="]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants