Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solr JQuery autocomplete doesn't autocomplete

I'm trying to implement an HTML autocomplete form with Solr and JQuery UI.

Solr's server log the query and Firebug confirm that I get a json array :

{"responseHeader":{"status":0,"QTime":3,"params":{"fl":"defaultCommonNameString","q":"defaultCommonName:angle","wt":"json"}},"response":{"numFound":42,"start":0,"docs":[{"defaultCommonNameString":["angle"]},{"defaultCommonNameString":["reporter un angle"]},{"defaultCommonNameString":["formule pour un angle"]},{"defaultCommonNameString":["angle droit"]},{"defaultCommonNameString":["angle au centre"]}, ...]}}}

But this code doesn't display the response :

<link rel="stylesheet" type="text/css" href="css/demos.css" />
<script type="text/javascript" src="jquery/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="jquery/jquery.ui.core.js"></script>
<script type="text/javascript" src="jquery/jquery.ui.widget.js"></script>
<script type="text/javascript" src="jquery/jquery.ui.position.js"></script>
<script type="text/javascript" src="jquery/jquery.ui.autocomplete.js"></script>
<script>
$(function() {
    $("#autosearch").autocomplete({
        source: function( request, response ) {
            $.ajax({
            url: "http://localhost:8080/solr/select",
            data: { 
                q: "defaultCommonName:"+request.term,
        wt:"json",
        fl: "defaultCommonNameString",
             }, 
             dataType: "json",
             type: "GET",

             success: function( data ) {
                data=parse();
                function parse(){
                var parsedQueries=[];
                    for(var i=0;i<data.response.docs.length;i++){
                        parsedQueries[i]=data.response.docs[i].Query;
                    }
                    return parsedQueries;
                }

                response($.map(data.response.docs, function( item ) { 
                    return { 
                        label: item,
                        value: item
                    }; 
            }));
            }
          }); 
         },
        minLength: 1
        });
    });
</SCRIPT>
</head>
<body>
<label for="autosearch">Search Here :  </label>
<input id="autosearch"/>
</body>

Thanks for help

like image 549
doumeasse38 Avatar asked Oct 04 '12 09:10

doumeasse38


1 Answers

Many thanks D_K, that's work fine !!!

The code :

$(function() {
    $("#autosearch").autocomplete({
        source: function( request, response ) {
            $.ajax({
            url: "http://localhost:8080/solr/select",
            data: { 
                q: "defaultCommonName:"+request.term,
                fl: "defaultCommonNameString",
                wt: 'json',
             }, 
             dataType: "jsonp",
             jsonp: 'json.wrf',

             success: function( data ) {
                response($.map(data.response.docs, function( item ) { 
                    return { 
                        label: item.defaultCommonNameString,
                        value: item.defaultCommonNameString,
                    }; 
            }));
            }
          }); 
         },
        minLength: 1
        });
    });
like image 138
doumeasse38 Avatar answered Nov 15 '22 04:11

doumeasse38