如何缓存从自动完成的API检索到的搜索结果

时间:2019-01-22 14:22:44

标签: java gwt autocomplete jsonp bootstrap-typeahead

我一直在使用Bootstrap-3-TypeaheadGWT进行搜索自动完成功能,并且数据是通过JsonP请求来自http://dawa.aws.dk/dok/api的。

下面是Typahead的创建方法

private Typeahead<Location> createTypeAhead() {
    typeAhead = new Typeahead<>(new Dataset<Location>() {
        @Override
        public void findMatches(final String query, final SuggestionCallback<Location> callback) {
            requestCounter--;
            startSendingRequest = true;
            clear.setIcon(IconType.SPINNER);
            clear.setIconSpin(true);
            final Set<Suggestion<Location>> suggestions = new HashSet<>();
            queryLower = query.toLowerCase();
            JsonpRequestBuilder jsonpRequestBuilder;
            if (!streetSelected) {
                jsonpRequestBuilder = new JsonpRequestBuilder();
                jsonpRequestBuilder.requestObject("https://dawa.aws.dk/vejnavne/autocomplete?side=1&per_side=500&noformat=1&q=" + queryLower + "*", new AsyncCallback<MyJsArray<VejAutocomplete>>() {
                    @Override
                    public void onFailure(Throwable caught) {
                        Notify.notify("suggestion matches failed");
                    }

                    @Override
                    public void onSuccess(MyJsArray<VejAutocomplete> result) {
                        Set<Location> locationSet = new LinkedHashSet<>();
                        for (VejAutocomplete item : result.getAsList()) {
                            String lowerCase = item.getTekst().toLowerCase();
                            if (lowerCase.startsWith(queryLower)) {
                                locationSet.add(new Location(Location.LocationType.STREET, item.getTekst(), item));
                                locationArrayList.clear();
                                locationArrayList.addAll(locationSet);
                            }
                        }
                    }
                });
            }
            for (Location address : locationArrayList) {
                String value = address.getValue();
                Suggestion<Location> s = Suggestion.create(value, address, this);

                if (address.getValue().toLowerCase().startsWith(queryLower)) {
                    suggestions.add(s);
                }
            }
            callback.execute(suggestions);
            if (typeAhead.getValue().length() != 0 && queryLower.length() <= 5 && requestCounter < 5 && requestCounter > 0) {

                new Timer() {
                    @Override
                    public void run() {
                        findMatches(queryLower, callback);
                    }
                }.schedule(500);
            } else {
                clear.setIconSpin(false);
                clear.setIcon(IconType.CLOSE);
                requestCounter = 5;
            }
        }
    });
    return typeAhead;
}

结果如下:

enter image description here

我使用了递归来发送4-5次请求,因为它没有显示带有单个字母关键字的建议列表。而且它仍然无法与某些单个字母(例如“ s”或“ e”)一起使用。已成功从API检索数据,但未显示在建议列表中,如下所示:

enter image description here

我假设我应该缓存所有搜索结果,然后从头开始重新创建自动完成功能,在这种情况下会变得很复杂。

有什么好办法解决这个问题吗?

0 个答案:

没有答案