This repository has been archived by the owner on May 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.tag
89 lines (78 loc) · 2.25 KB
/
search.tag
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
<search>
<results
if={results}
hits={results.hits}
total={results.total}
counts={results.counts}
page={results.page}
page_count={results.page_count}
prev_url={results.prev_url}
next_url={results.next_url}
collections={opts.collections}
></results>
<p if={searching}>searching ...</p>
<p if={error} class="alert alert-danger">{error}</p>
<script>
function buildQuery(q) {
return {
query_string: {
default_field: 'text',
query: q,
default_operator: 'AND',
},
}
}
function search(query, success, error) {
$.ajax({
url: '/search',
method: 'POST',
data: JSON.stringify({
from: (query.page - 1) * query.size,
size: query.size,
query: buildQuery(query.q),
collections: query.collections,
fields: ['title', 'url', 'mime_type'],
highlight: {fields: {text: {fragment_size: 150, number_of_fragments: 3}}},
}),
success: success,
error: error,
})
}
performSearch() {
if(this.query === opts.query) return
this.query = opts.query
this.searching = true
search(this.query, function(resp) {
this.searching = false
var url = function(p) {
var u = "?q=" + encodeURIComponent(this.query.q)
if(p > 1) u += "&p=" + p
return u
}.bind(this)
page_count = Math.ceil(resp.hits.total / this.query.size)
var page = this.query.page
var prev_url = page > 1 ? url(page - 1) : null
var next_url = page < page_count ? url(page + 1) : null
this.results = {
hits: resp.hits.hits,
total: resp.hits.total,
counts: resp.count_by_index,
page: page,
page_count: page_count,
prev_url: prev_url,
next_url: next_url,
}
this.error = null
this.update()
}.bind(this), function(err) {
console.log(err.responseText)
this.searching = false
this.results = null
this.error = "Server error while searching"
this.update()
}.bind(this))
}
this.on('update', this.performSearch.bind(this))
this.performSearch()
</script>
</search>