Java实现对Solr的增删改查以及如何封装一个Query对象

今天写了个JAVA工程来对以及部署在tomcat上的Solr项目进行增删改查操作。

首先先获取到HttpSolrServer

HttpSolrServer httpSolrServer = new HttpSolrServer("http://localhost:8080//collection1");

传进去的参数就是Solr的访问地址。

向solr服务器添加一个文档

public void addSolrIndex() throws Exception, IOException {
		// solr web server
		SolrInputDocument document = new SolrInputDocument();
		document.addField("id", "0001");
		document.addField("content", "哈哈18岁");
		httpSolrServer.add(document);
 
		httpSolrServer.commit();
	}

删除solr服务器中的索引

public void deleteSolrIndex() throws Exception {
		// httpSolrServer.deleteByQuery("*:*");
		httpSolrServer.deleteById("test0001");
		httpSolrServer.commit();
	}

更新solr服务器中的索引

public void updateSolrIndex() throws Exception {
		SolrInputDocument document = new SolrInputDocument();
		document.addField("id", "test0001");
		document.addField("content", "赵老师30岁");
		httpSolrServer.add(document);
		httpSolrServer.commit();
	}

solr查询操作

public void searchSolrIndex() {
		try {
			// 封装查询条件
			SolrQuery query = new SolrQuery();
			query.set("q", "花儿朵朵");
			// 添加过滤条件
			// query.setFilterQueries("product_name:花儿朵朵",
			// "product_catalog_name:辣条");
			String  s = "10-20";
			String[] split = s.split("-");
			query.setFilterQueries("product_price:["+split[0]+" TO "+split[1]+"]");
			// 排序
			query.addSort("product_price", ORDER.desc);
 
			query.setStart(0);
			query.setRows(10);
 
			// 查询返回的域
			// query.setFields("", "");
			// 设置默认查询域
			query.set("df", "product_keywords");
 
			// 设置高亮
			query.setHighlight(true);
			query.addHighlightField("product_name");
			query.setHighlightSimplePre("<em color=\"red\">");
			query.setHighlightSimplePost("</em>");
 
			// 执行查询并返回一个QueryResponse对象
			QueryResponse response = httpSolrServer.query(query);
			SolrDocumentList results = response.getResults();
			// 本次查询命中的文档数量
			long numFound = results.getNumFound();
			System.out.println("numFound = " + numFound);
			
			
			// 获取高亮显示的内容
			Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
 
			for (SolrDocument solrDocument : results) {
				String id = (String) solrDocument.get("id");
				
				String product_name = "";
				List<String> list = highlighting.get(id).get("product_name");
				if (null != list && list.size() > 0) {
					product_name = list.get(0);
				} else {
					product_name = (String) solrDocument.get("product_name");
				}
				
				String product_catalog_name = (String) solrDocument.get("product_catalog_name");
				float product_price = (float) solrDocument.get("product_price");
				String product_picture = (String) solrDocument.get("product_picture");
 
				System.out.println("id = " + id + " \t product_name=" + product_name + " \t product_catalog_name="
						+ product_catalog_name + " \t product_price=" + product_price + " \t product_picture="
						+ product_picture);
			}
		} catch (SolrServerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 
	}

 

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注