`
wbj0110
  • 浏览: 1552704 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

solr的增删改查和高亮以及分组

    博客分类:
  • Solr
阅读更多

代码如下:

 

  1. package com.hj.solr;  
  2.   
  3. import org.apache.solr.client.solrj.beans.Field;  
  4.   
  5. /** 
  6.  * 在变量的set方法上注解上lucene内部的字段名称 
  7.  */  
  8. public class Message {  
  9.     private String id;  
  10.     private String title;  
  11.     private String content[];  
  12.       
  13.       
  14.     public Message() {  
  15.         super();  
  16.     }  
  17.   
  18.     public Message(String id, String title, String[] content) {  
  19.         super();  
  20.         this.id = id;  
  21.         this.title = title;  
  22.         this.content = content;  
  23.     }  
  24.   
  25.     public String getId() {  
  26.         return id;  
  27.     }  
  28.       
  29.     @Field  
  30.     public void setId(String id) {  
  31.         this.id = id;  
  32.     }  
  33.     public String getTitle() {  
  34.         return title;  
  35.     }  
  36.       
  37.     @Field("msg_title")  
  38.     public void setTitle(String title) {  
  39.         this.title = title;  
  40.     }  
  41.     public String[] getContent() {  
  42.         return content;  
  43.     }  
  44.       
  45.     @Field("msg_content")  
  46.     public void setContent(String[] content) {  
  47.         this.content = content;  
  48.     }  
  49. }  
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;
	}
}

 

  1. package com.hj.solr;  
  2.   
  3. import java.io.IOException;  
  4. import java.net.MalformedURLException;  
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7.   
  8. import org.apache.solr.client.solrj.SolrQuery;  
  9. import org.apache.solr.client.solrj.SolrServerException;  
  10. import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;  
  11. import org.apache.solr.client.solrj.response.FacetField.Count;  
  12. import org.apache.solr.client.solrj.response.QueryResponse;  
  13. import org.apache.solr.common.SolrDocument;  
  14. import org.apache.solr.common.SolrDocumentList;  
  15. import org.apache.solr.common.SolrInputDocument;  
  16. import org.junit.Test;  
  17.   
  18. /** 
  19.  * @date 2013年12月4日 
  20.  * @author huangjie 
  21.  */  
  22. @SuppressWarnings("deprecation")  
  23. public class SolrTest {  
  24.     //指定solr服务器的地址  
  25.     private final static String URL = "http://localhost:8080/solr";  
  26.       
  27.     @Test  
  28.     public void test1(){  
  29.         //1、创建SolrServer对象,该对象有两个可以使用,都是线程安全的  
  30. //      CommonsHttpSolrServer:启动web服务器使用的,通过http请求的  
  31. //      EmbeddedSolrServer:内嵌式的,导入solr的jar包就可以使用了  
  32.         try {  
  33.             CommonsHttpSolrServer server = new CommonsHttpSolrServer(URL);  
  34.             //把查询出来的数据全部删除  
  35. //          server.deleteByQuery("*:*");  
  36. //          server.commit();  
  37.               
  38.             SolrInputDocument doc = new SolrInputDocument();  
  39.             //id是必填的,并且是String类型的  
  40.             //<field name="id" type="string" indexed="true" stored="true" required="true" />  
  41.             //id是唯一的主键,当多次添加的时候,最后添加的相同id会覆盖前面的域  
  42.             doc.addField("id""1");  
  43.             doc.addField("msg_title""这是我的第一个solrj程序");  
  44.             doc.addField("msg_content""solr程序的运行");  
  45.             server.add(doc);  
  46.             server.commit();  
  47.         } catch (MalformedURLException e) {  
  48.             e.printStackTrace();  
  49.         } catch (SolrServerException e) {  
  50.             e.printStackTrace();  
  51.         } catch (IOException e) {  
  52.             e.printStackTrace();  
  53.         }  
  54.     }  
  55.       
  56.     /** 
  57.      * 基于列表的添加 
  58.      * @throws SolrServerException 
  59.      * @throws IOException 
  60.      */  
  61.     @Test  
  62.     public void test2() throws SolrServerException, IOException{  
  63.         List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();  
  64.         SolrInputDocument doc = new SolrInputDocument();  
  65.         doc.addField("id""2");  
  66.         doc.addField("msg_title""很好,solr可以工作了");  
  67.         doc.addField("msg_content""solr总算可以正式工作了");  
  68.           
  69.         docs.add(doc);  
  70.           
  71.         doc = new SolrInputDocument();  
  72.         doc.addField("id""3");  
  73.         doc.addField("msg_title""测试以下solr的添加");  
  74.         doc.addField("msg_content""看看能不能添加一个列表信息");  
  75.           
  76.         docs.add(doc);  
  77.           
  78.         CommonsHttpSolrServer server = new CommonsHttpSolrServer(URL);  
  79.         server.add(docs);  
  80.         server.commit();  
  81.     }  
  82.       
  83.     /** 
  84.      * 基于javabean的添加 
  85.      * @throws SolrServerException 
  86.      * @throws IOException 
  87.      */  
  88.     @Test  
  89.     public void test3() throws SolrServerException, IOException{  
  90.         List<Message> msgs = new ArrayList<Message>();  
  91.         //多值域的添加使用数组  
  92.         msgs.add(new Message("4","基于javabean的添加"new String[]{"javabean的添加附件","javabean的添加附件1"}));  
  93.         msgs.add(new Message("5","基于javabean的列表数据的添加"new String[]{"通过对象完成添加的附件","通过对象完成添加的附件1"}));  
  94.           
  95.         CommonsHttpSolrServer server = new CommonsHttpSolrServer(URL);  
  96.         server.addBeans(msgs);  
  97.         server.commit();  
  98.     }  
  99.       
  100.     @Test  
  101.     public void test4() throws SolrServerException, MalformedURLException{  
  102.         CommonsHttpSolrServer server = new CommonsHttpSolrServer(URL);  
  103.         //定义查询字符串  
  104.         SolrQuery query = new SolrQuery("*:*");  
  105.         //实现分页的查询  
  106.         query.setStart(0);  
  107.         query.setRows(3);  
  108.         QueryResponse res = server.query(query);  
  109.         //查询出来的结果都保存在SolrDocumentList中  
  110.         SolrDocumentList sdl = res.getResults();  
  111.         System.out.println("总数:"+sdl.getNumFound());  
  112.         for(SolrDocument sd : sdl){  
  113.             System.out.println(sd.get("id")+"#"+sd.get("msg_title")+"#"+sd.get("msg_content"));  
  114.         }  
  115.     }  
  116.       
  117.     @Test  
  118.     public void test5() throws MalformedURLException, SolrServerException{  
  119.         CommonsHttpSolrServer server = new CommonsHttpSolrServer(URL);  
  120.         //相当于QueryParser  
  121.         SolrQuery query = new SolrQuery("*:*");  
  122.         query.setStart(1);  
  123.         query.setRows(3);  
  124.         QueryResponse res = server.query(query);  
  125.         //可以直接查询相应的bean对象,但是不是很常用  
  126.         //使用这种方式无法获取总数量  
  127.         List<Message> list = res.getBeans(Message.class);  
  128.         System.out.println("当前总数:"+list.size());  
  129.         for(Message msg : list){  
  130.             System.out.println(msg.getId()+"#"+msg.getTitle()+"#"+msg.getContent());  
  131.         }  
  132.     }  
  133.       
  134.     @Test  
  135.     public void test6() throws SolrServerException, MalformedURLException{  
  136.         CommonsHttpSolrServer server = new CommonsHttpSolrServer(URL);  
  137.         SolrQuery query = new SolrQuery("msg_content:solr");  
  138.         query.setHighlight(true).setHighlightSimplePre("<span class='red'>").setHighlightSimplePost("</span>")  
  139.         .setStart(0).setRows(10);  
  140.         //hl.fl表示高亮的field,也就是高亮的区域  
  141.         query.setParam("hl.fl""msg_title,msg_content");  
  142.         QueryResponse res = server.query(query);  
  143.           
  144.         SolrDocumentList sdl = res.getResults();  
  145.         System.out.println("总数:"+sdl.getNumFound());  
  146.         for(SolrDocument sd : sdl){  
  147. //          System.out.println(sd.get("id")+"#"+sd.get("msg_title")+"#"+sd.get("msg_content"));  
  148.             String id = (String) sd.get("id");  
  149.             //在solr这里对需要加高亮的字段必须要在索引中的store=true才行  
  150.             System.out.println(id+"#"+res.getHighlighting().get(id).get("msg_content"));;  
  151.               
  152.         }  
  153.     }  
  154.       
  155.     /** 
  156.      * 测试分组 
  157.      * @throws MalformedURLException  
  158.      * @throws SolrServerException  
  159.      */  
  160.     @Test  
  161.     public void test7() throws MalformedURLException, SolrServerException{  
  162.         CommonsHttpSolrServer server = new CommonsHttpSolrServer(URL);  
  163.         SolrQuery query = new SolrQuery("city:*");  
  164.         query.setIncludeScore(false);  
  165.         query.setFacet(true);  
  166.         query.addFacetField("city");  
  167.         query.setFacetSort(true);  
  168.         QueryResponse res = server.query(query);  
  169.         List<Count> countList = res.getFacetField("city").getValues();  
  170.           
  171.         for(Count count : countList){  
  172.             System.out.println(count.getName()+"#"+count.getCount());  
  173.         }  
  174.         /** 
  175.          * 输出结果: 
  176.          *  上海#5290 
  177.             深圳#2763 
  178.             广州#2504 
  179.             北京#1962 
  180.             东莞#1764 
  181.             杭州#1713 
  182.             苏州#1661 
  183.             南京#1529 
  184.          */  
  185.     }  
  186.   
  187.       
  188.       
  189. }  
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、

 

2、

3、

4、

5、

6、

7、

8、

9、

10、

 

工程下载地址:http://download.csdn.net/detail/wxwzy738/6665131

http://blog.csdn.net/wxwzy738/article/details/17149433

 

 

大家可以加我个人微信号:scccdgf

 

 

或者关注soledede的微信公众号:soledede
微信公众号:
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics