solr json ajax

用ajax去请求solr服务。返回json,然后解释。让solr返回json的参数是wt=json。然后javascript用evel()解释成对象。
我的solr会返回:auother,title,introduce这几个域。先创建一个jsp或html,如:json.jsp

1.json.jsp关键的html内容
   < form  action ="select/"  name ="f1"  method ="get"  onsubmit ="xmlhttpPost('/solr/select'); return false;"  >
      Chenlb: 
      
< input  type ="text"  name ="q"  size ="80"  value ="文档" >
      
< input  name ="start"  type ="hidden"  value ="0" >
    
< input  name ="rows"  type ="hidden"  value ="10" >
    
< input  name ="indent"  type ="hidden"  value ="on" >
    
< input  name ="wt"  type ="hidden"  value ="" >
      
< input  type ="button"  value =" 搜 索 "  onclick ="xmlhttpPost('/solr/select');" >
      
< input  type ="button"  value =" get json "  onclick ="document.forms['f1'].wt.value='json';document.forms['f1'].submit();" >
      
< input  type ="button"  value =" get xml "  onclick ="document.forms['f1'].wt.value='';document.forms['f1'].submit();" >
  
</ form >
  
< div  style ="background-color: #ccccff; height: 15px;" ></ div >
  
< p >
    
< div  id ="header" ></ div >
    
< div  id ="response" ></ div >
    
< table  id ="docs"  class ="tab"  cellspacing ="1" >
        
< tr  height ="25"  style ="background-color: #cccccc; color: #0000ff;" >
            
< td > 作者 </ td >
            
< td > 简介 </ td >
            
< td > 标题 </ td >
            
< td > score </ td >
        
</ tr >
    
</ table >

2.javascript部分
function  xmlhttpPost(strURL) {
    
var  xmlHttpReq  =  false ;
    
var  self  =  this ;
    
if  (window.XMLHttpRequest) {  //  Mozilla/Safari
        self.xmlHttpReq  =  new  XMLHttpRequest(); 
    }
    
else  if  (window.ActiveXObject) {  //  IE
        self.xmlHttpReq  =  new  ActiveXObject( " Microsoft.XMLHTTP " );
    }
    
    
var  params  =  getstandardargs().concat(getquerystring());
    
var  strData  =  params.join(' & ');
    
    
var  header  =  document.getElementById( " response " );
    header.innerHTML 
=  strURL + ' ? ' + strData;

    self.xmlHttpReq.open('get', strURL
+ ' ? ' + strData + ' & time = ' + new  Date().getTime(),  true );
    self.xmlHttpReq.setRequestHeader('Content
- Type', 'application / x - www - form - urlencoded');
    self.xmlHttpReq.onreadystatechange 
=  function () {
        
if  (self.xmlHttpReq.readyState  ==  4 ) {
            updatepage(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send(
null );
}

function  getstandardargs() {
    
var  params  =  [
        'wt
= json'
        , 'indent
= on'
        , 'hl
= true '
        , 'hl.fl
= '
        , 'fl
=* ,score'
        , 'start
= 0 '
        , 'rows
= 10 '
        ];

    
return  params;
}
function  getquerystring() {
  
var  form  =  document.forms['f1'];
  
var  query  =  form.q.value;
  qstr 
=  'q = +  encodeURI(query);     // escape
   return  qstr;
}

//  this function does all the work of parsing the solr response and updating the page.
function  updatepage(str){
  
// document.getElementById("response").innerHTML = str;
   var  rsp  =  eval( " ( " + str + " ) " );  //  use eval to parse Solr's JSON response
  parse(rsp);
}

function  parse(j) {
    
var  header  =  document.getElementById( " header " );
    
var  rh  =  j.responseHeader;
    
var  header_str  =  "  搜索: " "" +rh.params.q+ " " " , 花了:  " + rh.QTime + " ms, 共显示:  " + j.response.numFound + " 条记录, 总共有:  " + rh.params.rows;
    header.innerHTML 
=  header_str;
    
var  docs  =  j.response.docs;
    
var  tab  =  document.getElementById( " docs " );
    
for (; tab.rows.length  > 1 ; ) {
        tab.deleteRow(
- 1 );
    }
    
var  tr;
    
var  td;
    
for ( var  i = 0 ; i < docs.length; i ++ ) {
        tr 
=  tab.insertRow( - 1 );
        td 
=  tr.insertCell( - 1 );
        td.innerHTML 
=  docs[i].author;
        
        td 
=  tr.insertCell( - 1 );
        td.innerHTML 
=  docs[i].introduce;
        
        td 
=  tr.insertCell( - 1 );
        td.innerHTML 
=  docs[i].title;
        
        td 
=  tr.insertCell( - 1 );
        td.innerHTML 
=  docs[i].score;
    }
   }

3.现在可以搜索了。
http://localhost:8080/solr/json.jsp

转载于:https://www.cnblogs.com/wycg1984/archive/2009/09/16/1567584.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值