Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

feat(answers): add findAnswers #804

Merged
merged 12 commits into from Dec 23, 2020
46 changes: 46 additions & 0 deletions src/algoliasearch.helper.js
Expand Up @@ -8,6 +8,8 @@ var requestBuilder = require('./requestBuilder');
var events = require('events');
var inherits = require('./functions/inherits');
var objectHasKeys = require('./functions/objectHasKeys');
var omit = require('./functions/omit');
var merge = require('./functions/merge');

var version = require('./version');

Expand Down Expand Up @@ -248,6 +250,50 @@ AlgoliaSearchHelper.prototype.searchOnce = function(options, cb) {
});
};

/**
* Start the search for answers with the parameters set in the state.
* This method returns a promise.
* @return {AlgoliaSearchHelper}
eunjae-lee marked this conversation as resolved.
Show resolved Hide resolved
* @return {promise.<FacetSearchResult>} the answer results
*/
AlgoliaSearchHelper.prototype.searchForAnswers = function(attributesForPrediction, queryLanguages, nbHits) {
Haroenv marked this conversation as resolved.
Show resolved Hide resolved
Haroenv marked this conversation as resolved.
Show resolved Hide resolved
var state = this.state;
var derivedHelper = this.derivedHelpers[0];
if (!derivedHelper) {
return Promise.resolve([]);
}
var derivedState = derivedHelper.getModifiedState(state);
var data = merge(
{
attributesForPrediction: attributesForPrediction,
nbHits: nbHits
},
{
params: omit(requestBuilder._getHitsSearchParams(derivedState), [
'attributesToSnippet',
'hitsPerPage',
'restrictSearchableAttributes',
'snippetEllipsisText' // FIXME remove this line once the engine is fixed.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlike the doc, snippetEllipsisText is throwing an error. I've reported it to the team, but for now let's have it here.

])
}
);

if (
typeof this.client.initIndex !== 'function'
) {
throw new Error(
'search for answers was called, but this client does not have a function client.initIndex'
);
}
var index = this.client.initIndex(derivedState.index);
if (typeof index.findAnswers !== 'function') {
throw new Error(
'search for answers was called, but this client does not have a function client.initIndex(index).findAnswers'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is helpful, but the second error message gives pretty much the same information. Could it be consolidated to save space? How do we do it in searchForFacetValues?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlike SFFV, the existence of findAnswers can be checked only after creating index here. So I couldn't merge them in a single conditional block.
2433fb1

);
}
return index.findAnswers(derivedState.query, queryLanguages, data);
};

/**
* Structure of each result when using
* [`searchForFacetValues()`](reference.html#AlgoliaSearchHelper#searchForFacetValues)
Expand Down