Skip to content

Commit

Permalink
Improved search
Browse files Browse the repository at this point in the history
  • Loading branch information
wakaba committed Feb 22, 2015
1 parent c658991 commit a7945ef
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
36 changes: 32 additions & 4 deletions lib/SWE/DB/ES.pm
Expand Up @@ -19,9 +19,31 @@ sub basic_auth ($) {
$_[0]->{config}->get_file_base64_text ('es_password')]; $_[0]->{config}->get_file_base64_text ('es_password')];
} # basic_auth } # basic_auth


sub _tc ($) {
my @node = ($_[0]);
my @result;
while (@node) {
my $node = shift @node;
my $ln = $node->local_name;
if (defined $ln) {
if ({map { $_ => 1 } qw(
p dt dd li insert delete h1 td th pre rt comment-p ed nsuri
)}->{$ln}) {
push @result, "\n";
unshift @node, $node->child_nodes->to_list;
} else {
unshift @node, $node->child_nodes->to_list;
}
} elsif ($node->node_type == $node->TEXT_NODE) {
push @result, $node->text_content;
}
}
return join "", @result;
} # _tc

sub update ($$$$) { sub update ($$$$) {
my ($self, $id, $title, $doc) = @_; my ($self, $id, $title, $doc) = @_;
my $text = ($title // '') . "\n" . $doc->document_element->text_content; my $text = ($title // '') . "\n" . _tc $doc->document_element;
katakana_to_hiragana $text; katakana_to_hiragana $text;
undef $title unless defined $title and length $title; undef $title unless defined $title and length $title;


Expand All @@ -43,6 +65,12 @@ sub update ($$$$) {
sub search ($$) { sub search ($$) {
my ($self, $word) = @_; my ($self, $word) = @_;
katakana_to_hiragana $word; katakana_to_hiragana $word;
$word =~ s/\s+/ /g;
$word =~ s/^ //;
$word =~ s/ $//;

## <http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html>
$word =~ s{([+\-=&|><!(){}\[\]^"~*?:\\/])}{\\$1}g;


my $url_prefix = $self->url_prefix; my $url_prefix = $self->url_prefix;
my $cv = AE::cv; my $cv = AE::cv;
Expand All @@ -51,10 +79,10 @@ sub search ($$) {
basic_auth => $self->basic_auth, basic_auth => $self->basic_auth,
content => perl2json_bytes { content => perl2json_bytes {
query => { query => {
match => {content => { query_string => {
query => $word, query => $word,
operator => 'and', default_field => 'content',
}}, },
}, },
}, },
anyevent => 1, anyevent => 1,
Expand Down
14 changes: 11 additions & 3 deletions sketch/search.pl
Expand Up @@ -51,9 +51,17 @@
basic_auth => $auth, basic_auth => $auth,
content => perl2json_bytes { content => perl2json_bytes {
query => { query => {
match => { # match => {
content => {query => "えあう", operator => 'and'}, # content => {query => "えあう", operator => 'and'},
}, # },
# terms => {
# content => ['bc', 'def', 'え', 'あ', 'う'],
# minimum_should_match => 2,
# },
query_string => {
default_field => 'content',
query => '"bc あ う"',
},
}, },
}, },
cb => sub { cb => sub {
Expand Down

0 comments on commit a7945ef

Please sign in to comment.