Skip to content
This repository was archived by the owner on May 28, 2023. It is now read-only.

Commit 5ff1f97

Browse files
author
tkostuch
committed
3948 use elasticsearch-js instead of request
1 parent 9e50017 commit 5ff1f97

File tree

2 files changed

+51
-52
lines changed

2 files changed

+51
-52
lines changed

src/api/attribute/service.ts

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11

2-
import request from 'request';
32
import TagCache from 'redis-tag-cache'
43
import get from 'lodash/get';
54
import cache from '../../lib/cache-instance'
5+
import { adjustQuery, getClient as getElasticClient } from './../../lib/elastic'
6+
import bodybuilder from 'bodybuilder'
67

78
export interface AttributeListParam {
89
[key: string]: number[]
@@ -63,64 +64,63 @@ function clearAttributeOpitons (attribute, optionsIds: number[]) {
6364
}
6465
}
6566

66-
function list (attributesParam: AttributeListParam, config, indexName: string): Promise<any[]> {
67-
return new Promise(async (resolve, reject) => {
68-
// we start with all attributeCodes that are requested
69-
let attributeCodes = Object.keys(attributesParam)
67+
async function list (attributesParam: AttributeListParam, config, indexName: string): Promise<any[]> {
68+
// we start with all attributeCodes that are requested
69+
let attributeCodes = Object.keys(attributesParam)
7070

71-
// here we check if some of attribute are in cache
72-
const rawCachedAttributeList = await Promise.all(
73-
attributeCodes.map(attributeCode => getAttributeFromCache(attributeCode, config))
74-
)
71+
// here we check if some of attribute are in cache
72+
const rawCachedAttributeList = await Promise.all(
73+
attributeCodes.map(attributeCode => getAttributeFromCache(attributeCode, config))
74+
)
7575

76-
const cachedAttributeList = rawCachedAttributeList
77-
.filter(Boolean) // remove empty results from cache.get
78-
.map((cachedAttribute, index) => {
79-
if (cachedAttribute) {
80-
const attributeOptionsIds = attributesParam[cachedAttribute.attribute_code]
76+
const cachedAttributeList = rawCachedAttributeList
77+
.filter(Boolean) // remove empty results from cache.get
78+
.map((cachedAttribute, index) => {
79+
if (cachedAttribute) {
80+
const attributeOptionsIds = attributesParam[cachedAttribute.attribute_code]
8181

82-
// side effect - we want to reduce starting 'attributeCodes' because some of them are in cache
83-
attributeCodes.splice(index, 1)
82+
// side effect - we want to reduce starting 'attributeCodes' because some of them are in cache
83+
attributeCodes.splice(index, 1)
8484

85-
// clear unused options
86-
return clearAttributeOpitons(cachedAttribute, attributeOptionsIds)
87-
}
88-
})
89-
90-
// if all requested attributes are in cache then we can return here
91-
if (!attributeCodes.length) {
92-
return resolve(cachedAttributeList)
93-
}
94-
95-
// fetch attributes for rest attributeCodes
96-
request({
97-
uri: getUri(config, indexName),
98-
method: 'POST',
99-
body: {'query': {'bool': {'filter': {'bool': {'must': [{'terms': {'attribute_code': attributeCodes}}]}}}}},
100-
json: true
101-
}, async (err, res, body) => {
102-
if (err) {
103-
reject(err)
85+
// clear unused options
86+
return clearAttributeOpitons(cachedAttribute, attributeOptionsIds)
10487
}
105-
const fetchedAttributeList = get(body, 'hits.hits', []).map(hit => hit._source)
106-
107-
// save atrributes in cache
108-
await setAttributeInCache(fetchedAttributeList, config)
88+
})
10989

110-
// cached and fetched attributes
111-
const allAttributes = [
112-
...cachedAttributeList,
113-
...fetchedAttributeList.map(fetchedAttribute => {
114-
const attributeOptionsIds = attributesParam[fetchedAttribute.attribute_code]
90+
// if all requested attributes are in cache then we can return here
91+
if (!attributeCodes.length) {
92+
return cachedAttributeList
93+
}
11594

116-
// clear unused options
117-
return clearAttributeOpitons(fetchedAttribute, attributeOptionsIds)
118-
})
119-
]
95+
// fetch attributes for rest attributeCodes
96+
try {
97+
const query = adjustQuery({
98+
index: indexName,
99+
type: 'attribute',
100+
body: bodybuilder().filter('terms', 'attribute_code', attributeCodes).build()
101+
}, 'attribute', config)
102+
const response = await getElasticClient(config).search(query)
103+
const fetchedAttributeList = get(response.body, 'hits.hits', []).map(hit => hit._source)
104+
105+
// save atrributes in cache
106+
await setAttributeInCache(fetchedAttributeList, config)
107+
108+
// cached and fetched attributes
109+
const allAttributes = [
110+
...cachedAttributeList,
111+
...fetchedAttributeList.map(fetchedAttribute => {
112+
const attributeOptionsIds = attributesParam[fetchedAttribute.attribute_code]
113+
114+
// clear unused options
115+
return clearAttributeOpitons(fetchedAttribute, attributeOptionsIds)
116+
})
117+
]
120118

121-
return resolve(allAttributes)
122-
})
123-
})
119+
return allAttributes
120+
} catch (err) {
121+
console.error(err)
122+
return []
123+
}
124124
}
125125

126126
/**

src/graphql/elasticsearch/queryBuilder.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,5 @@ export function buildQuery ({
1919
if (search !== '') {
2020
builtQuery['min_score'] = config.get('elasticsearch.min_score')
2121
}
22-
2322
return builtQuery;
2423
}

0 commit comments

Comments
 (0)