-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathsearch.psgi
More file actions
108 lines (91 loc) · 2.6 KB
/
search.psgi
File metadata and controls
108 lines (91 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env perl
use strict;
use warnings;
use Encode qw(encode_utf8);
use Plack::Request;
use Plack::Builder;
use Plack::Response;
use ElasticSearch;
use FindBin;
use lib "$FindBin::RealBin/lib";
use ES::Util qw(run);
chdir($FindBin::RealBin);
our $host = 'localhost:9200';
our $es = ElasticSearch->new( servers => $host );
builder {
mount '/elastic-search-website/' => \&old_doc_search;
mount '/search/' => \&doc_search;
mount '/status' => \&status;
};
#===================================
sub status {
#===================================
my $req = Plack::Request->new( shift() );
my $result
= eval { run(qw(git show --shortstat _index)); }
|| $@
|| 'Unknown error';
return [ 200, [ 'Content-Type' => 'text/plain' ], [$result] ];
}
#===================================
sub doc_search {
#===================================
my $req = Plack::Request->new( shift() );
my $callback = $req->param('callback');
my $q = $req->param('q');
my $result = $es->search(
index => 'docs',
fields => [ 'title', 'abbr', 'url', 'path' ],
query => {
multi_match => {
query => $q,
fields => [
'title', 'title.shingles',
'title.ngrams', 'text',
'text.shingles', 'text.ngrams',
'book'
],
minimum_should_match => '50%',
}
},
size => 10,
as_json => 1
);
$result = $callback . '(' . $result . ')'
if $callback;
return [
200,
[ 'Content-Type' => 'application/json' ],
[ encode_utf8($result) ]
];
}
#===================================
sub old_doc_search {
#===================================
my $req = Plack::Request->new( shift() );
my $callback = $req->param('callback');
my $q = $req->param('q');
my $result = $es->search(
index => 'es_docs',
fields => [ 'title', 'category', 'url' ],
query => {
multi_match => {
query => $q,
fields => [
'title', 'title.shingles^2',
'content', 'content.shingles^10'
],
minimum_should_match => '60%',
}
},
size => 10,
as_json => 1
);
$result = $callback . '(' . $result . ')'
if $callback;
return [
200,
[ 'Content-Type' => 'application/json' ],
[ encode_utf8($result) ]
];
}