Im使用Amazon CloudSearch作爲源的一個JQuery自動完成選擇。它工作得很好,但由於我無法識別的原因,當僅返回一個結果時,編碼變得混亂。Jquery Autocomplete x Amazon CloudSearch編碼
該解決方案的工作原理如下:
文本輸入,人們鍵入某個城市的街區或街道:
<div id="searchFormContainer">
<input type="text" value=" Digite o bairro ou rua" name="inputSearch" id="inputSearch"/>
</div>
JQuery的自動完成配置:
var sourceFunction = function (request, response) {
var successFunction = function (places) {
var placesWithLabel = jQuery.map(places, function (place) {
var label = (place.addressName) ? place.addressName+', ' : '';
label += place.neighborhoodName;
var value = label;
return {label:label, value:value};
});
if(placesWithLabel.length==0){
placesWithLabel.push({label:"Não encontrado", value:""})
}
response(placesWithLabel);
}
var ajaxOptions = {appendTo: '#searchFormContainer',
url: '/textSearch', dataType: "json",
data: {strToSearch: request.term,
cityName: self.place.city.name},
success: successFunction};
jQuery.ajax(ajaxOptions);
};
var openFunction = function() {
jQuery(this).removeClass("ui-corner-all").addClass("ui-corner-top");
};
var closeFunction = function() {
jQuery(this).removeClass("ui-corner-top").addClass("ui-corner-all");
};
var selectFunction = function (event, data) {
if(data.item.value==""){
return;
}else{
//redirect to result page
}
};
**var autocompleteOptions = {appendTo: '#searchFormContainer',
source: sourceFunction,
minLength: 1,
open: openFunction,
close: closeFunction,
select: selectFunction};**
**$('#searchFormContainer #inputSearch').autocomplete(autocompleteOptions);**
它成功地工作,呼籲我的python視圖名爲textSearch,它的確如下:
def textSearch(request):
results = simplejson.loads(requests.get('http://%s/2011-02-01/search?' % (settings.SEARCH_CLOUD_HOST), params=fieldsParameter).text)['hits']
return HttpResponse(simplejson.dumps(results), mimetype='application/json')
以例如用於命名的Oscar Freire在附近塞基耶拉塞 - 薩爾街道搜索,當我在自動完成輸入奧斯卡神父,亞馬遜CloudSearch返回我結果:
[16/Apr/2013 23:59:47] "GET /textSearch?strToSearch=Oscar+F&cityName=S%C3%A3o+Paulo HTTP/1.1" 200 682
Neighbourhood from Amazon --> Cerqueira César
Returns from Amazon --> {'id_address': u'52267', 'neighborhoodName': u'Cerqueira C\xe9sar', 'addressName': u'Rua Oscar Freire', 'id_neighborhood': u'19694'}
Neighbourhood from Amazon --> Jardim Anália Franco
Returns from Amazon --> {'id_address': u'61073', 'neighborhoodName': u'Jardim An\xe1lia Franco', 'addressName': u'Rua Jos\xe9 Oscar Abreu Sampaio', 'id_neighborhood': u'19881'}
但,輸入一個以上的字母與搜索關鍵字奧斯卡FRE,僅返回一個結果I`ve:
[16/Apr/2013 23:59:49] "GET /textSearch?strToSearch=Oscar+Fr&cityName=S%C3%A3o+Paulo HTTP/1.1" 200 286
Neighbourhood from Amazon --> Cerqueira CĂŠsar
Returns from Amazon --> {'id_address': u'52267', 'neighborhoodName': u'Cerqueira C\u0102\u0160sar', 'addressName': u'Rua Oscar Freire', 'id_neighborhood': u'19694'}
正如我們在輸出中看到的那樣,返回的對象是相同的(兩個結果的id_address是相同的)。 (從CloudSearch提取RAW JSON)此條亞馬遜對象是:
{"rank":"-text_relevance","match-expr":"(label 'Oscar Freire')","hits":{"found":1,"start":0,"hit":[{"id":"52267","data":{"address":["Rua Oscar Freire"],"bairro":["Cerqueira César"],"fieldtype":["address"],"id_address":["52267"],"id_neighborhood":["19694"],"latitude":["-23.568315"],"longitude":["-46.66293"],"text_relevance":["310"]}}]},"info":{"rid":"e2467862eecf73","time-ms":3,"cpu-time-ms":0}}
對我來說是清楚的是,響應與錯誤的編碼到我的蟒蛇視圖來的事實。但我不能老是意識到這哪裏問題開始。如果這是來自JQuery自動完成的不良行爲或Amazon響應的問題。有任何想法嗎?
問候