server: improve part search
This commit is contained in:
parent
2e066b09a9
commit
43c9abd92f
29
server.rb
29
server.rb
|
@ -142,14 +142,35 @@ end
|
|||
get '/search/:terms' do
|
||||
terms = params['terms'].split(" ")
|
||||
terms.keep_if {|term| term.length >= 3}
|
||||
ids = Set.new
|
||||
part_statement = @db.prepare("SELECT id FROM part WHERE name LIKE ?")
|
||||
halt 404 if terms.empty?
|
||||
# search in names, description, and category
|
||||
statements = []
|
||||
statements << @db.prepare("SELECT id FROM part WHERE name LIKE ?")
|
||||
statements << @db.prepare("SELECT id FROM part WHERE description LIKE ?")
|
||||
statements << @db.prepare("SELECT property_value.part AS id FROM property_value JOIN property ON property.id = property_value.property WHERE property.name = 'category' AND property_value.value LIKE ?")
|
||||
term_ids = []
|
||||
terms.each do |term|
|
||||
part_statement.execute("%#{term}%").each do |row|
|
||||
ids = Set.new
|
||||
# OR term location
|
||||
statements.each do |statement|
|
||||
statement.execute("%#{term}%").each do |row|
|
||||
ids << row["id"]
|
||||
end
|
||||
end
|
||||
puts ids
|
||||
term_ids << ids
|
||||
end
|
||||
# get all children
|
||||
statement = @db.prepare("SELECT id FROM part WHERE family IN (?)")
|
||||
term_ids.each do |term_id|
|
||||
statement.execute(term_id.to_a * ",").each do |row|
|
||||
term_id << row["id"]
|
||||
end
|
||||
end
|
||||
# AND terms
|
||||
ids = term_ids.shift
|
||||
term_ids.each do |term_id|
|
||||
ids &= term_id
|
||||
end
|
||||
parts = ids.collect {|id| get_part_by_id(id)}
|
||||
parts.compact!
|
||||
parts = parts[0, PARTS_LIMIT]
|
||||
|
|
Loading…
Reference in New Issue