设为首页 收藏本站
查看: 1144|回复: 0

[经验分享] solr的增删改查和高亮以及分组

[复制链接]
累计签到:2 天
连续签到:1 天
发表于 2016-12-16 09:54:58 | 显示全部楼层 |阅读模式
  代码如下:

[java] view plaincopyprint? DSC0000.jpg DSC0001.jpg





  • package com.hj.solr;  
  •   
  • import org.apache.solr.client.solrj.beans.Field;  
  •   
  • /** 
  •  * 在变量的set方法上注解上lucene内部的字段名称 
  •  */  
  • public class Message {  
  •     private String id;  
  •     private String title;  
  •     private String content[];  
  •       
  •       
  •     public Message() {  
  •         super();  
  •     }  
  •   
  •     public Message(String id, String title, String[] content) {  
  •         super();  
  •         this.id = id;  
  •         this.title = title;  
  •         this.content = content;  
  •     }  
  •   
  •     public String getId() {  
  •         return id;  
  •     }  
  •       
  •     @Field  
  •     public void setId(String id) {  
  •         this.id = id;  
  •     }  
  •     public String getTitle() {  
  •         return title;  
  •     }  
  •       
  •     @Field("msg_title")  
  •     public void setTitle(String title) {  
  •         this.title = title;  
  •     }  
  •     public String[] getContent() {  
  •         return content;  
  •     }  
  •       
  •     @Field("msg_content")  
  •     public void setContent(String[] content) {  
  •         this.content = content;  
  •     }  
  • }  



package com.hj.solr;
import org.apache.solr.client.solrj.beans.Field;
/**
* 在变量的set方法上注解上lucene内部的字段名称
*/
public class Message {
private String id;
private String title;
private String content[];

public Message() {
super();
}
public Message(String id, String title, String[] content) {
super();
this.id = id;
this.title = title;
this.content = content;
}
public String getId() {
return id;
}
@Field
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
@Field("msg_title")
public void setTitle(String title) {
this.title = title;
}
public String[] getContent() {
return content;
}
@Field("msg_content")
public void setContent(String[] content) {
this.content = content;
}
}


[java] view plaincopyprint?





  • package com.hj.solr;  
  •   
  • import java.io.IOException;  
  • import java.net.MalformedURLException;  
  • import java.util.ArrayList;  
  • import java.util.List;  
  •   
  • 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.FacetField.Count;  
  • 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;  
  •   
  • /** 
  •  * @date 2013年12月4日 
  •  * @author huangjie 
  •  */  
  • @SuppressWarnings("deprecation")  
  • public class SolrTest {  
  •     //指定solr服务器的地址  
  •     private final static String URL = "http://localhost:8080/solr";  
  •       
  •     @Test  
  •     public void test1(){  
  •         //1、创建SolrServer对象,该对象有两个可以使用,都是线程安全的  
  • //      CommonsHttpSolrServer:启动web服务器使用的,通过http请求的  
  • //      EmbeddedSolrServer:内嵌式的,导入solr的jar包就可以使用了  
  •         try {  
  •             CommonsHttpSolrServer server = new CommonsHttpSolrServer(URL);  
  •             //把查询出来的数据全部删除  
  • //          server.deleteByQuery("*:*");  
  • //          server.commit();  
  •               
  •             SolrInputDocument doc = new SolrInputDocument();  
  •             //id是必填的,并且是String类型的  
  •             //<field name="id" type="string" indexed="true" stored="true" required="true" />  
  •             //id是唯一的主键,当多次添加的时候,最后添加的相同id会覆盖前面的域  
  •             doc.addField("id", "1");  
  •             doc.addField("msg_title", "这是我的第一个solrj程序");  
  •             doc.addField("msg_content", "solr程序的运行");  
  •             server.add(doc);  
  •             server.commit();  
  •         } catch (MalformedURLException e) {  
  •             e.printStackTrace();  
  •         } catch (SolrServerException e) {  
  •             e.printStackTrace();  
  •         } catch (IOException e) {  
  •             e.printStackTrace();  
  •         }  
  •     }  
  •       
  •     /** 
  •      * 基于列表的添加 
  •      * @throws SolrServerException 
  •      * @throws IOException 
  •      */  
  •     @Test  
  •     public void test2() throws SolrServerException, IOException{  
  •         List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();  
  •         SolrInputDocument doc = new SolrInputDocument();  
  •         doc.addField("id", "2");  
  •         doc.addField("msg_title", "很好,solr可以工作了");  
  •         doc.addField("msg_content", "solr总算可以正式工作了");  
  •           
  •         docs.add(doc);  
  •           
  •         doc = new SolrInputDocument();  
  •         doc.addField("id", "3");  
  •         doc.addField("msg_title", "测试以下solr的添加");  
  •         doc.addField("msg_content", "看看能不能添加一个列表信息");  
  •           
  •         docs.add(doc);  
  •           
  •         CommonsHttpSolrServer server = new CommonsHttpSolrServer(URL);  
  •         server.add(docs);  
  •         server.commit();  
  •     }  
  •       
  •     /** 
  •      * 基于javabean的添加 
  •      * @throws SolrServerException 
  •      * @throws IOException 
  •      */  
  •     @Test  
  •     public void test3() throws SolrServerException, IOException{  
  •         List<Message> msgs = new ArrayList<Message>();  
  •         //多值域的添加使用数组  
  •         msgs.add(new Message("4","基于javabean的添加", new String[]{"javabean的添加附件","javabean的添加附件1"}));  
  •         msgs.add(new Message("5","基于javabean的列表数据的添加", new String[]{"通过对象完成添加的附件","通过对象完成添加的附件1"}));  
  •           
  •         CommonsHttpSolrServer server = new CommonsHttpSolrServer(URL);  
  •         server.addBeans(msgs);  
  •         server.commit();  
  •     }  
  •       
  •     @Test  
  •     public void test4() throws SolrServerException, MalformedURLException{  
  •         CommonsHttpSolrServer server = new CommonsHttpSolrServer(URL);  
  •         //定义查询字符串  
  •         SolrQuery query = new SolrQuery("*:*");  
  •         //实现分页的查询  
  •         query.setStart(0);  
  •         query.setRows(3);  
  •         QueryResponse res = server.query(query);  
  •         //查询出来的结果都保存在SolrDocumentList中  
  •         SolrDocumentList sdl = res.getResults();  
  •         System.out.println("总数:"+sdl.getNumFound());  
  •         for(SolrDocument sd : sdl){  
  •             System.out.println(sd.get("id")+"#"+sd.get("msg_title")+"#"+sd.get("msg_content"));  
  •         }  
  •     }  
  •       
  •     @Test  
  •     public void test5() throws MalformedURLException, SolrServerException{  
  •         CommonsHttpSolrServer server = new CommonsHttpSolrServer(URL);  
  •         //相当于QueryParser  
  •         SolrQuery query = new SolrQuery("*:*");  
  •         query.setStart(1);  
  •         query.setRows(3);  
  •         QueryResponse res = server.query(query);  
  •         //可以直接查询相应的bean对象,但是不是很常用  
  •         //使用这种方式无法获取总数量  
  •         List<Message> list = res.getBeans(Message.class);  
  •         System.out.println("当前总数:"+list.size());  
  •         for(Message msg : list){  
  •             System.out.println(msg.getId()+"#"+msg.getTitle()+"#"+msg.getContent());  
  •         }  
  •     }  
  •       
  •     @Test  
  •     public void test6() throws SolrServerException, MalformedURLException{  
  •         CommonsHttpSolrServer server = new CommonsHttpSolrServer(URL);  
  •         SolrQuery query = new SolrQuery("msg_content:solr");  
  •         query.setHighlight(true).setHighlightSimplePre("<span class='red'>").setHighlightSimplePost("</span>")  
  •         .setStart(0).setRows(10);  
  •         //hl.fl表示高亮的field,也就是高亮的区域  
  •         query.setParam("hl.fl", "msg_title,msg_content");  
  •         QueryResponse res = server.query(query);  
  •           
  •         SolrDocumentList sdl = res.getResults();  
  •         System.out.println("总数:"+sdl.getNumFound());  
  •         for(SolrDocument sd : sdl){  
  • //          System.out.println(sd.get("id")+"#"+sd.get("msg_title")+"#"+sd.get("msg_content"));  
  •             String id = (String) sd.get("id");  
  •             //在solr这里对需要加高亮的字段必须要在索引中的store=true才行  
  •             System.out.println(id+"#"+res.getHighlighting().get(id).get("msg_content"));;  
  •               
  •         }  
  •     }  
  •       
  •     /** 
  •      * 测试分组 
  •      * @throws MalformedURLException  
  •      * @throws SolrServerException  
  •      */  
  •     @Test  
  •     public void test7() throws MalformedURLException, SolrServerException{  
  •         CommonsHttpSolrServer server = new CommonsHttpSolrServer(URL);  
  •         SolrQuery query = new SolrQuery("city:*");  
  •         query.setIncludeScore(false);  
  •         query.setFacet(true);  
  •         query.addFacetField("city");  
  •         query.setFacetSort(true);  
  •         QueryResponse res = server.query(query);  
  •         List<Count> countList = res.getFacetField("city").getValues();  
  •           
  •         for(Count count : countList){  
  •             System.out.println(count.getName()+"#"+count.getCount());  
  •         }  
  •         /** 
  •          * 输出结果: 
  •          *  上海#5290 
  •             深圳#2763 
  •             广州#2504 
  •             北京#1962 
  •             东莞#1764 
  •             杭州#1713 
  •             苏州#1661 
  •             南京#1529 
  •          */  
  •     }  
  •   
  •       
  •       
  • }  



package com.hj.solr;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
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.FacetField.Count;
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;
/**
* @date 2013年12月4日
* @author huangjie
*/
@SuppressWarnings("deprecation")
public class SolrTest {
//指定solr服务器的地址
private final static String URL = "http://localhost:8080/solr";
@Test
public void test1(){
//1、创建SolrServer对象,该对象有两个可以使用,都是线程安全的
//CommonsHttpSolrServer:启动web服务器使用的,通过http请求的
//EmbeddedSolrServer:内嵌式的,导入solr的jar包就可以使用了
try {
CommonsHttpSolrServer server = new CommonsHttpSolrServer(URL);
//把查询出来的数据全部删除
//server.deleteByQuery("*:*");
//server.commit();
SolrInputDocument doc = new SolrInputDocument();
//id是必填的,并且是String类型的
//<field name="id" type="string" indexed="true" stored="true" required="true" />
//id是唯一的主键,当多次添加的时候,最后添加的相同id会覆盖前面的域
doc.addField("id", "1");
doc.addField("msg_title", "这是我的第一个solrj程序");
doc.addField("msg_content", "solr程序的运行");
server.add(doc);
server.commit();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 基于列表的添加
* @throws SolrServerException
* @throws IOException
*/
@Test
public void test2() throws SolrServerException, IOException{
List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "2");
doc.addField("msg_title", "很好,solr可以工作了");
doc.addField("msg_content", "solr总算可以正式工作了");
docs.add(doc);
doc = new SolrInputDocument();
doc.addField("id", "3");
doc.addField("msg_title", "测试以下solr的添加");
doc.addField("msg_content", "看看能不能添加一个列表信息");
docs.add(doc);
CommonsHttpSolrServer server = new CommonsHttpSolrServer(URL);
server.add(docs);
server.commit();
}
/**
* 基于javabean的添加
* @throws SolrServerException
* @throws IOException
*/
@Test
public void test3() throws SolrServerException, IOException{
List<Message> msgs = new ArrayList<Message>();
//多值域的添加使用数组
msgs.add(new Message("4","基于javabean的添加", new String[]{"javabean的添加附件","javabean的添加附件1"}));
msgs.add(new Message("5","基于javabean的列表数据的添加", new String[]{"通过对象完成添加的附件","通过对象完成添加的附件1"}));
CommonsHttpSolrServer server = new CommonsHttpSolrServer(URL);
server.addBeans(msgs);
server.commit();
}
@Test
public void test4() throws SolrServerException, MalformedURLException{
CommonsHttpSolrServer server = new CommonsHttpSolrServer(URL);
//定义查询字符串
SolrQuery query = new SolrQuery("*:*");
//实现分页的查询
query.setStart(0);
query.setRows(3);
QueryResponse res = server.query(query);
//查询出来的结果都保存在SolrDocumentList中
SolrDocumentList sdl = res.getResults();
System.out.println("总数:"+sdl.getNumFound());
for(SolrDocument sd : sdl){
System.out.println(sd.get("id")+"#"+sd.get("msg_title")+"#"+sd.get("msg_content"));
}
}
@Test
public void test5() throws MalformedURLException, SolrServerException{
CommonsHttpSolrServer server = new CommonsHttpSolrServer(URL);
//相当于QueryParser
SolrQuery query = new SolrQuery("*:*");
query.setStart(1);
query.setRows(3);
QueryResponse res = server.query(query);
//可以直接查询相应的bean对象,但是不是很常用
//使用这种方式无法获取总数量
List<Message> list = res.getBeans(Message.class);
System.out.println("当前总数:"+list.size());
for(Message msg : list){
System.out.println(msg.getId()+"#"+msg.getTitle()+"#"+msg.getContent());
}
}
@Test
public void test6() throws SolrServerException, MalformedURLException{
CommonsHttpSolrServer server = new CommonsHttpSolrServer(URL);
SolrQuery query = new SolrQuery("msg_content:solr");
query.setHighlight(true).setHighlightSimplePre("<span class='red'>").setHighlightSimplePost("</span>")
.setStart(0).setRows(10);
//hl.fl表示高亮的field,也就是高亮的区域
query.setParam("hl.fl", "msg_title,msg_content");
QueryResponse res = server.query(query);
SolrDocumentList sdl = res.getResults();
System.out.println("总数:"+sdl.getNumFound());
for(SolrDocument sd : sdl){
//System.out.println(sd.get("id")+"#"+sd.get("msg_title")+"#"+sd.get("msg_content"));
String id = (String) sd.get("id");
//在solr这里对需要加高亮的字段必须要在索引中的store=true才行
System.out.println(id+"#"+res.getHighlighting().get(id).get("msg_content"));;
}
}
/**
* 测试分组
* @throws MalformedURLException
* @throws SolrServerException
*/
@Test
public void test7() throws MalformedURLException, SolrServerException{
CommonsHttpSolrServer server = new CommonsHttpSolrServer(URL);
SolrQuery query = new SolrQuery("city:*");
query.setIncludeScore(false);
query.setFacet(true);
query.addFacetField("city");
query.setFacetSort(true);
QueryResponse res = server.query(query);
List<Count> countList = res.getFacetField("city").getValues();
for(Count count : countList){
System.out.println(count.getName()+"#"+count.getCount());
}
/**
* 输出结果:
*  上海#5290
深圳#2763
广州#2504
北京#1962
东莞#1764
杭州#1713
苏州#1661
南京#1529
*/
}

}

  
1、 DSC0002.jpg
  2、 DSC0003.jpg
  3、 DSC0004.jpg
  4、 DSC0005.jpg
  5、 DSC0006.jpg
  6、 DSC0007.jpg
  7、 DSC0008.jpg
  8、 DSC0009.jpg
  9、 DSC00010.jpg
  10、
  工程下载地址:http://download.csdn.net/detail/wxwzy738/6665131
  http://blog.csdn.net/wxwzy738/article/details/17149433
  大家可以加我个人微信号:scccdgf

或者关注soledede的微信公众号:soledede

微信公众号:


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-315030-1-1.html 上篇帖子: Solr的自动完成/自动补充实现介绍(第二部分) 下篇帖子: 完整的solr的增加索引的xml的格式
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表