import java.util.List;
import java.util.Map;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.apache.solr.client.solrj.SolrServer;
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.SolrDocumentList;
public class TestMyMannager {
public void queryIndex() throws Exception {
//创建连接
SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr");
//创建一个query对象
SolrQuery solrQuery = new SolrQuery();
//设置默认的查询范围
solrQuery.set("df", "product_name");
//设置查询条件
solrQuery.setQuery("台灯");
//设置过滤条件
solrQuery.setFilterQueries("product_catalog_name:幽默杂货");
//设置排序条件
solrQuery.setSort("product_price",ORDER.desc);
//分页处理
//开始索引为0
solrQuery.setStart(0);
//每页显示多少行
solrQuery.setRows(8);
//设置高亮(开启)
solrQuery.setHighlight(true);
//设置高亮域
solrQuery.addHighlightField("product_name");
//设置高亮前缀
solrQuery.setHighlightSimplePre("<em>");
//设置高亮后缀
solrQuery.setHighlightSimplePost("</em>");
//执行语句
QueryResponse query = solrServer.query(solrQuery);
//取到查询结果集
SolrDocumentList results = query.getResults();
//共查询到商品数量
long numFound = results.getNumFound();
//遍历结果
String productname="";
for (SolrDocument doc : results) {
Map<String, Map<String, List<String>>> highlighting = query.getHighlighting();
List<String> list = highlighting.get(doc.get("id")).get("product_name");
if (list!=null&&list.size()>0) {
productname = list.get(0);
}else{
productname=(String) doc.get("product_name");
}
System.out.println(doc.get("id"));
System.out.println(doc.get("product_name"));
}
}
}