使用solrj索引查詢

2021-06-06 20:01:24 字數 1618 閱讀 9436

1、 查詢

solrj提供的查詢功能比較強大,可以進行結果中查詢、範圍查詢、排序等。

下面是筆者封裝的乙個查詢函式:

public static queryresponse search(string field, string key,int start,

int count, string sortfield, boolean flag, boolean hightlight)

if (null == sortfield || null == flag || sortfield.length !=flag.length)

solrquery query = null;

try

//設定起始位置與返回結果數

query.setstart(start);

query.setrows(count);

//設定排序

for(int i=0; i

if (flag[i]) else

}//設定高亮

if (null != hightlight)

} catch (exception e)

queryresponse rsp = null;

try catch (exception e)

//返回查詢結果

return rsp;

}補充一下範圍查詢的格式:[star t toend],start與end是相應資料格式的值的字串形式,「to」 

一定要保持大寫!

2、 讀取查詢結果

6、 讀取查詢結果

doclist讀取返回結果:

solrdocumentlist solrlist = rsp.getresults();

beans讀取返回結果:

listtmplists =rsp.getbeans(item.class);

讀取高亮顯示結果:

rsp = search(field, key, 0, 10, sortfield, flag, true);

if(null == rsp)

map>>hightlight = rsp.gethighlighting();

//item即為上面定義的bean類

listtmplists =rsp.getbeans(item.class);

for (int i = 0; i < tmplists.size(); i++)}

7、 facet的乙個應用:自動補全

//prefix為字首,min為最大返回結果數

public static string autocomplete(string prefix, int min) catch (exception e)

if(null != rsp)

for(int i=0; i

string tmp = countlist.get(i).tostring().split(" ");

//排除單個字

if(tmp[0].length()< 2)

min--;

if(min == 0)

}words = sb.tostring().split(" ");

}else

return words;

}

SolrJ查詢使用,高亮查詢

main方法測試 public static void main string zkhost string defaultcollection collection1 cloudsolrserver solr new cloudsolrserver zkhost solr.setdefaultcol...

使用SolrJ管理索引庫

使用solrj操作索引庫,必須先開啟伺服器。因為操作索引庫的是solr專案,我們只是通過solrj介面向該專案傳送請求,告訴solr專案來操作索引庫。新增的jar包分為兩部分 1 solrj相關jar 2 solr執行的jar包 本質沒有update方法,update就是刪除document後新增乙...

Solr5 3 1 SolrJ查詢索引結果

通過solrj獲取solr檢索結果 1 通過solrparams的方式提交查詢引數 2 通過solrquery提交引數 設定高亮 query.setquery name datou query.sethighlight true 開啟高亮元件或用query.setparam hl true quer...