Solr是一个基于Lucene的Java搜索引擎服务器,当我们需要想百度一样搜索出多条结果,却不需要每条结果的具体内容的时候,这个时候我们需要用到fl参数,
fl指定查询的字段,只要不把内容字段放在里面就行了。
当需要查看某一条的具体内容的时候就需要使用fq参数,用于过滤指定项的id,这样就可以只查出那一条的数据
在查询和解析返回结果我们可以用Solrj gradle地址compile group: 'org.apache.solr', name: 'solr-solrj', version: '4.1.0' 还依赖httpclient包。
package dlp.cn.gat.query.util; import android.os.AsyncTask; import android.util.Log; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.impl.HttpSolrServer; import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.common.SolrDocument; import org.apache.solr.common.params.ModifiableSolrParams; import org.apache.solr.common.util.SimpleOrderedMap; import java.util.Iterator; import java.util.List; import java.util.Map; /** * 作者:徐仕海 * 公司:德宁普科技有限公司 * Created on 2016/7/19 0019. * 用于管理Solr的搜索任务 */ public class SolrManager { private static SolrManager solrManager = new SolrManager(); private SolrManager() { } public static SolrManager getSolrManager() { return solrManager; } private ModifiableSolrParams lastSolrParams = null; QueryResponse lastQueryResponse; /** * 查询solr服务器上的多条数据基本信息,异步的方式 * * @param solrParams 参数 * @param onQueryListener * @param save 如果是分页搜索,就需要将结果保存起来,之后会用到,但是如果是单条查询就是查询某一项的详细数据的话就不要保存起来 */ private void query(final ModifiableSolrParams solrParams, final boolean save,final OnQueryListener onQueryListener) { new AsyncTask<ModifiableSolrParams, Void, QueryResponse>() { @Override protected QueryResponse doInBackground(ModifiableSolrParams... params) { ModifiableSolrParams solrQuery = params[0]; String url = "http://192.168.0.40:8983/solr"; HttpSolrServer solrServer = new HttpSolrServer(url, new DefaultHttpClient()); solrServer.setConnectionTimeout(8000); solrServer.setMaxRetries(3); try { QueryResponse queryResponse = solrServer.query(solrQuery); return queryResponse; } catch (SolrServerException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(QueryResponse queryResponse) { super.onPostExecute(queryResponse); if(save){ if (lastSolrParams == null) { lastSolrParams = solrParams; } /**表示是分页搜索,那结果就要追加到原来的结果上去*/ if (lastQueryResponse != null && lastSolrParams.get("q").equals(solrParams.get("q"))) { lastQueryResponse.getResults().addAll(queryResponse.getResults()); lastQueryResponse.getHighlighting().putAll(queryResponse.getHighlighting()); } else { lastQueryResponse = queryResponse; lastSolrParams = solrParams; } } if (onQueryListener == null) return; if (queryResponse != null) { if(save){ onQueryListener.onQuerySuccess(lastQueryResponse); }else{ onQueryListener.onQuerySuccess(queryResponse); } } else { onQueryListener.onQueryFail(); } } }.execute(solrParams); } /** * 这是用于搜索关键字的,里面不会包含真正的数据,因为数据太大,速度又慢,那些数据又用不到,还浪费用户流量 * * @param keyWord * @param onQueryListener */ public void query(final String keyWord,int start, final OnQueryListener onQueryListener) { ModifiableSolrParams solrQuery = new SolrQuery(keyWord) .set("wt", "xml") .set("fl", "*FileName,lily.id,lily.table,lily.key,lily.vtagId,lily.version") .set("rows", 20) .set("start",start) .set("indent", true) .set("hl", true) .set("hl.fl", "*") .set("hl.simple.pre", "€€※※") .set("hl.simple.post", "※※€€"); query(solrQuery, true, onQueryListener); } /** * 这是用于检索指定key的单条记录的,会包含真正的数据,用于用户查看详细信息 * * @param onQueryListener */ public void queryDoc(String keyWord, SolrDocument solrDocument, final OnQueryListener onQueryListener) { ModifiableSolrParams solrQuery = new SolrQuery(keyWord) .set("wt", "xml") .set("fq", "lily.id:" + solrDocument.getFieldValue("lily.id")) .set("rows", 1) .set("indent", true) .set("hl", true) .set("hl.fl", "*") .set("hl.simple.pre", "€€※※") .set("hl.simple.post", "※※€€"); query(solrQuery, false, onQueryListener); } /** * 简化用户查看文件的详情的过程 * @param position * @param onQueryListener */ public void queryDoc(int position,final OnQueryListener onQueryListener){ if(lastQueryResponse==null){ Log.e("Solr","lastQueryResponse为空"); return; } try{ SolrDocument solrDocument = lastQueryResponse.getResults().get(position); String keyWord = ((SimpleOrderedMap<String>) lastQueryResponse.getHeader().get("params")).get("q"); queryDoc(keyWord,solrDocument,onQueryListener); }catch (Exception e){ e.printStackTrace(); Log.e("Solr", "获取参数失败"); } } /** * 读取SolrDocument中字段以FileName结尾的值 * @param solrDocument * @return */ public static String getFileName(SolrDocument solrDocument){ if(solrDocument==null)return ""; Iterator<Map.Entry<String,Object>> iterator = solrDocument.iterator(); while (iterator.hasNext()){ Map.Entry<String,Object> entry = iterator.next(); if(entry.getKey().endsWith("FileName")){ return (String)entry.getValue(); } } return ""; } /** * 读取SolrDocument中字段以FileName结尾的字段 * @param solrDocument * @return */ public static String getFileNameKey(SolrDocument solrDocument){ if(solrDocument==null)return ""; Iterator<Map.Entry<String,Object>> iterator = solrDocument.iterator(); while (iterator.hasNext()){ Map.Entry<String,Object> entry = iterator.next(); if(entry.getKey().endsWith("FileName")){ return entry.getKey(); } } return ""; } /** * 读取SolrDocument中字段以Content结尾的值 * @param solrDocument * @return */ public static String getFileContent(SolrDocument solrDocument){ if(solrDocument==null)return ""; Iterator<Map.Entry<String,Object>> iterator = solrDocument.iterator(); while (iterator.hasNext()){ Map.Entry<String,Object> entry = iterator.next(); if(entry.getKey().endsWith("Content")){ return (String)entry.getValue(); } } return ""; } /** * 读取SolrDocument中字段以Content结尾的值 * @param solrDocument * @return */ public static List<String> getFileContent(Map<String, List<String>> solrDocument){ if(solrDocument==null)return null; Iterator<Map.Entry<String, List<String>>> iterator = solrDocument.entrySet().iterator(); while (iterator.hasNext()){ Map.Entry<String,List<String>> entry = iterator.next(); if(entry.getKey().endsWith("Content")){ return entry.getValue(); } } return null; } public QueryResponse getQueryResponse(){ return lastQueryResponse; } public interface OnQueryListener { void onQuerySuccess(QueryResponse queryResponse); void onQueryFail(); } }