solrJ的简单查询操作

来源:互联网 发布:python socket 连接 编辑:程序博客网 时间:2024/05/01 10:14
package solrj;import java.io.IOException;import java.util.List;import java.util.Map;import javax.swing.event.DocumentListener;import javax.xml.ws.Response;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;import org.apache.solr.common.SolrInputDocument;import org.junit.Test;public class SolrJDemo {        添加查询文档        @Testpublic void insertIndex() throws Exception, Exception {String urlString = "http://192.168.1.102:8080/solr/solrcore1";// 使用solrServer远程连接urlSolrServer solrServer = new HttpSolrServer(urlString);// 创建一个文档对象// 使用solrServer远程连接url// 创建一个文档对象SolrInputDocument doc = new SolrInputDocument();doc.addField("id", "p1010110");doc.addField("product_name", "牙刷真好!!!!");doc.addField("product_price", "1200");solrServer.add(doc);solrServer.commit();}// 删除文档对象@Testpublic void deleteIndex() throws Exception {String url = "http://192.168.1.102:8080/solr/solrcore1";// 使用solrServer远程连接urlSolrServer solrServer = new HttpSolrServer(url);// 创建一个文档对象solrServer.deleteByQuery("id:p1010110");solrServer.commit();}// 查询文档对象@Testpublic void selectIndex() throws Exception {String url = "http://192.168.1.102:8080/solr/solrcore1";// 使用solrServer连接远程urlSolrServer solrServer = new HttpSolrServer(url);// 创建查询的solrQuerySolrQuery solrQuery = new SolrQuery();//根据document的id查询//solrQuery.set("q", "id:2");// 设置查询字段solrQuery.set("q","台灯");//过滤查询字段solrQuery.set("fl", "id,product_name,product_price");//solrQuery.setFields("id,product_name,product_price");//和默认查询字段一起使用solrQuery.set("df","product_keywords" );//设置过滤查询solrQuery.set("fq", "product_price:[10 TO 20]");//solrQuery.addFilterQuery("product_price:[10 TO 20]");//排序solrQuery.set("sort", "product_price  desc");//solrQuery.setSort("product_price",ORDER.asc);//分页solrQuery.setStart(2);solrQuery.setRows(3);//设置高亮//开启高亮solrQuery.setHighlight(true);solrQuery.addHighlightField("product_name");solrQuery.setHighlightSimplePre("<font color='red'>");solrQuery.setHighlightSimplePost("</font>");// 执行查询条件QueryResponse response = solrServer.query(solrQuery);// 获取查询的简略信息SolrDocumentList solrDocumnetList = response.getResults();System.out.println("命中条数:" + solrDocumnetList.getNumFound());for(SolrDocument sdoc : solrDocumnetList){String id = (String) sdoc.get("id");//这个id是哪个id??/String productName = (String) sdoc.get("product_name");float productPrice = (float) sdoc.get("product_price");System.out.println(id+"===="+productName+"===="+productPrice);//获取高亮Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();Map<String, List<String>> map = highlighting.get(id);List<String> list = map.get("product_name");if(list!=null && list.size()>0){for(String s  : list){System.out.println(s);}}}}


0 0
原创粉丝点击