I'm trying to figure out how I can convert my Angular 1.x search service for Elasticsearch that uses promises (q library) to an Angular 4.x search service using Observables?
OR
I've also seen toPromise, where an Observable is converted to a promise and vise versa. However, I've seen a handful of simple examples for search and autocomplete using Observables, so it seems Observables is the preferred way of using HTTP in Angular 4.
This was what I used for autocomplete in Angular 1.x
this.getSuggestions = function(query) {
var deferred = $q.defer();
esClient.search({
index: 'query-index',
body: {
"query": {
"match_phrase_prefix": {
"suggestions": {
"query": query,
"max_expansions": 10,
"lenient": true
}
}
},
"size": 5,
"from": 0,
"_source": ["suggestions"]
}
}).then(function(es_return) {
deferred.resolve(es_return);
}, function(error) {
deferred.reject(error);
});
return deferred.promise;
};
I'm not sure really where to even begin on how to convert that to an Observable?