Solr Client 客户端

1.新建Java Project空白工程项目

 
Solr Client 客户端
 

2.引入solr依赖包

 
Solr Client 客户端
 

3.创建Product Beans实体类

 
Solr Client 客户端
 

4.添加com.skg.beans.product字段名称和类型

 注:字段名称和类型要和D:\skg\skg-solr-server\solr-home\product\conf\schema.xml 相同

 
Solr Client 客户端
 

5.创建实体类

package com.skg.beans;

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

public class Product
{
	@Field
	private String id;
	@Field
	private String key;
	@Field
	private String title;
	@Field
	private String sub_title;
	@Field
	private String cid;
	@Field
	private String img;
	@Field
	private Double price;
	@Field
	private String props;
	@Field
	private String text;
	@Field
	private Double area;
	@Field
	private Integer num;
	@Field
	private Long time;
	@Field
	private String status;
	@Field
	private String area_id;
	@Field
	private String city_id;
	@Field
	private String style_id;

	public String getId()
	{
		return id;
	}

	public void setId(String id)
	{
		this.id = id;
	}

	public String getKey()
	{
		return key;
	}

	public void setKey(String key)
	{
		this.key = key;
	}

	public String getTitle()
	{
		return title;
	}

	public void setTitle(String title)
	{
		this.title = title;
	}

	public String getSub_title()
	{
		return sub_title;
	}

	public void setSub_title(String sub_title)
	{
		this.sub_title = sub_title;
	}

	public String getCid()
	{
		return cid;
	}

	public void setCid(String cid)
	{
		this.cid = cid;
	}

	public String getImg()
	{
		return img;
	}

	public void setImg(String img)
	{
		this.img = img;
	}

	public Double getPrice()
	{
		return price;
	}

	public void setPrice(Double price)
	{
		this.price = price;
	}

	public String getProps()
	{
		return props;
	}

	public void setProps(String props)
	{
		this.props = props;
	}

	public String getText()
	{
		return text;
	}

	public void setText(String text)
	{
		this.text = text;
	}

	public Double getArea()
	{
		return area;
	}

	public void setArea(Double area)
	{
		this.area = area;
	}

	public Integer getNum()
	{
		return num;
	}

	public void setNum(Integer num)
	{
		this.num = num;
	}

	public Long getTime()
	{
		return time;
	}

	public void setTime(Long time)
	{
		this.time = time;
	}

	public String getStatus()
	{
		return status;
	}

	public void setStatus(String status)
	{
		this.status = status;
	}

	public String getArea_id()
	{
		return area_id;
	}

	public void setArea_id(String area_id)
	{
		this.area_id = area_id;
	}

	public String getCity_id()
	{
		return city_id;
	}

	public void setCity_id(String city_id)
	{
		this.city_id = city_id;
	}

	public String getStyle_id()
	{
		return style_id;
	}

	public void setStyle_id(String style_id)
	{
		this.style_id = style_id;
	}

}

6.测试新增功能

创建 com.skg.client.AddTest.java

package com.skg.client;

import java.io.IOException;

import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.BinaryRequestWriter;
import org.apache.solr.client.solrj.impl.HttpSolrServer;

import com.skg.beans.Product;

public class AddTest
{
	public static final String SOLR_URL = "http://localhost/solr/product";

	public static void AddBeans() throws IOException, SolrServerException
	{
		System.out.println("**********************添加索引开始*******************************");

		// Http方式连接Solr服务器端
		HttpSolrServer server = new HttpSolrServer(SOLR_URL);
		server.setRequestWriter(new BinaryRequestWriter());

		// 设置实体类
		Product product = new Product();
		product.setId("1000000000001");
		product.setTitle("标题 西部农场厨房定制C000037TY");
		product.setText("内容 skg");
		product.setProps("多属性以' '空格分隔 由IK分词切分,如  12000-10000 main_color:null chufang 爱丽丝田园 厨房 xibunongchang reservations:null 0-25 lx fit_to:享受生活,个性温暖,热情大方\n 西部农场 ailisi 10000-12000 L型 25-0");

		server.addBean(product); // 添加实体类
		server.optimize(); // 保存索引

		System.out.println("查询添加结果:http://localhost/solr/#/product/query ");
		System.out.println("**********************添加索引结束*******************************");
	}

	public static void main(String[] args) throws IOException, SolrServerException
	{
		AddBeans();
	}
}

运行结果:

 
Solr Client 客户端
 

查询结果:http://localhost/solr/#/product/query 

 
Solr Client 客户端
 

7.测试删除功能

创建 com.skg.client.DelTest.java

package com.skg.client;

import java.io.IOException;

import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;

public class DelTest
{
	public static final String SOLR_URL = "http://localhost/solr/product";

	public static void DelBeans() throws SolrServerException, IOException
	{
		System.out.println("**********************删除索引开始*******************************");
		HttpSolrServer server = new HttpSolrServer(SOLR_URL);
		// 删除ID等于1000000000001的记录
		server.deleteById("1000000000001");
		// 保存索引
		server.commit();
		System.out.println("查询添加结果:http://localhost/solr/#/product/query ");
		System.out.println("**********************删除索引开始*******************************");
	}

	public static void main(String[] args) throws IOException, SolrServerException
	{
		DelBeans();
	}
}

运行结果:

 
Solr Client 客户端
 

查询结果:

 
Solr Client 客户端
 

8.测试修改功能

由于SOLR不支持修改功能,解决方法是先删除记录,后添加新记录。

9.测试查询功能

创建 com.skg.client.QueryTest.java

package com.skg.client;

import java.io.IOException;

import org.apache.solr.client.solrj.SolrQuery;
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;

public class QueryTest
{
	public static final String SOLR_URL = "http://localhost/solr/product";

	public static void main(String[] args) throws SolrServerException, IOException
	{
		HttpSolrServer server = new HttpSolrServer(SOLR_URL);
		SolrQuery query = new SolrQuery();
		query.setQuery("title:西部");
		query.setStart(0);
		query.setRows(100);
		QueryResponse response = server.query(query);
		SolrDocumentList list = response.getResults();

		int iRow = 1;
		for (SolrDocument doc : list)
		{
			System.out.println("----------" + iRow + "------------");
			System.out.println("id: " + doc.getFieldValue("id").toString());
			System.out.println("title: " + doc.getFieldValue("title").toString());
			System.out.println("text: " + doc.getFieldValue("text").toString());
			System.out.println("attr: " + doc.getFieldValue("props").toString());
			iRow++;
		}
	}
}

运行结果:

 
Solr Client 客户端
 

相关推荐