sorl 增加删除修改更新

阅读更多
package com.zhen.course.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.request.UpdateRequest;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;

public class SolrJTest {
	SolrClient solrclient;

	public SolrJTest(String solrURL) {

		this.solrclient = new HttpSolrClient(solrURL);
	}

	public void close() {
		try {
			solrclient.close();
		} catch (IOException e) {

			e.printStackTrace();
		}
	}

	@Test
	public void addDucument() throws IOException {
		Collection docs = new ArrayList();
		for (int i = 1; i < 100; i++) {
			SolrInputDocument doc = new SolrInputDocument();
			doc.addField("id", i * 100);
			doc.addField("name", "搜狐网" + i);
			doc.addField("price", 100 * i);
			doc.addField("text", "搜狗社区" + i);
			docs.add(doc);
		}
		try {
			solrclient.add("sohu", docs);
			solrclient.commit("sohu");
		} catch (SolrServerException e) {
			e.printStackTrace();
		}
	}

	@Test
	public void queryDocuments() {
		SolrQuery params = new SolrQuery();
		params.set("q", "*:*");
		params.set("start", 0);
		params.set("rows", 8);
		params.set("sort", "id asc");
		try {
			QueryResponse rsp = solrclient.query(params);
			SolrDocumentList docs = rsp.getResults();
			System.out.println("params:" + params);
			System.out.println("num:" + docs.getNumFound());
			System.out.println("time:" + rsp.getQTime());
			for (SolrDocument doc : docs) {
				int id = (int) doc.getFieldValue("id");
				String name = (String) doc.getFieldValue("name");
				String price = String.valueOf(doc.getFieldValue("price"));
				String text = (String) doc.getFieldValue("text");
				System.out.println("id:" + id + " name:" + name + " price:" + price + " text:" + text);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Test
	public void deleteById(String id) {
		try {
			UpdateResponse rsp = solrclient.deleteById(id);
			solrclient.commit();
			System.out.println("delete id:" + id + " result:" + rsp.getStatus() + " Qtime:" + rsp.getQTime());
		} catch (SolrServerException | IOException e) {
			e.printStackTrace();
		}
	}

	public void deleteByQuery(String query) {

		try {
			UpdateRequest commit = new UpdateRequest();
			commit.deleteByQuery(query);
			commit.setCommitWithin(1000);// 在毫秒内提交
			commit.process(solrclient);
		} catch (SolrServerException | IOException e) {
			e.printStackTrace();
		}

	}

	public void deleteByQuery2(String query) {

		try {

			solrclient.deleteByQuery("sohu", query);
			solrclient.commit("sohu");
		} catch (SolrServerException | IOException e) {
			e.printStackTrace();
		}

	}

	public void updateDocuments(int id, String fieldName, Object fieldValue) {
		HashMap oper = new HashMap();
		// 多值更新方法
		// List mulitValues = new ArrayList();
		// mulitValues.add(fieldName);
		// mulitValues.add((String)fieldValue);
		oper.put("set", fieldValue);

		SolrInputDocument doc = new SolrInputDocument();
		doc.addField("id", id);
		doc.addField(fieldName, oper);
		try {
			solrclient.add("sohu", doc);
			solrclient.commit("sohu");

		} catch (SolrServerException | IOException e) {
			e.printStackTrace();
		}

	}

	public static void main(String args[]) throws IOException {

		SolrJTest test = new SolrJTest("http://localhost:8080/solr");

		// add
		test.addDucument();

		// delete
		// test.deleteById("200");
		test.deleteByQuery("name:user3");
		// update
		test.updateDocuments(1, "name", "搜狐视频");

		// query
		test.queryDocuments();

		test.close();
	}
}

 

你可能感兴趣的:(solr)