solr(4)solrj - how to delete/query documents

solr(4)solrj-howtodelete/querydocuments

1.Deletethedocumentinindexfiles

@Test

publicvoidremove(){

try{

query("id:1");

//删除id为1的索引

this.embeddedSolrServer.deleteById("1");

this.embeddedSolrServer.commit();

query("id:1");

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

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

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

ids.add("2");

ids.add("3");

this.embeddedSolrServer.deleteById(ids);

this.embeddedSolrServer.commit(true,true);

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

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

this.embeddedSolrServer.deleteByQuery("id:4id:6");

this.embeddedSolrServer.commit(true,true);

queryAll();

}catch(SolrServerExceptione){

e.printStackTrace();

}catch(IOExceptione){

e.printStackTrace();

}

}

2.queryallthedocumentsinindex

@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");

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

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

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

try{

QueryResponseresponse=embeddedSolrServer.query(params);

SolrDocumentListlist=response.getResults();

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

SolrDocumentdocTmp=list.get(i);

System.out.print(docTmp.get("name")+"------");

System.out.println(docTmp);

}

}catch(SolrServerExceptione){

e.printStackTrace();

}

}

3.Systemmethodforsolrj

@Test

publicvoidsystemMethod(){

System.out.println(solrServer.getBinder());

try{

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

System.out.println(solrServer.ping());//ping服务器是否连接成功

}catch(SolrServerExceptione){

e.printStackTrace();

}catch(IOExceptione){

e.printStackTrace();

}

}

4.Rollbackmethod

@Test

publicvoidrollback(){

try{

Productp299=newProduct();

p299.setId("299");

p299.setName("addbeanindex199");

p299.setManu("indexbeanmanu199");

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

UpdateResponseresponse=solrServer.addBean(p299);

System.out.println("response:"+response);

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

System.out.println("rollback:"+solrServer.rollback());

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

System.out.println("commit:"+solrServer.commit());

queryAll();

}catch(SolrServerExceptione){

e.printStackTrace();

}catch(IOExceptione){

e.printStackTrace();

}

}

5.CaseQuery

@Test

publicvoidqueryCase(){

//AND并且

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

//OR或者

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

//空格等同于OR

params.setQuery("name:servermanu: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]");

//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");

try{

QueryResponseresponse=solrServer.query(params);

List<Product>products=response.getBeans(Product.class);

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

System.out.println(products.get(i));

}

//输出查询结果集

SolrDocumentListlist=response.getResults();

System.out.println("queryresultnums:"+list.getNumFound());

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

System.out.println(list.get(i));

}

//输出分片信息

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

for(FacetFieldfacet:facets){

System.out.println(facet);

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

for(FacetField.Countcount:facetCounts){

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

+count.getCount());

}

}

}catch(SolrServerExceptione){

e.printStackTrace();

}

}

6.FacetSearch

@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=solrServer.query(params);

//输出查询结果集

SolrDocumentListlist=response.getResults();

System.out.println("Queryresultnums:"+list.getNumFound());

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

System.out.println(list.get(i));

}

System.out.println("Allfacetfiledresult:");

//输出分片信息

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

for(FacetFieldfacet:facets){

System.out.println(facet);

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

for(FacetField.Countcount:facetCounts){

//关键字-出现次数

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

}

}

System.out.println("Searchfacet[name]filedresult:");

//输出分片信息

FacetFieldfacetField=response.getFacetField("name");

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

for(Countcount:facetFields){

//关键字-出现次数

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

}

}catch(SolrServerExceptione){

e.printStackTrace();

}

}

7.highttheresults

@Test

publicvoidqueryHight(){

SolrQueryparams=newSolrQuery();

params.setQuery("manu:SolrInputDocuments");

params.setHighlight(true);

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

params.setHighlightSimplePre("<fontcolor='red'>");

//标记,高亮关键字前缀

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

//后缀

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

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

try{

QueryResponseresponse=solrServer.query(params);

//输出查询结果集

SolrDocumentListlist=response.getResults();

System.out.println("queryresultnums:"+list.getNumFound());

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

SolrDocumentdoc=list.get(i);

Stringid=(String)doc.getFieldValue("id");

if(response.getHighlighting().get(id)!=null){

List<String>highlightSnippets=response

.getHighlighting().get(id).get("manu");

System.out.println("hightlight="+highlightSnippets);

}

}

}catch(SolrServerExceptione){

e.printStackTrace();

}

}

references:

http://www.blogjava.net/hoojo/archive/2011/10/21/361747.html

http://wiki.apache.org/solr/Solrj

相关推荐