-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.js
53 lines (49 loc) · 1.25 KB
/
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
const elasticsearch = require('elasticsearch');
const jsontransform = require('node-json-transform');
const client = new elasticsearch.Client({
host: 'localhost:9200',
});
const mappingSearchModel = function mappingSearchModel(resp) {
const map = {
list: 'hits.hits',
item: {
text: '_source.model',
id: '_source.model_id',
},
};
const dataTransform = jsontransform.DataTransform(resp, map);
const outputList = dataTransform.transform();
return { items: outputList, total_count: resp.hits.total };
};
const searchModel = function searchModel(term, page) {
let resultsFrom = 0;
const resultsSize = 10;
if (page) {
resultsFrom = resultsSize * (page - 1);
}
const query = {
index: 'car_models',
type: 'document',
body: {
query: {
function_score: {
query: {
multi_match: {
query: term,
fields: ['brand', 'model'],
},
},
field_value_factor: {
field: 'count_by_model',
modifier: 'log1p',
factor: 2,
},
},
},
size: resultsSize,
from: resultsFrom,
},
};
return client.search(query).then(mappingSearchModel);
};
module.exports = { searchModel };