solrj的简单增删改应用

本文介绍如何使用SolrJ进行文档的增删查操作,包括文档的插入、删除及带有高亮显示的复杂查询过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
				}
			}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值