有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

前面的创建方法如下所示

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) 个答案