autocomplete: make number of results configurable (partly).
Add `Danbooru.Autocomplete.MAX_RESULTS` param that controls the number of results returned by autocomplete. This does not work with tag autocomplete for Builders. Builder autocomplete still needs to be refactored and unified with Member autocomplete. Also fix a bug in the /saved_searches/labels endpoint where the limit param wasn't respected.
This commit is contained in:
@@ -895,12 +895,12 @@ class Tag < ApplicationRecord
|
||||
q
|
||||
end
|
||||
|
||||
def names_matches_with_aliases(name)
|
||||
def names_matches_with_aliases(name, limit: 10)
|
||||
name = normalize_name(name)
|
||||
wildcard_name = name + '*'
|
||||
|
||||
query1 = Tag.select("tags.name, tags.post_count, tags.category, null AS antecedent_name")
|
||||
.search(:name_matches => wildcard_name, :order => "count").limit(10)
|
||||
.search(:name_matches => wildcard_name, :order => "count").limit(limit)
|
||||
|
||||
query2 = TagAlias.select("tags.name, tags.post_count, tags.category, tag_aliases.antecedent_name")
|
||||
.joins("INNER JOIN tags ON tags.name = tag_aliases.consequent_name")
|
||||
@@ -909,13 +909,13 @@ class Tag < ApplicationRecord
|
||||
.where("tags.name NOT LIKE ? ESCAPE E'\\\\'", wildcard_name.to_escaped_for_sql_like)
|
||||
.where("tag_aliases.post_count > 0")
|
||||
.order("tag_aliases.post_count desc")
|
||||
.limit(20) # Get 20 records even though only 10 will be displayed in case some duplicates get filtered out.
|
||||
.limit(limit * 2) # Get extra records in case some duplicates get filtered out.
|
||||
|
||||
sql_query = "((#{query1.to_sql}) UNION ALL (#{query2.to_sql})) AS unioned_query"
|
||||
tags = Tag.select("DISTINCT ON (name, post_count) *").from(sql_query).order("post_count desc").limit(10)
|
||||
tags = Tag.select("DISTINCT ON (name, post_count) *").from(sql_query).order("post_count desc").limit(limit)
|
||||
|
||||
if tags.empty?
|
||||
tags = Tag.select("tags.name, tags.post_count, tags.category, null AS antecedent_name").fuzzy_name_matches(name).order_similarity(name).nonempty.limit(10)
|
||||
tags = Tag.select("tags.name, tags.post_count, tags.category, null AS antecedent_name").fuzzy_name_matches(name).order_similarity(name).nonempty.limit(limit)
|
||||
end
|
||||
|
||||
tags
|
||||
|
||||
Reference in New Issue
Block a user