-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
App.vue
231 lines (205 loc) · 5.1 KB
/
App.vue
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<template>
<div>
<header class="header">
<h1 class="header-title">
<a href="/">
typesense-vue-instanstantsearch-demo
</a>
</h1>
<p class="header-subtitle">
using
<a href="https://github.com/algolia/vue-instantsearch">
Vue InstantSearch + Typesense Instantsearch adapter
</a>
</p>
</header>
<div class="container">
<ais-instant-search
:search-client="searchClient"
index-name="books"
:search-function="searchFunction"
>
<ais-configure v-bind="searchParameters" />
<div class="search-panel">
<div class="search-panel__filters">
<h4>Author</h4>
<ais-refinement-list attribute="authors" />
<h4>Year</h4>
<ais-refinement-list attribute="publication_year" />
</div>
<div class="search-panel__results">
<div class="searchbox">
<ais-search-box placeholder="" />
</div>
<ais-hits>
<template slot="item" slot-scope="{ item }">
<article>
<img
:src="item['image_url']"
:alt="item['name']"
height="100"
/>
<h1>
<ais-highlight
:hit="item"
attribute="title"
class="hit-name"
/>
</h1>
<div class="hit-authors">
<span
v-for="(fieldValue, index) in item['authors']"
:key="`highlight-${fieldValue}-${index}`"
>
<ais-highlight
:hit="item"
:attribute="`authors.${index}`"
/><template v-if="index < item['authors'].length - 1"
>,
</template>
</span>
</div>
<div class="hit-publication-year">
{{ item['publication_year'] }}
</div>
<div class="hit-rating">{{ item['average_rating'] }}/5</div>
</article>
</template>
</ais-hits>
<div class="pagination">
<ais-pagination />
</div>
</div>
</div>
</ais-instant-search>
</div>
</div>
</template>
<script>
import TypesenseInstantSearchAdapter from 'typesense-instantsearch-adapter';
const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
server: {
apiKey: 'xyz', // Be sure to use an API key that only allows searches, in production
nodes: [
{
host: 'localhost',
port: '8108',
protocol: 'http',
},
],
},
// The following parameters are directly passed to Typesense's search API endpoint.
// So you can pass any parameters supported by the search endpoint below.
// queryBy is required.
// filterBy is managed and overridden by InstantSearch.js. To set it, you want to use one of the filter widgets like refinementList or use the `configure` widget.
additionalSearchParameters: {
queryBy: 'title,authors',
},
});
const searchClient = typesenseInstantsearchAdapter.searchClient;
export default {
data() {
return {
searchClient,
searchParameters: {
hitsPerPage: 8,
facets: ['authors', 'publication_year'],
},
};
},
methods: {
searchFunction(helper) {
helper
.addFacetRefinement('authors', 'Suzanne Collins')
.addNumericRefinement('publication_year', '>=', 2004)
.search();
},
},
};
</script>
<style>
body,
h1 {
margin: 0;
padding: 0;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica,
Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol';
}
em {
background: cyan;
font-style: normal;
}
.header {
display: flex;
align-items: center;
min-height: 50px;
padding: 0.5rem 1rem;
background-image: linear-gradient(to right, #4dba87, #2f9088);
color: #fff;
margin-bottom: 1rem;
}
.header a {
color: #fff;
text-decoration: none;
}
.header-title {
font-size: 1.2rem;
font-weight: normal;
}
.header-title::after {
content: ' ▸ ';
padding: 0 0.5rem;
}
.header-subtitle {
font-size: 1.2rem;
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 1rem;
}
.search-panel {
display: flex;
}
.search-panel__filters {
flex: 1;
}
.search-panel__results {
flex: 3;
}
.ais-Highlight-highlighted {
color: inherit;
font-size: inherit;
}
.searchbox {
margin-bottom: 2rem;
}
.pagination {
margin: 2rem auto;
text-align: center;
}
.hit-name {
font-size: 1.1rem;
font-weight: bold;
margin-top: 10px;
}
.hit-authors {
margin-top: 3px;
font-size: 0.8rem;
}
.hit-publication-year {
font-size: 0.8rem;
margin-top: 20px;
}
.hit-rating {
margin-top: 3px;
font-size: 0.8rem;
}
.ais-Hits-item {
padding: 30px;
box-shadow: none;
border: 1px solid lighten(lightgray, 8%);
}
</style>