0

我使用Amazon CloudSearch作为JQuery Autocomplete select的源。它工作得很好,但是由于我无法识别的原因,当只返回一个结果时,编码变得一团糟。

该解决方案的工作原理如下:

人们输入给定城市的社区或街道的文本输入:

<div id="searchFormContainer">
    <input type="text" value="&nbsp;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);**

它成功运行,调用我的名为textSearch的 python 视图,它执行以下操作:

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')

例如,在 Cerqueira César 附近搜索名为 Oscar Freire 的街道时,当我在自动完成中键入Oscar Fr时,Amazon 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'}

但是,用搜索关键字Oscar Fre再输入一个字母,它只返回一个结果:

[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 中提取的原始 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}}

对我来说很清楚,响应来自我的 python 视图的错误编码。但我无法意识到这个问题从哪里开始。如果这是 JQuery 自动完成的不良行为或亚马逊响应的问题。有任何想法吗?

此致

4

1 回答 1

3

我认为隔离问题的可能原因很重要。这里有太多可能的编码问题来源,您应该从消除可能的问题来源开始。

如果您在浏览器中搜索“Oscar Fr”与“Oscar F”(使用 CloudSearch 搜索端点),编码是否会发生变化?如果它保持不变,那么问题不在于 CloudSearch,您可以向上移动堆栈。

于 2013-04-18T19:33:45.797 回答