Solr之客户端SolrJ

SolrJ是操作Solr的JAVA客户端,它提供了增加、修改、删除、查询Solr索引的JAVA接口。SolrJ针对Solr提供了Rest的HTTP接口进行了封装,SolrJ底层是通过使用httpClient中的方法来完成Solr的操作。SolrJ通常向后保持兼容性,可以使用新版本的SolrJ访问较旧的Solr,反之亦然。建议使用同Solr server同版本的SolrJ。
引入依赖包:

<dependency>
			<groupId>org.apache.solr</groupId>
			<artifactId>solr-solrj</artifactId>
			<version>5.2.1</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.7</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>jcl-over-slf4j</artifactId>
			<version>1.7.7</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
一、普通方式
1.增加、删除索引

package cn.slimsmart.solr.demo.solrj;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.solr.client.solrj.impl.HttpSolrClient;

// 采用SolrInputDocument对象增加、删除索引
public class Test {

	static String URL = "http://192.168.18.119:9080/solr/test";

	public static void main(String[] args) {
		addDocs();
		//delDocs();
	}

	public static void addDocs() {
		long start = System.currentTimeMillis();
		Collection<Student> docs = new ArrayList<Student>();
		for (int i = 1; i < 50; i++) {
			Student s = new Student();
			//各属性的名称在conf/schema.xml中存在
			s.setId("efgh00"+i);
			s.setName("li si" + i);
			s.setAge(3 * i);
			s.setDesc(i+"今天天气很好");
			docs.add(s);
		}
		try {
			HttpSolrClient client = new HttpSolrClient(URL);
			client.addBeans(docs);
			client.optimize();
			client.commit();
			client.close();
		} catch (Exception e) {
			System.out.println(e);
		}
		System.out.println("cost time:" + (System.currentTimeMillis() - start));
	}

	public static void delDocs() {
		long start = System.currentTimeMillis();
		try {
			HttpSolrClient client = new HttpSolrClient(URL);
			List<String> ids = new ArrayList<String>();
			for (int i = 1; i < 10; i++) {
				ids.add("efgh00" + i);
			}
			client.deleteById(ids);
			client.commit();
			client.close();
		} catch (Exception e) {
			System.out.println(e);
		}
		System.out.println("cost time:" + (System.currentTimeMillis() - start));
	}
}
2.处理查询

package cn.slimsmart.solr.demo.solrj;

import java.io.IOException;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.BinaryRequestWriter;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;

//普通方式处理查询结果
public class QueryDocsTest {
	
	static String URL = "http://192.168.18.119:9080/solr/test";
	public static void main(String[] args) throws SolrServerException, IOException {
		HttpSolrClient client = new HttpSolrClient(URL);
		client.setConnectionTimeout(5000);
		 //正常情况下,以下参数无须设置
        //使用老版本solrj操作新版本的solr时,因为两个版本的javabin incompatible,所以需要设置Parser
		client.setParser(new XMLResponseParser());
		client.setSoTimeout(1000); // socket read timeout
		client.setDefaultMaxConnectionsPerHost(100);
		client.setMaxTotalConnections(100);
		client.setFollowRedirects(false); // defaults to false
        // allowCompression defaults to false.
        // Server side must support gzip or deflate for this to have any effect.
		client.setAllowCompression(true);
		
		//使用ModifiableSolrParams传递参数
		/*  ModifiableSolrParams params = new ModifiableSolrParams();
      // http://192.168.18.119:9080/solr/test/select?q=context%3A%E4%BB%8A%E5%A4%A9&wt=json&indent=true
      // 设置参数,实现上面URL中的参数配置
      // 查询关键词
      params.set("q", "context:今天");
      // 返回信息
      params.set("fl", "id,name,age,desc");
      // 排序
      params.set("sort", "id asc");
      // 分页,start=0就是从0开始,rows=5当前返回5条记录,第二页就是变化start这个值为5就可以了
      params.set("start", 0);
      params.set("rows", 5);
      // 返回格式
      params.set("wt", "javabin");
      QueryResponse response = client.query(params);*/
		
		
        //使用SolrQuery传递参数,SolrQuery的封装性更好
		client.setRequestWriter(new BinaryRequestWriter());
        SolrQuery query = new SolrQuery();
        query.setQuery("context:今天");
        query.setFields("id","name","age","desc");
        query.setSort("id", ORDER.asc);
        query.setStart(0);
        query.setRows(2);
//      query.setRequestHandler("/select");
        QueryResponse response = client.query(query);
         
        // 搜索得到的结果数
        System.out.println("Find:" + response.getResults().getNumFound());
        // 输出结果
        int iRow = 1;
        for (SolrDocument doc : response.getResults()) {
            System.out.println("----------" + iRow + "------------");
            System.out.println("id: " + doc.getFieldValue("id").toString());
            System.out.println("name: " + doc.getFieldValue("name").toString());
            System.out.println("age: "+ doc.getFieldValue("age").toString());
            System.out.println("desc: " + doc.getFieldValue("desc"));
            iRow++;
        }
        client.close();
	}
}
二、java对象方式
创建java对象

package cn.slimsmart.solr.demo.solrj;

import org.apache.solr.client.solrj.beans.Field;

public class Student {
	
	@Field
	private String id;
	@Field
	private String name;
	@Field
	private int age;
	@Field
	private String desc;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getDesc() {
		return desc;
	}
	public void setDesc(String desc) {
		this.desc = desc;
	}
}
1.增加、删除索引

package cn.slimsmart.solr.demo.solrj;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.common.SolrInputDocument;

// 采用POJOs增加、删除索引
public class Test1 {

	static String URL = "http://192.168.18.119:9080/solr/test";

	public static void main(String[] args) {
		addDocs();
		delDocs();
	}

	public static void addDocs() {
		long start = System.currentTimeMillis();
		Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
		for (int i = 1; i < 50; i++) {
			SolrInputDocument doc1 = new SolrInputDocument();
			//各属性的名称在conf/schema.xml中存在
			doc1.addField("id", "abcd00" + i, 1.0f);
			doc1.addField("name", "zhan shan" + i, 1.0f);
			doc1.addField("age", 2 * i);
			doc1.addField("desc", "Apache Devicemap is a data repository containing devices attributes " + i);
			docs.add(doc1);
		}
		try {
			HttpSolrClient client = new HttpSolrClient(URL);
			client.add(docs.iterator());
			client.commit();
			client.close();
		} catch (Exception e) {
			System.out.println(e);
		}
		System.out.println("cost time:" + (System.currentTimeMillis() - start));
	}

	public static void delDocs() {
		long start = System.currentTimeMillis();
		try {
			HttpSolrClient client = new HttpSolrClient(URL);
			List<String> ids = new ArrayList<String>();
			for (int i = 1; i < 10; i++) {
				ids.add("abcd00" + i);
			}
			client.deleteById(ids);
			client.commit();
			client.close();
		} catch (Exception e) {
			System.out.println(e);
		}
		System.out.println("cost time:" + (System.currentTimeMillis() - start));
	}
}
2.处理查询
package cn.slimsmart.solr.demo.solrj;

import java.io.IOException;
import java.util.List;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.beans.DocumentObjectBinder;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;

//采用POJOs方式处理查询结果
public class QueryDocsTest1 {
	
	static String URL = "http://192.168.18.119:9080/solr/test";
	public static void main(String[] args) throws SolrServerException, IOException {
		HttpSolrClient client = new HttpSolrClient(URL);
		client.setConnectionTimeout(5000);
		 //正常情况下,以下参数无须设置
        //使用老版本solrj操作新版本的solr时,因为两个版本的javabin incompatible,所以需要设置Parser
		client.setParser(new XMLResponseParser());
		client.setSoTimeout(1000); // socket read timeout
		client.setDefaultMaxConnectionsPerHost(100);
		client.setMaxTotalConnections(100);
		client.setFollowRedirects(false); // defaults to false
        // allowCompression defaults to false.
        // Server side must support gzip or deflate for this to have any effect.
		client.setAllowCompression(true);
		
		SolrQuery query = new SolrQuery();
        query.setQuery("context:今天");
        query.setStart(0);
        query.setRows(2);
 
        QueryResponse response = client.query(query);
        // 搜索得到的结果数
        System.out.println("Find:" + response.getResults().getNumFound());
        // 输出结果
        int iRow = 1;
         
        SolrDocumentList list = response.getResults();
        DocumentObjectBinder binder = new DocumentObjectBinder();
        List<Student> beanList=binder.getBeans(Student.class, list);
        for(Student s:beanList){
            System.out.println(s.getId());
        }
        for (SolrDocument doc : response.getResults()) {
            System.out.println("----------" + iRow + "------------");
            System.out.println("id: " + doc.getFieldValue("id").toString());
            System.out.println("name: " + doc.getFieldValue("name").toString());
            iRow++;
        }
        client.close();
	}
}

另外,除了使用java客户端外,我们可以通过http请求管理索引,例如:

Solr 删除全部索引:http://localhost:8080/solr/update/?stream.body=<delete><query>*:*</query></delete>&stream.contentType=text/xml;charset=utf-8&commit=true
solr 删除指定ID的索引:http://localhost:8080/solr/update/?stream.body=<delete><id>1</id></delete>&stream.contentType=text/xml;charset=utf-8&commit=true

参考管理页面:http://192.168.18.119:9080/solr/admin.html#/test/documents

Solr之客户端SolrJ_第1张图片

参考文章:

1.官方文档
2.Solr JAVA客户端SolrJ 4.9使用示例教程

3.Solr分页查询代码,包含查询,分页,高亮及获取高亮处摘要代码

你可能感兴趣的:(Solr,Lucene,全文检索)