server: improve part search

This commit is contained in:
King Kévin 2023-01-25 11:58:17 +01:00
parent 2e066b09a9
commit 43c9abd92f
1 changed files with 27 additions and 6 deletions

View File

@ -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 ?")
terms.each do |term|
part_statement.execute("%#{term}%").each do |row|
ids << row["id"]
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|
ids = Set.new
# OR term location
statements.each do |statement|
statement.execute("%#{term}%").each do |row|
ids << row["id"]
end
end
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
puts ids
# 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]