请注意,为了简单地测试此服务,你可以通过其仪表盘手动将数据上传到 Algolia,但这并非长期使用的良好策略。
import {Algolia} from "nativescript-algolia"; var client = new Algolia('<app-id>', '<api-key>'); var index = client.initIndex("recipes");
<StackLayout class="green">
<SearchBar #SearchBar (submit)="search($event)"></SearchBar>
<ScrollView height="87%">
<WrapLayout horizontalAlignment="center">
<StackLayout style="margin-left: 10" class="card" width="45%" *ngFor="let recipe of recipes">
<StackLayout horizontalAlignment="center" (tap)="goToRecipe(recipe.id)">
<Image [src]="recipe.Image"></Image>
<Label class="name" horizontalAlignment="center" textWrap="true" [text]="recipe.Name"></Label>
</StackLayout>
</StackLayout>
</WrapLayout>
</ScrollView>
<Image src="~/images/search-by-algolia.png" style="margin:5;width:200"></Image>
</StackLayout>
search(e: any) {
this.loader.show({ message: 'Finding recipes...' });
//clear array
this.recipes = [];
this.term = "";
if (e && e.object) {
this.term = e.object.text;
index.search(this.term, (results, errors) => {
for (let id in results.hits) {
let result = (Object).assign({id: results.hits[id].objectID, Name: results.hits[id].Name, Image: results.hits[id].Image});
this.ngZone.run(() => {
this.recipes.push(result);
})
this.loader.hide();
}
})
}
else {
alert("Sorry, no recipes found!");
this.loader.hide();
}
}
index.setSettings({
'searchableAttributes': [
'Name',
'Ingredients',
'Method'
]
}, function(err, content) {
console.log(content);
});