wstlwl 发表于 2016-12-16 09:54:58

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

  代码如下:

view plaincopyprint?





[*]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;
}
}


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、
  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

微信公众号:

页: [1]
查看完整版本: solr的增删改查和高亮以及分组