Skip to content

Commit

Permalink
Search in nested fields like file fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ksuess committed Jan 7, 2021
1 parent b042512 commit 7793c4e
Showing 1 changed file with 40 additions and 8 deletions.
48 changes: 40 additions & 8 deletions src/components/Searchkit/CustomESRequestSerializer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,54 @@ export class CustomESRequestSerializer {
serialize = (stateQuery) => {
const { queryString, sortBy, sortOrder, page, size, filters } = stateQuery;
// console.debug('CustomESRequestSerializer queryString', queryString);
// console.debug('CustomESRequestSerializer sortBy', sortBy);
// console.debug('CustomESRequestSerializer filters', filters);

const bodyParams = {};

if (!_isEmpty(queryString)) {
let qs = queryString
.split(' ')
.map((s) => s + '~')
.join(' ');
bodyParams['query'] = {
// bodyParams['query'] = {
// query_string: {
// query: qs,
// },
// };
let simpleFields = [
'title^1.2',
'id',
'description^1.1',
'subjects^2',
'freemanualtags^2',
];
let nestedFields = ['manualfile__extracted.content'];
let shouldList = nestedFields.map((fld) => {
return {
nested: {
path: fld.split('.')[0],
query: {
query_string: {
query: qs,
fields: [fld],
},
},
},
};
});
shouldList.push({
query_string: {
query: qs,
fields: simpleFields,
},
});
bodyParams['query'] = {
bool: {
should: shouldList,
},
};
}

if (sortBy !== null) {
const sortObj = {};
sortObj[sortBy] = sortOrder && sortOrder === 'desc' ? 'desc' : 'asc';
Expand Down Expand Up @@ -91,7 +125,6 @@ export class CustomESRequestSerializer {
// }
// },


const aggFieldsMapping = {
freemanualtags_agg: 'freemanualtags',
'kompasscomponent_agg.kompasscomponent_token': 'kompasscomponent',
Expand Down Expand Up @@ -164,11 +197,10 @@ export class CustomESRequestSerializer {
// console.debug('post_filter', post_filter);
bodyParams['post_filter'] = post_filter;

// simulate a backend that defines all the possible complex aggregations per index
// for this demo, we define a few simple aggregations
// aggregations
bodyParams['aggs'] = {};

// listFields
// aggregations of listFields
Object.keys(aggFieldsMapping).map((aggName) => {
const fieldName = aggFieldsMapping[aggName];
// console.debug('aggs', fieldName, listFields.includes(fieldName));
Expand All @@ -180,7 +212,7 @@ export class CustomESRequestSerializer {
}
});

// nestedFields
// aggregations of nestedFields
Object.keys(aggFieldsMapping).map((aggName) => {
const myaggs = aggName.split('.');
const fieldName = aggFieldsMapping[aggName];
Expand Down Expand Up @@ -209,7 +241,7 @@ export class CustomESRequestSerializer {
_extend(bodyParams['aggs'], aggBucketTermsComponent);
}
});

// console.debug('serialize bodyParams', bodyParams);
return bodyParams;
};
}
Expand Down

0 comments on commit 7793c4e

Please sign in to comment.