您的位置:首页 > 编程语言

solr实战代码事例

2016-01-28 09:03 323 查看

三、利用SolrJ操作solrAPI,完成index操作

使用SolrJ操作Solr会比利用httpClient来操作Solr要简单。SolrJ是封装了httpClient方法,来操作solr的API的。SolrJ底层还是通过使用httpClient中的方法来完成Solr的操作。

1、首先,你需要添加如下jar包





其中apache-solr-solrj-3.4.0.jar、slf4j-api-1.6.1.jar可以在下载的apache-solr-3.4.0的压缩包中的dist中能找到。



2、其次,建立一个简单的测试类,完成Server对象的相关方法的测试工作,代码如下:

packagecom.hoo.test;


importjava.io.IOException;

importjava.net.MalformedURLException;

importjava.util.ArrayList;

importjava.util.Collection;

importjava.util.List;

importorg.apache.solr.client.solrj.SolrQuery;

importorg.apache.solr.client.solrj.SolrServer;

importorg.apache.solr.client.solrj.SolrServerException;

importorg.apache.solr.client.solrj.impl.CommonsHttpSolrServer;

importorg.apache.solr.client.solrj.response.QueryResponse;

importorg.apache.solr.client.solrj.response.UpdateResponse;

importorg.apache.solr.common.SolrDocumentList;

importorg.apache.solr.common.SolrInputDocument;

importorg.apache.solr.common.params.ModifiableSolrParams;

importorg.apache.solr.common.params.SolrParams;

importorg.junit.After;

importorg.junit.Before;

importorg.junit.Test;

importcom.hoo.entity.Index;


/**

*<b>function:</b>ServerTestCase

*@authorhoojo

*@createDate2011-10-19下午01:49:07

*@fileServerTest.java

*@packagecom.hoo.test

*@projectSolrExample

*@blog'target='_blank'>http://blog.csdn.net/IBM_hoojo[/code]
*@emailhoojo_@126.com

*@version1.0

*/

publicclassServerTest{


privateSolrServerserver;

privateCommonsHttpSolrServerhttpServer;


privatestaticfinalStringDEFAULT_URL="http://localhost:8983/solr/";


@Before

publicvoidinit(){

try{

server=newCommonsHttpSolrServer(DEFAULT_URL);

httpServer=newCommonsHttpSolrServer(DEFAULT_URL);

}catch(MalformedURLExceptione){

e.printStackTrace();

}

}


@After

publicvoiddestory(){

server=null;

httpServer=null;

System.runFinalization();

System.gc();

}


publicfinalvoidfail(Objecto){

System.out.println(o);

}


/**

*<b>function:</b>测试是否创建server对象成功

*@authorhoojo

*@createDate2011-10-21上午09:48:18

*/

@Test

publicvoidserver(){

fail(server);

fail(httpServer);

}


/**

*<b>function:</b>根据query参数查询索引

*@authorhoojo

*@createDate2011-10-21上午10:06:39

*@paramquery

*/

publicvoidquery(Stringquery){

SolrParamsparams=newSolrQuery(query);


try{

QueryResponseresponse=server.query(params);


SolrDocumentListlist=response.getResults();

for(inti=0;i<list.size();i++){

fail(list.get(i));

}

}catch(SolrServerExceptione){

e.printStackTrace();

}

}

}


测试运行servercase方法,如果成功创建对象,那你就成功的链接到。

注意:在运行本方法之前,请启动你的solr官方自动的项目。http://localhost:8983/solr/保证能够成功访问这个工程。因为接下来的所有工作都是围绕这个solr工程完成的。如果你现在还不知道,怎么部署、发布官方solr工程,请参考前面的具体章节。



3、Server的有关配置选项参数,server是CommonsHttpSolrServer的实例

server.setSoTimeout(1000);//socketreadtimeout

server.setConnectionTimeout(100);

server.setDefaultMaxConnectionsPerHost(100);

server.setMaxTotalConnections(100);

server.setFollowRedirects(false);//defaultstofalse

//allowCompressiondefaultstofalse.

//Serversidemustsupportgzipordeflateforthistohaveanyeffect.

server.setAllowCompression(true);

server.setMaxRetries(1);//defaultsto0.>1notrecommended.


//sorlrJ目前使用二进制的格式作为默认的格式。对于solr1.2的用户通过显示的设置才能使用XML格式。

server.setParser(newXMLResponseParser());


//二进制流输出格式

//server.setRequestWriter(newBinaryRequestWriter());




4、利用SolrJ完成IndexDocument的添加操作

/**

*<b>function:</b>添加doc文档

*@authorhoojo

*@createDate2011-10-21上午09:49:10

*/

@Test

publicvoidaddDoc(){

//创建doc文档

SolrInputDocumentdoc=newSolrInputDocument();

doc.addField("id",1);

doc.addField("name","SolrInputDocument");

doc.addField("manu","thisisSolrInputDocumentcontent");


try{

//添加一个doc文档

UpdateResponseresponse=server.add(doc);

fail(server.commit());//commit后才保存到索引库

fail(response);

fail("querytime:"+response.getQTime());

fail("ElapsedTime:"+response.getElapsedTime());

fail("status:"+response.getStatus());

}catch(SolrServerExceptione){

e.printStackTrace();

}catch(IOExceptione){

e.printStackTrace();

}

query("name:solr");

}


在apache-solr-3.4.0\example\solr\conf目录下的schema.xml中可以找到有关于field属性的配置,schema.xml中的field就和上面Document文档中的field(id、name、manu)对应。如果出现ERROR:unknownfield'xxxx'就表示你设置的这个field在schema.xml中不存在。如果一定要使用这个field,请你在schema.xml中进行filed元素的配置。具体请参考前面的章节。

注意:在schema.xml中配置了uniqueKey为id,就表示id是唯一的。如果在添加Document的时候,id重复添加。那么后面添加的相同id的doc会覆盖前面的doc,类似于update更新操作,而不会出现重复的数据。



5、利用SolrJ添加多个Document,即添加文档集合

/**

*<b>function:</b>添加docs文档集合

*@authorhoojo

*@createDate2011-10-21上午09:55:01

*/

@Test

publicvoidaddDocs(){

Collection<SolrInputDocument>docs=newArrayList<SolrInputDocument>();


SolrInputDocumentdoc=newSolrInputDocument();

doc.addField("id",2);

doc.addField("name","SolrInputDocuments1");

doc.addField("manu","thisisSolrInputDocuments1content");


docs.add(doc);


doc=newSolrInputDocument();

doc.addField("id",3);

doc.addField("name","SolrInputDocuments2");

doc.addField("manu","thisisSolrInputDocuments3content");


docs.add(doc);


try{

//adddocs

UpdateResponseresponse=server.add(docs);

//commit后才保存到索引库

fail(server.commit());

fail(response);

}catch(SolrServerExceptione){

e.printStackTrace();

}catch(IOExceptione){

e.printStackTrace();

}

query("solr");

}


就是添加一个List集合



6、添加JavaEntityBean,这个需要先创建一个JavaBean,然后来完成添加操作;

JavaBean:Index的代码

packagecom.hoo.entity;


importorg.apache.solr.client.solrj.beans.Field;


/**

*<b>function:</b>JavaEntityBean;Index需要添加相关的Annotation注解,便于告诉solr哪些属性参与到index中

*@authorhoojo

*@createDate2011-10-19下午05:33:27

*@fileIndex.java

*@packagecom.hoo.entity

*@projectSolrExample

*@blog'target='_blank'>http://blog.csdn.net/IBM_hoojo[/code]
*@emailhoojo_@126.com

*@version1.0

*/

publicclassIndex{

//@Fieldsetter方法上添加Annotation也是可以的

privateStringid;

@Field

privateStringname;

@Field

privateStringmanu;

@Field

privateString[]cat;


@Field

privateString[]features;

@Field

privatefloatprice;

@Field

privateintpopularity;

@Field

privatebooleaninStock;


publicStringgetId(){

returnid;

}


@Field

publicvoidsetId(Stringid){

this.id=id;

}

//getter、setter方法


publicStringtoString(){

returnthis.id+"#"+this.name+"#"+this.manu+"#"+this.cat;

}

}


注意上面的属性是和在apache-solr-3.4.0\example\solr\conf目录下的schema.xml中可以找到有关于field属性的配置对应的。如果你IndexJavaBean中出现的属性在schema.xml的field配置无法找到,那么出出现unknownfiled错误。

添加Bean完成doc添加操作

/**

*<b>function:</b>添加JavaEntityBean

*@authorhoojo

*@createDate2011-10-21上午09:55:37

*/

@Test

publicvoidaddBean(){

//Index需要添加相关的Annotation注解,便于告诉solr哪些属性参与到index中

Indexindex=newIndex();

index.setId("4");

index.setName("addbeanindex");

index.setManu("indexbeanmanu");

index.setCat(newString[]{"a1","b2"});


try{

//添加IndexBean到索引库

UpdateResponseresponse=server.addBean(index);

fail(server.commit());//commit后才保存到索引库

fail(response);

}catch(SolrServerExceptione){

e.printStackTrace();

}catch(IOExceptione){

e.printStackTrace();

}

queryAll();

}




7、添加Bean集合

/**

*<b>function:</b>添加EntityBean集合到索引库

*@authorhoojo

*@createDate2011-10-21上午10:00:55

*/

@Test

publicvoidaddBeans(){

Indexindex=newIndex();

index.setId("6");

index.setName("addbeansindex1");

index.setManu("indexbeansmanu1");

index.setCat(newString[]{"a","b"});


List<Index>indexs=newArrayList<Index>();

indexs.add(index);


index=newIndex();

index.setId("5");

index.setName("addbeansindex2");

index.setManu("indexbeansmanu2");

index.setCat(newString[]{"aaa","bbbb"});

indexs.add(index);

try{

//添加索引库

UpdateResponseresponse=server.addBeans(indexs);

fail(server.commit());//commit后才保存到索引库

fail(response);

}catch(SolrServerExceptione){

e.printStackTrace();

}catch(IOExceptione){

e.printStackTrace();

}

queryAll();

}




8、删除索引Document

/**

*<b>function:</b>删除索引操作

*@authorhoojo

*@createDate2011-10-21上午10:04:28

*/

@Test

publicvoidremove(){

try{

//删除id为1的索引

server.deleteById("1");

server.commit();

query("id:1");


//根据id集合,删除多个索引

List<String>ids=newArrayList<String>();

ids.add("2");

ids.add("3");

server.deleteById(ids);

server.commit(true,true);

query("id:3id:2");


//删除查询到的索引信息

server.deleteByQuery("id:4id:6");

server.commit(true,true);

queryAll();


}catch(SolrServerExceptione){

e.printStackTrace();

}catch(IOExceptione){

e.printStackTrace();

}

}




9、查询索引

/**

*<b>function:</b>查询所有索引信息

*@authorhoojo

*@createDate2011-10-21上午10:05:38

*/

@Test

publicvoidqueryAll(){

ModifiableSolrParamsparams=newModifiableSolrParams();

//查询关键词,*:*代表所有属性、所有值,即所有index

params.set("q","*:*");

//分页,start=0就是从0开始,,rows=5当前返回5条记录,第二页就是变化start这个值为5就可以了。

params.set("start",0);

params.set("rows",Integer.MAX_VALUE);


//排序,,如果按照id排序,,那么将scoredesc改成iddesc(orasc)

params.set("sort","scoredesc");


//返回信息*为全部这里是全部加上score,如果不加下面就不能使用score

params.set("fl","*,score");


try{

QueryResponseresponse=server.query(params);


SolrDocumentListlist=response.getResults();

for(inti=0;i<list.size();i++){

fail(list.get(i));

}

}catch(SolrServerExceptione){

e.printStackTrace();

}

}




10、其他和Server有关方法

/**

*<b>function:</b>其他server相关方法测试

*@authorhoojo

*@createDate2011-10-21上午10:02:03

*/

@Test

publicvoidotherMethod(){

fail(server.getBinder());

try{

fail(server.optimize());//合并索引文件,可以优化索引、提供性能,但需要一定的时间

fail(server.ping());//ping服务器是否连接成功


Indexindex=newIndex();

index.setId("299");

index.setName("addbeanindex199");

index.setManu("indexbeanmanu199");

index.setCat(newString[]{"a199","b199"});


UpdateResponseresponse=server.addBean(index);

fail("response:"+response);


queryAll();

//回滚掉之前的操作,rollbackaddBeanoperation

fail("rollback:"+server.rollback());

//提交操作,提交后无法回滚之前操作;发现addBean没有成功添加索引

fail("commit:"+server.commit());

queryAll();

}catch(SolrServerExceptione){

e.printStackTrace();

}catch(IOExceptione){

e.printStackTrace();

}

}




11、文档查询

/**

*<b>function:</b>query基本用法测试

*@authorhoojo

*@createDate2011-10-20下午04:44:28

*/

@Test

publicvoidqueryCase(){

//AND并且

SolrQueryparams=newSolrQuery("name:appleANDmanu:inc");


//OR或者

params.setQuery("name:appleORmanu:apache");

//空格等同于OR

params.setQuery("name:servermanu:dell");


//params.setQuery("name:solr-manu:inc");

//params.setQuery("name:server+manu:dell");


//查询name包含solrapple

params.setQuery("name:solr,apple");

//manu不包含inc

params.setQuery("name:solr,appleNOTmanu:inc");


//50<=price<=200

params.setQuery("price:[50TO200]");

params.setQuery("popularity:[5TO6]");

//params.setQuery("price:[50TO200]-popularity:[5TO6]");

//params.setQuery("price:[50TO200]+popularity:[5TO6]");


//50<=price<=200AND5<=popularity<=6

params.setQuery("price:[50TO200]ANDpopularity:[5TO6]");

params.setQuery("price:[50TO200]ORpopularity:[5TO6]");


//过滤器查询,可以提高性能filter类似多个条件组合,如and

//params.addFilterQuery("id:VA902B");

//params.addFilterQuery("price:[50TO200]");

//params.addFilterQuery("popularity:[*TO5]");

//params.addFilterQuery("weight:*");

//0<popularity<6没有等于

//params.addFilterQuery("popularity:{0TO6}");


//排序

params.addSortField("id",ORDER.asc);


//分页:start开始页,rows每页显示记录条数

//params.add("start","0");

//params.add("rows","200");

//params.setStart(0);

//params.setRows(200);


//设置高亮

params.setHighlight(true);//开启高亮组件

params.addHighlightField("name");//高亮字段

params.setHighlightSimplePre("<fontcolor='red'>");//标记,高亮关键字前缀

params.setHighlightSimplePost("</font>");//后缀

params.setHighlightSnippets(1);//结果分片数,默认为1

params.setHighlightFragsize(1000);//每个分片的最大长度,默认为100


//分片信息

params.setFacet(true)

.setFacetMinCount(1)

.setFacetLimit(5)//段

.addFacetField("name")//分片字段

.addFacetField("inStock");


//params.setQueryType("");


try{

QueryResponseresponse=server.query(params);


/*List<Index>indexs=response.getBeans(Index.class);

for(inti=0;i<indexs.size();i++){

fail(indexs.get(i));

}*/


//输出查询结果集

SolrDocumentListlist=response.getResults();

fail("queryresultnums:"+list.getNumFound());

for(inti=0;i<list.size();i++){

fail(list.get(i));

}


//输出分片信息

List<FacetField>facets=response.getFacetFields();

for(FacetFieldfacet:facets){

fail(facet);

List<Count>facetCounts=facet.getValues();

for(FacetField.Countcount:facetCounts){

System.out.println(count.getName()+":"+count.getCount());

}

}

}catch(SolrServerExceptione){

e.printStackTrace();

}

}




12、分片查询、统计

/**

*<b>function:</b>分片查询,可以统计关键字及出现的次数、或是做自动补全提示

*@authorhoojo

*@createDate2011-10-20下午04:54:25

*/

@Test

publicvoidfacetQueryCase(){

SolrQueryparams=newSolrQuery("*:*");


//排序

params.addSortField("id",ORDER.asc);


params.setStart(0);

params.setRows(200);


//Facet为solr中的层次分类查询

//分片信息

params.setFacet(true)

.setQuery("*:*")

.setFacetMinCount(1)

.setFacetLimit(5)//段

//.setFacetPrefix("electronics","cat")

.setFacetPrefix("cor")//查询manu、name中关键字前缀是cor的

.addFacetField("manu")

.addFacetField("name");//分片字段


try{

QueryResponseresponse=server.query(params);


//输出查询结果集

SolrDocumentListlist=response.getResults();

fail("Queryresultnums:"+list.getNumFound());


for(inti=0;i<list.size();i++){

fail(list.get(i));

}


fail("Allfacetfiledresult:");

//输出分片信息

List<FacetField>facets=response.getFacetFields();

for(FacetFieldfacet:facets){

fail(facet);

List<Count>facetCounts=facet.getValues();

for(FacetField.Countcount:facetCounts){

//关键字-出现次数

fail(count.getName()+":"+count.getCount());

}

}


fail("Searchfacet[name]filedresult:");

//输出分片信息

FacetFieldfacetField=response.getFacetField("name");

List<Count>facetFields=facetField.getValues();

for(Countcount:facetFields){

//关键字-出现次数

fail(count.getName()+":"+count.getCount());

}

}catch(SolrServerExceptione){

e.printStackTrace();

}

}


分片查询在某些统计关键字的时候还是很有用的,可以统计关键字出现的次数,可以通过统计的关键字来搜索相关文档的信息。



四、Document文档和JavaBean相互转换

这里转换的Bean是一个简单的User对象

packagecom.hoo.entity;


importjava.io.Serializable;

importorg.apache.solr.client.solrj.beans.Field;


/**

*<b>function:</b>UserEntityBean;所有被添加Annotation@Field注解的属性将参与index操作

*@authorhoojo

*@createDate2011-10-19下午04:16:00

*@fileUser.java

*@packagecom.hoo.entity

*@projectSolrExample

*@blog'target='_blank'>http://blog.csdn.net/IBM_hoojo[/code]
*@emailhoojo_@126.com

*@version1.0

*/

publicclassUserimplementsSerializable{


/**

*@authorHoojo

*/

privatestaticfinallongserialVersionUID=8606788203814942679L;


//@Field

privateintid;

@Field

privateStringname;

@Field

privateintage;


/**

*可以给某个属性重命名,likes就是solrindex的属性;在solrIndex中将显示like为likes

*/

@Field("likes")

privateString[]like;

@Field

privateStringaddress;

@Field

privateStringsex;

@Field

privateStringremark;

publicintgetId(){

returnid;

}


//setter方法上面也可以

@Field

publicvoidsetId(intid){

this.id=id;

}

publicStringgetName(){

returnname;

}

//getter、setter


@Override

publicStringtoString(){

returnthis.id+"#"+this.name+"#"+this.age+"#"+this.like+"#"+this.address+"#"+this.sex+"#"+this.remark;

}

}




测试类代码如下

packagecom.hoo.test;


importorg.apache.solr.client.solrj.beans.DocumentObjectBinder;

importorg.apache.solr.common.SolrDocument;

importorg.apache.solr.common.SolrDocumentList;

importorg.apache.solr.common.SolrInputDocument;

importorg.apache.solr.common.SolrInputField;

importorg.junit.Test;

importcom.hoo.entity.User;


/**

*<b>function:</b>SolrInputDocumentimplementsMap,Iterable

*@authorhoojo

*@createDate2011-10-19下午03:54:54

*@fileSolrInputDocumentTest.java

*@packagecom.hoo.test

*@projectSolrExample

*@blog'target='_blank'>http://blog.csdn.net/IBM_hoojo[/code]
*@emailhoojo_@126.com

*@version1.0

*/

publicclassSolrInputDocumentTest{


publicfinalvoidfail(Objecto){

System.out.println(o);

}


/**

*<b>function:</b>创建SolrInputDocument

*@authorhoojo

*@createDate2011-10-21下午03:38:20

*/

@Test

publicvoidcreateDoc(){

SolrInputDocumentdoc=newSolrInputDocument();

doc.addField("id",System.currentTimeMillis());

doc.addField("name","SolrInputDocument");

doc.addField("age",22,2.0f);


doc.addField("like",newString[]{"music","book","sport"});


doc.put("address",newSolrInputField("guangzhou"));


doc.setField("sex","man");

doc.setField("remark","chinapeople",2.0f);


fail(doc);

}


/**

*<b>function:</b>利用DocumentObjectBinder对象将SolrInputDocument和User对象相互转换

*@authorhoojo

*@createDate2011-10-21下午03:38:40

*/

@Test

publicvoiddocAndBean4Binder(){

SolrDocumentdoc=newSolrDocument();

doc.addField("id",456);

doc.addField("name","SolrInputDocument");


doc.addField("likes",newString[]{"music","book","sport"});


doc.put("address","guangzhou");


doc.setField("sex","man");

doc.setField("remark","chinapeople");


DocumentObjectBinderbinder=newDocumentObjectBinder();


Useruser=newUser();

user.setId(222);

user.setName("JavaBean");

user.setLike(newString[]{"music","book","sport"});

user.setAddress("guangdong");


fail(doc);

//User->>SolrInputDocument

fail(binder.toSolrInputDocument(user));

//SolrDocument->>User

fail(binder.getBean(User.class,doc));


SolrDocumentListlist=newSolrDocumentList();

list.add(doc);

list.add(doc);

//SolrDocumentList->>List

fail(binder.getBeans(User.class,list));

}


/**

*<b>function:</b>SolrInputDocument的相关方法

*@authorhoojo

*@createDate2011-10-21下午03:44:30

*/

@Test

publicvoiddocMethod(){

SolrInputDocumentdoc=newSolrInputDocument();

doc.addField("id",System.currentTimeMillis());

doc.addField("name","SolrInputDocument");

doc.addField("age",23,1.0f);

doc.addField("age",22,2.0f);

doc.addField("age",24,0f);


fail(doc.entrySet());

fail(doc.get("age"));

//排名有用,类似百度竞价排名

doc.setDocumentBoost(2.0f);

fail(doc.getDocumentBoost());

fail(doc.getField("name"));

fail(doc.getFieldNames());//keys

fail(doc.getFieldValues("age"));

fail(doc.getFieldValues("id"));

fail(doc.values());

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: