OR 検索と NOT 検索(#68) (#286)
Merge remote-tracking branch 'origin/main' into feature/068 #68 テスト・ケース作成 #68 #68 まづは NOT に対応 Co-authored-by: miteruzo <miteruzo@naver.com> Reviewed-on: #286
This commit was merged in pull request #286.
This commit is contained in:
@@ -245,19 +245,41 @@ class PostsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def filter_posts_by_tags tag_names, match_type
|
def filter_posts_by_tags tag_names, match_type
|
||||||
tag_names = TagName.canonicalise(tag_names)
|
literals = tag_names.map do |raw_name|
|
||||||
|
{ name: TagName.canonicalise(raw_name.sub(/\Anot:/i, '')).first,
|
||||||
|
negative: raw_name.downcase.start_with?('not:') }
|
||||||
|
end
|
||||||
|
|
||||||
posts = Post.joins(tags: :tag_name)
|
return Post.all if literals.empty?
|
||||||
|
|
||||||
if match_type == 'any'
|
if match_type == 'any'
|
||||||
posts.where(tag_names: { name: tag_names }).distinct
|
literals.reduce(Post.none) do |posts, literal|
|
||||||
|
posts.or(tag_literal_relation(literal[:name], negative: literal[:negative]))
|
||||||
|
end
|
||||||
else
|
else
|
||||||
posts.where(tag_names: { name: tag_names })
|
literals.reduce(Post.all) do |posts, literal|
|
||||||
.group('posts.id')
|
ids = tagged_post_ids_for(literal[:name])
|
||||||
.having('COUNT(DISTINCT tag_names.id) = ?', tag_names.uniq.size)
|
if literal[:negative]
|
||||||
|
posts.where.not(id: ids)
|
||||||
|
else
|
||||||
|
posts.where(id: ids)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def tag_literal_relation name, negative:
|
||||||
|
ids = tagged_post_ids_for(name)
|
||||||
|
if negative
|
||||||
|
Post.where.not(id: ids)
|
||||||
|
else
|
||||||
|
Post.where(id: ids)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def tagged_post_ids_for(name) =
|
||||||
|
Post.joins(tags: :tag_name).where(tag_names: { name: }).select(:id)
|
||||||
|
|
||||||
def sync_post_tags! post, desired_tags
|
def sync_post_tags! post, desired_tags
|
||||||
desired_tags.each do |t|
|
desired_tags.each do |t|
|
||||||
t.save! if t.new_record?
|
t.save! if t.new_record?
|
||||||
|
|||||||
@@ -17,8 +17,7 @@ class TagsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def autocomplete
|
def autocomplete
|
||||||
q = params[:q].to_s.strip
|
q = params[:q].to_s.strip.sub(/\Anot:/i, '')
|
||||||
return render json: [] if q.blank?
|
|
||||||
|
|
||||||
with_nico = bool?(:nico, default: true)
|
with_nico = bool?(:nico, default: true)
|
||||||
present_only = bool?(:present, default: true)
|
present_only = bool?(:present, default: true)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ RSpec.describe 'Posts API', type: :request do
|
|||||||
end
|
end
|
||||||
|
|
||||||
let!(:tag_name) { TagName.create!(name: 'spec_tag') }
|
let!(:tag_name) { TagName.create!(name: 'spec_tag') }
|
||||||
let!(:tag) { Tag.create!(tag_name: tag_name, category: 'general') }
|
let!(:tag) { Tag.create!(tag_name: tag_name, category: :general) }
|
||||||
|
|
||||||
let!(:post_record) do
|
let!(:post_record) do
|
||||||
Post.create!(title: 'spec post', url: 'https://example.com/spec').tap do |p|
|
Post.create!(title: 'spec post', url: 'https://example.com/spec').tap do |p|
|
||||||
@@ -28,9 +28,9 @@ RSpec.describe 'Posts API', type: :request do
|
|||||||
let!(:user) { create_member_user! }
|
let!(:user) { create_member_user! }
|
||||||
|
|
||||||
let!(:tag_name) { TagName.create!(name: "spec_tag") }
|
let!(:tag_name) { TagName.create!(name: "spec_tag") }
|
||||||
let!(:tag) { Tag.create!(tag_name:, category: "general") }
|
let!(:tag) { Tag.create!(tag_name:, category: :general) }
|
||||||
let!(:tag_name2) { TagName.create!(name: 'unko') }
|
let!(:tag_name2) { TagName.create!(name: 'unko') }
|
||||||
let!(:tag2) { Tag.create!(tag_name: tag_name2, category: 'deerjikist') }
|
let!(:tag2) { Tag.create!(tag_name: tag_name2, category: :deerjikist) }
|
||||||
let!(:alias_tag_name) { TagName.create!(name: 'manko', canonical: tag_name) }
|
let!(:alias_tag_name) { TagName.create!(name: 'manko', canonical: tag_name) }
|
||||||
|
|
||||||
let!(:hit_post) do
|
let!(:hit_post) do
|
||||||
@@ -116,6 +116,123 @@ RSpec.describe 'Posts API', type: :request do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'when tags contain not:' do
|
||||||
|
let!(:foo_tag_name) { TagName.create!(name: 'not_spec_foo') }
|
||||||
|
let!(:foo_tag) { Tag.create!(tag_name: foo_tag_name, category: :general) }
|
||||||
|
|
||||||
|
let!(:bar_tag_name) { TagName.create!(name: 'not_spec_bar') }
|
||||||
|
let!(:bar_tag) { Tag.create!(tag_name: bar_tag_name, category: :general) }
|
||||||
|
|
||||||
|
let!(:baz_tag_name) { TagName.create!(name: 'not_spec_baz') }
|
||||||
|
let!(:baz_tag) { Tag.create!(tag_name: baz_tag_name, category: :general) }
|
||||||
|
|
||||||
|
let!(:foo_alias_tag_name) do
|
||||||
|
TagName.create!(name: 'not_spec_foo_alias', canonical: foo_tag_name)
|
||||||
|
end
|
||||||
|
|
||||||
|
let!(:foo_only_post) do
|
||||||
|
Post.create!(uploaded_user: user, title: 'foo only',
|
||||||
|
url: 'https://example.com/not-spec-foo').tap do |p|
|
||||||
|
PostTag.create!(post: p, tag: foo_tag)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
let!(:bar_only_post) do
|
||||||
|
Post.create!(uploaded_user: user, title: 'bar only',
|
||||||
|
url: 'https://example.com/not-spec-bar').tap do |p|
|
||||||
|
PostTag.create!(post: p, tag: bar_tag)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
let!(:baz_only_post) do
|
||||||
|
Post.create!(uploaded_user: user, title: 'baz only',
|
||||||
|
url: 'https://example.com/not-spec-baz').tap do |p|
|
||||||
|
PostTag.create!(post: p, tag: baz_tag)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
let!(:foo_bar_post) do
|
||||||
|
Post.create!(uploaded_user: user, title: 'foo bar',
|
||||||
|
url: 'https://example.com/not-spec-foo-bar').tap do |p|
|
||||||
|
PostTag.create!(post: p, tag: foo_tag)
|
||||||
|
PostTag.create!(post: p, tag: bar_tag)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
let!(:foo_baz_post) do
|
||||||
|
Post.create!(uploaded_user: user, title: 'foo baz',
|
||||||
|
url: 'https://example.com/not-spec-foo-baz').tap do |p|
|
||||||
|
PostTag.create!(post: p, tag: foo_tag)
|
||||||
|
PostTag.create!(post: p, tag: baz_tag)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:controlled_ids) do
|
||||||
|
[foo_only_post.id, bar_only_post.id, baz_only_post.id,
|
||||||
|
foo_bar_post.id, foo_baz_post.id]
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'supports not search' do
|
||||||
|
get '/posts', params: { tags: 'not:not_spec_foo' }
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
|
||||||
|
expect(json.fetch('posts').map { |p| p['id'] } & controlled_ids).to match_array(
|
||||||
|
[bar_only_post.id, baz_only_post.id]
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'supports alias in not search' do
|
||||||
|
get '/posts', params: { tags: 'not:not_spec_foo_alias' }
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
|
||||||
|
expect(json.fetch('posts').map { |p| p['id'] } & controlled_ids).to match_array(
|
||||||
|
[bar_only_post.id, baz_only_post.id]
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'treats multiple not terms as AND when match is omitted' do
|
||||||
|
get '/posts', params: { tags: 'not:not_spec_foo not:not_spec_bar' }
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
|
||||||
|
expect(json.fetch('posts').map { |p| p['id'] } & controlled_ids).to match_array(
|
||||||
|
[baz_only_post.id]
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'treats multiple not terms as OR when match=any' do
|
||||||
|
get '/posts', params: { tags: 'not:not_spec_foo not:not_spec_bar', match: 'any' }
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
|
||||||
|
expect(json.fetch('posts').map { |p| p['id'] } & controlled_ids).to match_array(
|
||||||
|
[foo_only_post.id, bar_only_post.id, baz_only_post.id, foo_baz_post.id]
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'supports mixed positive and negative search with AND' do
|
||||||
|
get '/posts', params: { tags: 'not_spec_foo not:not_spec_bar' }
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
|
||||||
|
expect(json.fetch('posts').map { |p| p['id'] } & controlled_ids).to match_array(
|
||||||
|
[foo_only_post.id, foo_baz_post.id]
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'supports mixed positive and negative search with OR when match=any' do
|
||||||
|
get '/posts', params: { tags: 'not_spec_foo not:not_spec_bar', match: 'any' }
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
|
||||||
|
expect(json.fetch('posts').map { |p| p['id'] } & controlled_ids).to match_array(
|
||||||
|
[foo_only_post.id, baz_only_post.id, foo_bar_post.id, foo_baz_post.id]
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when url is provided' do
|
context 'when url is provided' do
|
||||||
let!(:url_hit_post) do
|
let!(:url_hit_post) do
|
||||||
Post.create!(uploaded_user: user, title: 'url hit',
|
Post.create!(uploaded_user: user, title: 'url hit',
|
||||||
@@ -409,7 +526,7 @@ RSpec.describe 'Posts API', type: :request do
|
|||||||
context "when nico tag already exists in tags" do
|
context "when nico tag already exists in tags" do
|
||||||
before do
|
before do
|
||||||
Tag.find_or_create_by!(tag_name: TagName.find_or_create_by!(name: 'nico:nico_tag'),
|
Tag.find_or_create_by!(tag_name: TagName.find_or_create_by!(name: 'nico:nico_tag'),
|
||||||
category: 'nico')
|
category: :nico)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'return 400' do
|
it 'return 400' do
|
||||||
@@ -475,7 +592,7 @@ RSpec.describe 'Posts API', type: :request do
|
|||||||
|
|
||||||
# 追加で別タグも作って、更新時に入れ替わることを見る
|
# 追加で別タグも作って、更新時に入れ替わることを見る
|
||||||
tn2 = TagName.create!(name: 'spec_tag_2')
|
tn2 = TagName.create!(name: 'spec_tag_2')
|
||||||
Tag.create!(tag_name: tn2, category: 'general')
|
Tag.create!(tag_name: tn2, category: :general)
|
||||||
|
|
||||||
put "/posts/#{post_record.id}", params: {
|
put "/posts/#{post_record.id}", params: {
|
||||||
title: 'updated title',
|
title: 'updated title',
|
||||||
@@ -494,7 +611,7 @@ RSpec.describe 'Posts API', type: :request do
|
|||||||
context "when nico tag already exists in tags" do
|
context "when nico tag already exists in tags" do
|
||||||
before do
|
before do
|
||||||
Tag.find_or_create_by!(tag_name: TagName.find_or_create_by!(name: 'nico:nico_tag'),
|
Tag.find_or_create_by!(tag_name: TagName.find_or_create_by!(name: 'nico:nico_tag'),
|
||||||
category: 'nico')
|
category: :nico)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'return 400' do
|
it 'return 400' do
|
||||||
@@ -531,7 +648,7 @@ RSpec.describe 'Posts API', type: :request do
|
|||||||
it 'returns add/remove events (history) for a post' do
|
it 'returns add/remove events (history) for a post' do
|
||||||
# add
|
# add
|
||||||
tn2 = TagName.create!(name: 'spec_tag2')
|
tn2 = TagName.create!(name: 'spec_tag2')
|
||||||
tag2 = Tag.create!(tag_name: tn2, category: 'general')
|
tag2 = Tag.create!(tag_name: tn2, category: :general)
|
||||||
pt = PostTag.create!(post: post_record, tag: tag2, created_user: member)
|
pt = PostTag.create!(post: post_record, tag: tag2, created_user: member)
|
||||||
|
|
||||||
# remove (discard)
|
# remove (discard)
|
||||||
|
|||||||
@@ -56,8 +56,11 @@ export default (() => {
|
|||||||
if (activeIndex < 0)
|
if (activeIndex < 0)
|
||||||
break
|
break
|
||||||
ev.preventDefault ()
|
ev.preventDefault ()
|
||||||
const selected = suggestions[activeIndex]
|
{
|
||||||
selected && handleTagSelect (selected)
|
const selected = suggestions[activeIndex]
|
||||||
|
if (selected)
|
||||||
|
handleTagSelect (selected)
|
||||||
|
}
|
||||||
break
|
break
|
||||||
|
|
||||||
case 'Escape':
|
case 'Escape':
|
||||||
@@ -67,14 +70,25 @@ export default (() => {
|
|||||||
}
|
}
|
||||||
if (ev.key === 'Enter' && (!(suggestionsVsbl) || activeIndex < 0))
|
if (ev.key === 'Enter' && (!(suggestionsVsbl) || activeIndex < 0))
|
||||||
{
|
{
|
||||||
navigate (`/posts?${ (new URLSearchParams ({ tags: search })).toString () }`)
|
const parts = search.split (' ')
|
||||||
|
const match = parts.map (t => t.toLowerCase ()).includes ('type:or') ? 'any' : 'all'
|
||||||
|
navigate (`/posts?${
|
||||||
|
(new URLSearchParams ({
|
||||||
|
tags: parts.map (t => t.toLowerCase ())
|
||||||
|
.filter (t => t !== 'type:or')
|
||||||
|
.join (' '),
|
||||||
|
match }))
|
||||||
|
.toString () }`)
|
||||||
setSuggestionsVsbl (false)
|
setSuggestionsVsbl (false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleTagSelect = (tag: Tag) => {
|
const handleTagSelect = (tag: Tag) => {
|
||||||
const parts = search.split (' ')
|
const parts = search.split (' ')
|
||||||
parts[parts.length - 1] = tag.name
|
parts[parts.length - 1] = (
|
||||||
|
(parts[parts.length - 1].slice(0, 4).toLowerCase () === 'not:')
|
||||||
|
? ('not:' + tag.name)
|
||||||
|
: tag.name)
|
||||||
setSearch (parts.join (' ') + ' ')
|
setSearch (parts.join (' ') + ' ')
|
||||||
setSuggestions ([])
|
setSuggestions ([])
|
||||||
setActiveIndex (-1)
|
setActiveIndex (-1)
|
||||||
@@ -82,7 +96,8 @@ export default (() => {
|
|||||||
|
|
||||||
useEffect (() => {
|
useEffect (() => {
|
||||||
const query = new URLSearchParams (location.search)
|
const query = new URLSearchParams (location.search)
|
||||||
const tagsQuery = query.get ('tags') ?? ''
|
const anyFlg = query.get ('match') === 'any'
|
||||||
|
const tagsQuery = `${ query.get ('tags') ?? '' }${ anyFlg ? ' type:or' : '' }`
|
||||||
setSearch (tagsQuery)
|
setSearch (tagsQuery)
|
||||||
}, [location.search])
|
}, [location.search])
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user