Skip to content

Commit

Permalink
Request only needed values
Browse files Browse the repository at this point in the history
  • Loading branch information
olamothe committed Oct 24, 2017
1 parent 7146847 commit cd63fae
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/controllers/FacetQueryController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ export class FacetQueryController {
* @returns {Promise|Promise<T>}
*/
public search(params: FacetSearchParameters, oldLength = params.nbResults): Promise<IIndexFieldValue[]> {
// For search, we want to retrieve the exact values we requested, and not additional ones
params.completeFacetWithStandardValues = false;
return new Promise((resolve, reject) => {
const onResult = (fieldValues?: IIndexFieldValue[]) => {
const newLength = fieldValues.length;
Expand Down
23 changes: 16 additions & 7 deletions src/ui/Facet/FacetSearchParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class FacetSearchParameters {
public alwaysExclude: string[] = [];
public sortCriteria = 'occurrences';
public fetchMore = false;
public completeFacetWithStandardValues = true;

constructor(public facet: Facet) {
this.nbResults = facet.options.numberOfValuesInFacetSearch;
Expand Down Expand Up @@ -54,18 +55,26 @@ export class FacetSearchParameters {
typedByUser = ['*' + this.valueToSearch + '*'];
}

let completeFacetWithStandardValues = true;
let allowedValues;
if (this.valueToSearch) {
allowedValues = typedByUser
.concat(this.alwaysInclude)
.concat(this.alwaysExclude)
} else {
allowedValues = _.compact(
typedByUser
.concat(this.alwaysInclude)
.concat(this.facet.options.allowedValues)
)
}

let completeFacetWithStandardValues = this.completeFacetWithStandardValues;
if (this.facet.options.lookupField != null) {
completeFacetWithStandardValues = false;
}

const request: IGroupByRequest = {
allowedValues: _.compact(
typedByUser
.concat(this.alwaysInclude)
.concat(this.alwaysExclude)
.concat(this.facet.options.allowedValues)
),
allowedValues: allowedValues,
maximumNumberOfValues: nbResults,
completeFacetWithStandardValues: completeFacetWithStandardValues,
field: <string>this.facet.options.field,
Expand Down
14 changes: 11 additions & 3 deletions src/ui/Facet/FacetValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,17 @@ export class FacetValue {
this.computedField = newValue.computedField;
}

cloneWithZeroOccurrences(): FacetValue {
this.occurrences = 0;
clone(): FacetValue {
this.computedField = undefined;
this.delta = undefined;
return this;
}

cloneWithZeroOccurrences(): FacetValue {
this.occurrences = 0;
return this.clone();
}

cloneWithDelta(count: number, delta: number): FacetValue {
Assert.isLargerOrEqualsThan(0, count);
var clone = this.cloneWithZeroOccurrences();
Expand Down Expand Up @@ -192,7 +196,11 @@ export class FacetValues {
if (Utils.exists(myValue)) {
myValue.selected = true;
} else {
this.values.push(otherValue.cloneWithZeroOccurrences());
if (otherValue.occurrences) {
this.values.push(otherValue.clone())
} else {
this.values.push(otherValue.cloneWithZeroOccurrences());
}
}
});

Expand Down

0 comments on commit cd63fae

Please sign in to comment.