|
6 | 6 |
|
7 | 7 | import { flattenDeep, isArray, zipObject } from 'lodash';
|
8 | 8 |
|
9 |
| -import { CONSTANTS } from '../middleware'; |
| 9 | +import { ENV_CONFIG } from '@/config'; |
| 10 | + |
10 | 11 | import esSearch from './utils/esSearch';
|
11 | 12 |
|
12 | 13 | const resolveSetIdsFromEs = (esClient) => (setId) =>
|
13 |
| - esSearch(esClient)({ |
14 |
| - index: CONSTANTS.ES_ARRANGER_SET_INDEX, |
15 |
| - body: { |
16 |
| - query: { |
17 |
| - bool: { |
18 |
| - must: { match: { setId } }, |
19 |
| - }, |
20 |
| - }, |
21 |
| - }, |
22 |
| - }).then(({ hits: { hits } }) => flattenDeep(hits.map(({ _source: { ids } }) => ids))); |
| 14 | + esSearch(esClient)({ |
| 15 | + index: ENV_CONFIG.ES_ARRANGER_SET_INDEX, |
| 16 | + body: { |
| 17 | + query: { |
| 18 | + bool: { |
| 19 | + must: { match: { setId } }, |
| 20 | + }, |
| 21 | + }, |
| 22 | + }, |
| 23 | + }).then(({ hits: { hits } }) => flattenDeep(hits.map(({ _source: { ids } }) => ids))); |
23 | 24 |
|
24 | 25 | const getSetIdsFromSqon = ({ content } = {}, collection = []) =>
|
25 |
| - (isArray(content) |
26 |
| - ? flattenDeep( |
27 |
| - content.reduce( |
28 |
| - (acc, subSqon) => [...acc, ...getSetIdsFromSqon(subSqon, collection)], |
29 |
| - collection, |
30 |
| - ), |
31 |
| - ) |
32 |
| - : isArray(content?.value) |
33 |
| - ? content?.value.filter((value) => String(value).indexOf('set_id:') === 0) |
34 |
| - : [...(String(content?.value).indexOf?.('set_id:') === 0 ? [content.value] : [])] |
35 |
| - ).map((setId) => setId.replace('set_id:', '')); |
| 26 | + (isArray(content) |
| 27 | + ? flattenDeep( |
| 28 | + content.reduce( |
| 29 | + (acc, subSqon) => [...acc, ...getSetIdsFromSqon(subSqon, collection)], |
| 30 | + collection, |
| 31 | + ), |
| 32 | + ) |
| 33 | + : isArray(content?.value) |
| 34 | + ? content?.value.filter((value) => String(value).indexOf('set_id:') === 0) |
| 35 | + : [...(String(content?.value).indexOf?.('set_id:') === 0 ? [content.value] : [])] |
| 36 | + ).map((setId) => setId.replace('set_id:', '')); |
36 | 37 |
|
37 | 38 | const injectIdsIntoSqon = ({ sqon, setIdsToValueMap }) => ({
|
38 |
| - ...sqon, |
39 |
| - content: sqon.content.map((op) => ({ |
40 |
| - ...op, |
41 |
| - content: !isArray(op.content) |
42 |
| - ? { |
43 |
| - ...op.content, |
44 |
| - value: isArray(op.content.value) |
45 |
| - ? flattenDeep( |
46 |
| - op.content.value.map((value) => setIdsToValueMap[value] || op.content.value), |
47 |
| - ) |
48 |
| - : setIdsToValueMap[op.content.value] || op.content.value, |
49 |
| - } |
50 |
| - : injectIdsIntoSqon({ sqon: op, setIdsToValueMap }).content, |
51 |
| - })), |
| 39 | + ...sqon, |
| 40 | + content: sqon.content.map((op) => ({ |
| 41 | + ...op, |
| 42 | + content: !isArray(op.content) |
| 43 | + ? { |
| 44 | + ...op.content, |
| 45 | + value: isArray(op.content.value) |
| 46 | + ? flattenDeep( |
| 47 | + op.content.value.map((value) => setIdsToValueMap[value] || op.content.value), |
| 48 | + ) |
| 49 | + : setIdsToValueMap[op.content.value] || op.content.value, |
| 50 | + } |
| 51 | + : injectIdsIntoSqon({ sqon: op, setIdsToValueMap }).content, |
| 52 | + })), |
52 | 53 | });
|
53 | 54 |
|
54 | 55 | export const resolveSetsInSqon = ({ sqon, esClient }) => {
|
55 |
| - const setIds = getSetIdsFromSqon(sqon || {}); |
56 |
| - return setIds.length |
57 |
| - ? Promise.all(setIds.map(resolveSetIdsFromEs(esClient))).then((searchResult) => { |
58 |
| - const setIdsToValueMap = zipObject( |
59 |
| - setIds.map((id) => `set_id:${id}`), |
60 |
| - searchResult, |
61 |
| - ); |
62 |
| - return injectIdsIntoSqon({ sqon, setIdsToValueMap }); |
63 |
| - }) |
64 |
| - : sqon; |
| 56 | + const setIds = getSetIdsFromSqon(sqon || {}); |
| 57 | + return setIds.length |
| 58 | + ? Promise.all(setIds.map(resolveSetIdsFromEs(esClient))).then((searchResult) => { |
| 59 | + const setIdsToValueMap = zipObject( |
| 60 | + setIds.map((id) => `set_id:${id}`), |
| 61 | + searchResult, |
| 62 | + ); |
| 63 | + return injectIdsIntoSqon({ sqon, setIdsToValueMap }); |
| 64 | + }) |
| 65 | + : sqon; |
65 | 66 | };
|
0 commit comments