This repository has been archived by the owner on Jun 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asset.search.js
57 lines (52 loc) · 1.61 KB
/
asset.search.js
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
'use strict';
const { query, db } = require('@arangodb');
const joi = require('joi');
const createRouter = require('@arangodb/foxx/router');
const router = createRouter();
const view_name = 'AssetsSearchView';
const collection_name = 'assets';
db._dropView(view_name);
const asset_view = db._createView(view_name, 'arangosearch', {});
asset_view.properties({
links: {
[collection_name]:
{
analyzers: ["text_en"],
includeAllFields: false,
fields: {
source: {},
properties: {
includeAllFields: true,
fields:
{
seasonYear: {
analyzers: ["identity"],
}
}
}
}
}
}
}
);
router.get('/search', function (req, res) {
const start = Date.now();
const { q } = req.queryParams;
const results = db._query(`
let words = TOKENS(@q, 'text_en')
FOR asset IN AssetsSearchView
SEARCH ANALYZER(asset.properties.brand in words, 'text_en')
OR ANALYZER(asset.properties.season in words, 'text_en')
OR ANALYZER(asset.properties.city in words, 'text_en')
SORT TFIDF(asset) DESC
LIMIT 200
RETURN MERGE({id:asset._id}, asset.properties)
`, {q}).toArray();
const time = `${Date.now() - start}ms`;
res.json({ results, time });
})
.queryParam('q', joi.string().required(), 'search query')
.response(['text/plain'], 'Matched results')
.summary('Search Asset')
.description('Full-text search on asset');
module.exports = router;