From 0d272f1d6937922ef74d0919e7c06c4ea27f7856 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=86=E3=83=84=E3=83=8E=E3=83=8B=E3=82=B8=E3=82=AB?= <969+codex-bot@noreply.localhost> Date: Mon, 23 Nov 2026 02:45:53 +0900 Subject: [PATCH] =?UTF-8?q?AI:=20#272=20=E3=82=BF=E3=82=B0=E8=A3=9C?= =?UTF-8?q?=E5=AE=8C=EF=BC=8C=E5=89=8D=E6=96=B9=E6=A4=9C=E7=B4=A2=E3=82=92?= =?UTF-8?q?=E5=84=AA=E5=85=88=E8=A1=A8=E7=A4=BA=E3=81=99=E3=82=8B=E3=81=8C?= =?UTF-8?q?=EF=BC=8C=E5=BE=8C=E6=96=B9=E6=A4=9C=E7=B4=A2=E3=82=82=E8=A1=8C?= =?UTF-8?q?=E3=81=B5=E3=82=84=E3=81=85=E3=81=AB=E3=81=99=E3=82=8B=EF=BC=88?= =?UTF-8?q?=E6=9C=AB=E5=B0=BE=E3=81=AB=E8=BF=BD=E8=A8=98=EF=BC=89=20(#458)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refs #272 Branch mode: `new`; base: `main`; head: `feature/272` Implementation model: `gpt-6.1-sol` / reasoning: `medium` ## Automated implementation (requires human review) タグ補完で前方一致を優先し、20件に満たない場合に後方一致を重複なく末尾へ追加しました。正式名・別名・外部タグが対象です。 `git diff --check` は通過しました。指定に従い、テスト・lint・ビルドは未実行です。 ## Verification - Tests/builds/migrations were not authorized and were not invoked by the controller. - Codex was instructed not to run them; inspect its command transcript. - The repository owner alone reviews changes. No AI review is executed. - Do not merge before human acceptance and required checks. --------- Co-authored-by: Codex Night Worker Co-authored-by: miteruzo Co-authored-by: みてるぞ Reviewed-on: https://git.miteruzo.com/miteruzo/btrc-hub/pulls/458 Co-authored-by: テツノニジカ <969+codex-bot@noreply.localhost> --- backend/app/controllers/tags_controller.rb | 114 +++++++++++---------- backend/spec/requests/tags_spec.rb | 51 ++++++++- 2 files changed, 107 insertions(+), 58 deletions(-) diff --git a/backend/app/controllers/tags_controller.rb b/backend/app/controllers/tags_controller.rb index 15ef924..e571219 100644 --- a/backend/app/controllers/tags_controller.rb +++ b/backend/app/controllers/tags_controller.rb @@ -219,63 +219,16 @@ class TagsController < ApplicationController locale = resolve_locale! q = params[:q].to_s.strip.sub(/\Anot:/i, '') - prefix = "#{ ActiveRecord::Base.sanitize_sql_like(q).tr('*?', '%_') }%" + literal = ActiveRecord::Base.sanitize_sql_like(q).tr('*?', '%_') + nico = bool?(:nico, default: true) + present = bool?(:present, default: true) - with_nico = bool?(:nico, default: true) - present_only = bool?(:present, default: true) - - alias_rows = - TagName - .where(language_code: locale.language_code, primary_flg: false) - .where('name LIKE ?', prefix) - .pluck(:tag_id, :name) - - matched_alias_by_tag_id = { } - tag_ids = [] - alias_rows.each do |tag_id, alias_name| - tag_ids << tag_id - matched_alias_by_tag_id[tag_id] ||= alias_name + rows = autocomplete_rows(locale:, pattern: "#{ literal }%", nico:, present:) + if rows.length < 20 && q.present? + partial_rows = autocomplete_rows(locale:, pattern: "%#{ literal }%", nico:, present:) + rows = (rows + partial_rows).uniq { [_1['category'], _1['id']] }.first(20) end - base = - Tag - .joins(:tag_names) - .preload(:tag_names, :materials, :deerjikists, :tag_descriptions, - parents: [:tag_names, :materials, :deerjikists, :tag_descriptions]) - .where(tag_names: { language_code: locale.language_code, primary_flg: true }, - deprecated_at: nil) - base = base.where('tags.post_count > 0') if present_only - - canonical_hit = base.where('tag_names.name LIKE ?', prefix) - - internal_tags = canonical_hit.or(base.where(id: tag_ids.uniq)) - - internal_rows = - internal_tags - .order(Arel.sql('tags.post_count DESC, tag_names.name')) - .limit(20) - .map { |tag| - TagRepr.base(tag, language_code: locale.language_code) - .merge(matched_alias: matched_alias_by_tag_id[tag.id]) - } - - return render json: internal_rows unless with_nico - - external_base = ExternalTag.all - external_base = external_base.where('post_count > 0') if present_only - - external_rows = - external_base - .where("CONCAT(platform, ':', name) LIKE ? OR name LIKE ?", prefix, prefix) - .order(post_count: :desc, name: :asc) - .limit(20) - .map { external_tag_json(_1) } - - rows = - (internal_rows + external_rows) - .sort_by { |row| [-row['post_count'], row['name'] || row[:name]] } - .first(20) - render json: rows end @@ -546,6 +499,59 @@ class TagsController < ApplicationController private + def autocomplete_rows locale:, pattern:, nico:, present: + alias_rows = + TagName + .where(language_code: locale.language_code, primary_flg: false) + .where('name LIKE ?', pattern) + .pluck(:tag_id, :name) + + matched_alias_by_tag_id = { } + tag_ids = [] + alias_rows.each do |tag_id, alias_name| + tag_ids << tag_id + matched_alias_by_tag_id[tag_id] ||= alias_name + end + + base = + Tag + .joins(:tag_names) + .preload(:tag_names, :materials, :deerjikists, :tag_descriptions, + parents: [:tag_names, :materials, :deerjikists, :tag_descriptions]) + .where(tag_names: { language_code: locale.language_code, primary_flg: true }, + deprecated_at: nil) + base = base.where('tags.post_count > 0') if present + + canonical_hit = base.where('tag_names.name LIKE ?', pattern) + + internal_tags = canonical_hit.or(base.where(id: tag_ids.uniq)) + + internal_rows = + internal_tags + .order(Arel.sql('tags.post_count DESC, tag_names.name')) + .limit(20) + .map { |tag| + TagRepr.base(tag, language_code: locale.language_code) + .merge(matched_alias: matched_alias_by_tag_id[tag.id]) + } + + return internal_rows unless nico + + external_base = ExternalTag.all + external_base = external_base.where('post_count > 0') if present + + external_rows = + external_base + .where("CONCAT(platform, ':', name) LIKE ? OR name LIKE ?", pattern, pattern) + .order(post_count: :desc, name: :asc) + .limit(20) + .map { external_tag_json(_1) } + + (internal_rows + external_rows) + .sort_by { |row| [-row['post_count'], row['name'] || row[:name]] } + .first(20) + end + def material_filter_param default: value = params[:material_filter].to_s.presence return default unless ['present', 'missing', 'any'].include?(value) diff --git a/backend/spec/requests/tags_spec.rb b/backend/spec/requests/tags_spec.rb index da16111..423b42d 100644 --- a/backend/spec/requests/tags_spec.rb +++ b/backend/spec/requests/tags_spec.rb @@ -492,9 +492,9 @@ RSpec.describe 'Tags API', type: :request do end { - 'wild*end' => [0, 1, 2], - 'wild?end' => [1], - 'not:wild?end' => [1], + 'wild*end' => [0, 1, 2, 3], + 'wild?end' => [1, 3], + 'not:wild?end' => [1, 3], 'nico:wild?end' => [1] }.each do |query, indices| it "matches primary, alias, and external names with #{ query.inspect }" do get '/tags/autocomplete', params: { q: query } @@ -557,6 +557,7 @@ RSpec.describe 'Tags API', type: :request do create(:tag, category: :general, primary_name: name, post_count: 1) create(:external_tag, name:, post_count: 1) end + create(:external_tag, name: 'suffix_combined', post_count: 100) get '/tags/autocomplete', params: { q: 'combined' } @@ -565,6 +566,40 @@ RSpec.describe 'Tags API', type: :request do expect(json.map { |row| row.fetch('name') }).to eq(expected.first(20)) end + it 'appends primary, alias, and external partial matches without duplicate resources' do + prefix = create(:tag, primary_name: 'tail', post_count: 1) + create(:tag_name, :alias, name: 'tail_alias', tag: prefix) + create(:tag_name, :alias, name: 'alias_tail', tag: prefix) + create(:tag, primary_name: 'primary_tail', post_count: 4) + alias_target = create(:tag, primary_name: 'alias_target', post_count: 3) + create(:tag_name, :alias, name: 'middle_tail_alias', tag: alias_target) + external = create(:external_tag, id: prefix.id, name: 'external_tail', post_count: 5) + create(:tag, primary_name: 'middle_tail_only', post_count: 100) + create(:external_tag, name: 'middle_tail_only', post_count: 100) + + get '/tags/autocomplete', params: { q: 'tail' } + + expect(response).to have_http_status(:ok) + expect(json.map { |row| row.fetch('name') }).to eq( + ['tail', 'middle_tail_only', 'nico:middle_tail_only', + 'nico:external_tail', 'primary_tail', 'alias_target']) + expect(json.first.fetch('matched_alias')).to eq('tail_alias') + expect(json.last.fetch('matched_alias')).to eq('middle_tail_alias') + expect(json[3]).to include('id' => external.id, 'category' => 'nico') + end + + it 'fills only the remaining places with partial matches' do + names = 19.times.map { |number| "capacity_#{ number.to_s.rjust(2, '0') }" } + names.each { create(:tag, primary_name: _1, post_count: 1) } + create(:tag, primary_name: 'first_capacity_match', post_count: 3) + create(:external_tag, name: 'second_capacity_match', post_count: 2) + + get '/tags/autocomplete', params: { q: 'capacity' } + + expect(response).to have_http_status(:ok) + expect(json.map { |row| row.fetch('name') }).to eq(names + ['first_capacity_match']) + end + it 'excludes external tags but preserves internal alias matches when nico is false' do internal = create(:tag, category: :general, primary_name: 'switch_internal', post_count: 1) alias_target = create(:tag, category: :general, primary_name: 'alias_target', post_count: 1) @@ -593,12 +628,20 @@ RSpec.describe 'Tags API', type: :request do create(:external_tag, name: literal, post_count: 1) create(:tag, category: :general, primary_name: 'literalXmatch', post_count: 2) create(:external_tag, name: 'literalXmatch', post_count: 2) + suffix = "suffix_literal#{ wildcard }" + create(:tag, primary_name: suffix, post_count: 1) + suffix_alias = create(:tag, primary_name: 'suffix_alias_target', post_count: 1) + create(:tag_name, :alias, name: "alias_#{ suffix }", tag: suffix_alias) + create(:external_tag, name: suffix, post_count: 1) + create(:tag, primary_name: 'suffix_literalX', post_count: 2) + create(:external_tag, name: 'suffix_literalX', post_count: 2) get '/tags/autocomplete', params: { q: "literal#{ wildcard }" } expect(json.map { |row| row.fetch('name') }) .to contain_exactly( - literal, primary_tag_name_for(alias_target, 'ja').name, "nico:#{ literal }") + literal, primary_tag_name_for(alias_target, 'ja').name, "nico:#{ literal }", + suffix, 'suffix_alias_target', "nico:#{ suffix }") end end