如何使用AngularJS显示Elasticsearch Phrase Suggester的搜索建议

时间:2015-09-05 03:22:14

标签: javascript angularjs

尝试使用AngularJS从ES Phrase Suggester的Suggestion对象中显示选项数组中的前10个选项,但是难以接受....

这将输出选项数组中的第一个元素:

{{suggestions.options[0].text}}

案文是实际建议。

Chrome中Dev Console的输出:

suggest: {,…}
phraseSuggestion: [{text: "pytho", offset: 0, length: 5, options: [{text: "python", score: 0.12858662}]}]
0: {text: "pytho", offset: 0, length: 5, options: [{text: "python", score: 0.12858662}]}
length: 5
offset: 0
options: [{text: "python", score: 0.12858662}]
0: {text: "python", score: 0.12858662}
text: "pytho"

我如何访问:

options: [{text: "python", score: 0.12858662}]

在我的Html模板中,显示数组中的前5到10个选项。

1 个答案:

答案 0 :(得分:1)

因此,如果你有像这样的JSON输出,并且你想显示前2个选项,你可以这样做:

JSFiddle

HTML:

<div ng-app="myApp" ng-controller="dummy">
    <p ng-repeat="option in out.options | limitTo:2">{{option.text}} - {{option.score}}</p>
</div>

JS:

angular.module('myApp', [])
    .controller('dummy', ['$scope', '$sce', function ($scope, $sce) {
    $scope.out = {
        "options": [{
            "text": "python",
                "score": 0.12858662
        }, {
            "text": "linux",
                "score": 0.12858662
        }, {
            "text": "php",
                "score": 0.12858662
        }, {
            "text": "js",
                "score": 0.12858662
        }, {
            "text": "java",
                "score": 0.12858662
        }]
    };

}]);