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 {
//创建查询文档对象
@Test
public void insertIndex() throws Exception, Exception {
String urlString = "http://192.168.1.102:8080/solr/solrcore1";
// 使用solrServer远程连接url
SolrServer 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();
}
// 删除文档对象
@Test
public void deleteIndex() throws Exception {
String url = "http://192.168.1.102:8080/solr/solrcore1";
// 使用solrServer远程连接url
SolrServer solrServer = new HttpSolrServer(url);
// 创建一个文档对象
solrServer.deleteByQuery("id:p1010110");
solrServer.commit();
}
// 查询文档对象
@Test
public void selectIndex() throws Exception {
String url = "http://192.168.1.102:8080/solr/solrcore1";
// 使用solrServer连接远程url
SolrServer solrServer = new HttpSolrServer(url);
// 创建查询的solrQuery
SolrQuery 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);
}
}
}
}
solrj的简单增删改应用
最新推荐文章于 2023-04-20 13:46:42 发布