import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
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 {
/**
* 获取CommonsHttpSolrServer连接
* @return
*/
public CommonsHttpSolrServer getConnection() {
try {
String url = "http://127.0.0.1:8080/solr";
CommonsHttpSolrServer server = new CommonsHttpSolrServer(url);
server.setSoTimeout(3000); // socket read timeout
server.setConnectionTimeout(1000);
server.setDefaultMaxConnectionsPerHost(1000);
server.setMaxTotalConnections(10);
server.setFollowRedirects(false); // defaults to false
server.setAllowCompression(true);
server.setMaxRetries(1);
return server;
} catch (MalformedURLException e) {
e.printStackTrace();
}
return null;
}
/**
* 向solr中添加索引
*/
@Test
public void addIndexToSolr(){
try {
//创建一个http连接
CommonsHttpSolrServer server = getConnection();
//构造文档和域
SolrInputDocument doc1 = new SolrInputDocument();
SolrInputDocument doc2 = new SolrInputDocument();
//确认schema.xml中是否有id,name,price这几个域
//<field name="id" type="string" indexed="true" stored="true" required="true" />
//<field name="sku" type="text_en_splitting_tight" indexed="true" stored="true" omitNorms="true"/>
//<field name="name" type="text_general" indexed="true" stored="true"/>
//添加域
doc1.addField( "id", "id3", 1.0f );
doc1.addField( "name", "doc3", 1.0f );
doc1.addField( "price", 30 );
doc2.addField( "id", "id4", 2.0f );
doc2.addField( "name", "doc4", 2.0f );
doc2.addField( "price", 40 );
//批量添加文档
Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
docs.add( doc1 );
docs.add( doc2 );
server.add(docs);
server.commit();
}catch (SolrServerException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
/**
* 添加中文信息
*/
@Test
public void addChineseIndexToSolr(){
try {
//创建一个http连接
CommonsHttpSolrServer server = getConnection();
//构造文档和域
SolrInputDocument doc1 = new SolrInputDocument();
SolrInputDocument doc2 = new SolrInputDocument();
//确认schema.xml中是否有id,name,price这几个域
//<field name="id" type="string" indexed="true" stored="true" required="true" />
//<field name="sku" type="text_en_splitting_tight" indexed="true" stored="true" omitNorms="true"/>
//<field name="name" type="text_general" indexed="true" stored="true"/>
//添加域
doc1.addField( "id", "id8");
doc1.addField("msg_title", "java编程技术xx");
doc1.addField( "msg_content","北京人民大会堂");
doc2.addField( "id", "id9");
doc2.addField( "msg_title","lucene in action");
doc2.addField( "msg_content", "泉州市行政服务中心");
//批量添加文档
Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
docs.add( doc1 );
docs.add( doc2 );
server.add(docs);
server.commit();
}catch (SolrServerException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
@Test
/**
* 查询+高亮
*/
public void readDataFormSolr(){
try {
//创建一个http连接
CommonsHttpSolrServer server = getConnection();
SolrQuery query = new SolrQuery();
//请求所有文档
//1、SolrQuery类,此类有个方法setStart(10),当设置为10时,表示从第11记录取,默认取值为0,就是从第,1条开始
//2、setRows(10),表示取出的记录数,默认就是10条
query.setQuery( "msg_content:北京" );
//设置高亮
query.addHighlightField("msg_content");
query.setHighlight(true);
query.setHighlightSimplePre("<span>");
query.setHighlightSimplePost("</span>");
//query.setStart(0);
//query.addSortField( "price", SolrQuery.ORDER.asc );
QueryResponse rsp = server.query( query );
//获取结果
SolrDocumentList docs = rsp.getResults();
System.out.println(docs);
// //遍历,获取结果
Iterator iter = docs.iterator();
//获取高亮结果
Map<String, Map<String, List<String>>> hightlight = rsp.getHighlighting();
while(iter.hasNext()){
//获取文档
SolrDocument document = (SolrDocument)iter.next();
//获取每个域值
String id = (String)document.get("id");
String msg_title = (String)document.get("msg_title");
String hightlightName = "";
if(hightlight!=null){
//获取高亮结果
hightlightName = hightlight.get(id).get("msg_content").toString();
}
String msg_content = (String)document.get("msg_content");
System.out.println("msg_title="+msg_title +" msg_content="+msg_content +" hightlightName="+hightlightName);
}
} catch (SolrServerException e) {
e.printStackTrace();
}
}
/**
* 查询
*/
@Test
public void readDataFormSolr1(){
try {
//创建一个http连接
CommonsHttpSolrServer server = getConnection();
SolrQuery query = new SolrQuery();
//请求所有文档
//1、SolrQuery类,此类有个方法setStart(10),当设置为10时,表示从第11记录取,默认取值为0,就是从第,1条开始
//2、setRows(10),表示取出的记录数,默认就是10条
query.setQuery( "msg_content:北京" );
QueryResponse rsp = server.query( query );
//获取结果
SolrDocumentList docs = rsp.getResults();
System.out.println(docs);
// //遍历,获取结果
Iterator iter = docs.iterator();
while(iter.hasNext()){
//获取文档
SolrDocument document = (SolrDocument)iter.next();
//获取每个域值
String id = (String)document.get("id");
String msg_title = (String)document.get("msg_title");
String msg_content = (String)document.get("msg_content");
System.out.println("msg_title="+msg_title +" msg_content="+msg_content);
}
} catch (SolrServerException e) {
e.printStackTrace();
}
}
}
solrj实现基本的添加索引,查询
最新推荐文章于 2021-03-11 06:12:49 发布