Skip to content

Commit

Permalink
add full_text search column
Browse files Browse the repository at this point in the history
  • Loading branch information
uzulla committed Nov 9, 2013
1 parent b88c71b commit 5373b67
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 12 deletions.
1 change: 1 addition & 0 deletions db/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CREATE TABLE IF NOT EXISTS `post` (
`user_key` varchar(64) NOT NULL,
`profile_image_url` varchar(128) NOT NULL,
`profile_url` varchar(128) NOT NULL,
`full_text` text NOT NULL,
`plusplus` int(10) NOT NULL DEFAULT 0,
`created_at_ms` bigint(20) unsigned NOT NULL,
PRIMARY KEY (`id`)
Expand Down
1 change: 1 addition & 0 deletions db/init_mysql55.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CREATE TABLE IF NOT EXISTS `post` (
`profile_image_url` varchar(128) NOT NULL,
`profile_url` varchar(128) NOT NULL,
`plusplus` int(10) NOT NULL DEFAULT 0,
`full_text` text NOT NULL,
`created_at_ms` bigint(20) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=1 ;
Expand Down
1 change: 1 addition & 0 deletions db/init_sqlite.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ CREATE TABLE post (
profile_image_url varchar(128) NOT NULL,
profile_url varchar(128) NOT NULL,
plusplus int(10) NOT NULL DEFAULT 0,
full_text text NOT NULL,
created_at_ms bigint(20) NOT NULL
);

Expand Down
4 changes: 3 additions & 1 deletion lib/Yancha/DataStorage.pm
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ sub make_post {
my ( $self, $data ) = @_;
my $user = $data->{ user };

my $full_text = ($user->{nickname} // '')." ".(join(' ', @{$data->{ tags }}) // '')." ".($data->{ text } // '');
return {
text => $data->{ text },
nickname => $user ? $user->{ nickname } : $data->{ nickname },
user_key => $user ? $user->{ user_key } : $data->{ user_key },
profile_image_url => $user ? $user->{ profile_image_url } : $data->{ profile_image_url },
profile_url => $user ? $user->{ profile_url } : $data->{ profile_url },
tags => exists $data->{ tags} ? $data->{ tags } : [],
tags => exists $data->{ tags } ? $data->{ tags } : [],
full_text => $full_text,
plusplus => exists $data->{ plusplus } ? $data->{ plusplus } : 0,
created_at_ms => exists $data->{ created_at_ms }
? $data->{ created_at_ms } : $self->_get_now_micro_sec(),
Expand Down
19 changes: 8 additions & 11 deletions lib/Yancha/DataStorage/DBI.pm
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ sub add_post {

my ($sql, @binds) = $self->{sql_maker}->insert('post', [
(map { $_ => $post->{$_} }
qw/user_key nickname profile_image_url profile_url text tags created_at_ms/),
qw/user_key nickname profile_image_url profile_url text tags full_text created_at_ms/),
]);
$self->dbh->do($sql, {}, @binds);

Expand Down Expand Up @@ -328,17 +328,12 @@ sub search_post {
$where_tag->add('tags', [ map { { 'like' => '% ' . uc($_) . ' %' } } @$tags ]);
}

my $where_text_and_nick;
my $where_full_text;
if ( exists $params->{ text } ) {
my $where_text = $maker->new_condition;
$where_full_text = $maker->new_condition;
my $keywords = $params->{ text };
$keywords = ref $keywords ? $keywords : [ $keywords ];
$where_text->add( 'text', [ '-and', map { { 'like', => '%' . $_ . '%' } } grep { $_ ne '' } @$keywords ] );

my $where_nick = $maker->new_condition;
$where_nick->add( 'nickname', [ '-and', map { { 'like', => '%' . $_ . '%' } } grep { $_ ne '' } @$keywords ] );

$where_text_and_nick = $where_text | $where_nick;
$where_full_text->add( 'full_text', [ '-and', map { { 'like', => '%' . $_ . '%' } } grep { $_ ne '' } @$keywords ] );
}

my $where_time;
Expand Down Expand Up @@ -375,7 +370,7 @@ sub search_post {
$older_than_id->add( 'id', { '<' => $params->{ older_than_id } });
}

for ( $where_id, $where_tag, $where_text_and_nick, $where_time, $older_than_id ) {
for ( $where_id, $where_tag, $where_full_text, $where_time, $older_than_id ) {
if ( !$where and $_ ) {
$where = $_;
next;
Expand All @@ -385,7 +380,9 @@ sub search_post {
}

my ( $sql, @binds ) = $maker->select( 'post', ['*'], $where, $attr );

# use Data::Dumper;
#warn Dumper($sql);
#warn Dumper(@binds);
#print STDERR $sql,"\n";
#print STDERR Data::Dumper::Dumper(\@binds);
my $sth = $self->dbh->prepare( $sql );
Expand Down

1 comment on commit 5373b67

@uzulla
Copy link
Owner Author

@uzulla uzulla commented on 5373b67 Nov 22, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DB変換のメモを残す。

id||"\n"||text||"\n"||tag||"\n"||nickname

alter table post add full_text text NOT NULL ;

sqlite update
update post set full_text = id||" "||text||" "||tags||" "||nickname ;

mysql update
update post set full_text = concat(id, " ", tags, " ", text, "", nickname) ;

Please sign in to comment.