古越剑箫

学习是一种习惯

  :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: :: 管理 ::

1、首先导入solrj需要的的架包

2、需要注意的是低版本是solr是使用SolrServer进行URL实例的,5.0之后已经使用SolrClient替代这个类了,在添加之后首先我们需要根据schema.xml配置一下我们的分词器

这里的msg_all还需要在schema.xml中配置

它的主要作用是将msg_title,msg_content两个域的值拷贝到msg_all域中,我们在搜索的时候可以只搜索这个msg_all域就可以了,

solr默认搜索需要带上域,比如

 

solr更改默认搜索域的地方也在schema.xml,它默认是搜索text域的,但是5.0之后不在这里配置默认搜索域了,它的文档也告诉我们,在solrconfig.xml中配置

 

在solrconfig.xml中配置默认搜素域,这样我们就可以按照我们自己的域进行搜索了

 

 配置好以上,就可以使用代码进行CURD

private final  static String URL="http://localhost:8080/solr/java";
    public SolrClient  server=null;
    
    @Before
    public void init() throws Exception{
        server=new HttpSolrClient(URL);
    }

 

删除所有分词

//删除所有分词
    @Test
    public void testDel() throws Exception{
        server.deleteByQuery("*:*");
        server.commit();//先删除 基于query的删除 会删除所有建立的索引文件
    }

 

增加分词

@Test
    public void testAdd() throws Exception{
        SolrInputDocument doc=new SolrInputDocument();
        doc.addField("id", "3");
        doc.addField("msg_title", "新浪微博");
        doc.addField("msg_content", "我有一个微博帐号名字叫做什么呢?");
        server.add(doc);
        server.commit();
    }

 

基于Bean增加分词

@Test
    public void test03() throws Exception{
        List<Message> msgs=new ArrayList<Message>();
        msgs.add(new Message("4", "第四个测试solr测试文件", new String[]{"中华人民共和国万岁","中华上下五千年那年"}));
        msgs.add(new Message("5", "第5个好朋友是什么意思呢?", new String[]{"上海是个好地方","歌唱我们亲爱的祖国曾经走过千山万水"}));
        server.addBeans(msgs);
        server.commit();
    }

 

查询结果

@Test
    public void test04() throws Exception{
        //定义查询内容 * 代表查询所有    这个是基于结果集
         SolrQuery query = new SolrQuery("solr");
         query.setStart(0);//起始页
         query.setRows(3);//每页显示数量
         QueryResponse rsp = server.query( query );
         SolrDocumentList results = rsp.getResults();
         System.out.println(results.getNumFound());//查询总条数
         for(SolrDocument doc:results){
             System.out.println(doc);
         }
    }

 

将查询结果集封装为对象Bean

@Test
    public void test05() throws Exception{
         SolrQuery query = new SolrQuery("中华");// * 号 是查询 所有的数据
         QueryResponse rsp = server.query( query );
         List<Message> beans = rsp.getBeans(Message.class);//这个不能获取查询的总数了 也不能高亮
         for(Message message:beans){
             System.out.println(message.toString());
         }
    }

 

将结果集高亮显示

@Test
    public void test06() throws Exception{
        //定义查询内容 * 代表查询所有    这个是基于结果集
         SolrQuery query = new SolrQuery("solr");
         query.setStart(0);//起始页
         query.setRows(5);//每页显示数量
         query.setParam("hl.fl", "msg_title,msg_content");//设置哪些字段域会高亮显示
         query.setHighlight(true).setHighlightSimplePre("<span class='hight'>")
         .setHighlightSimplePost("</span>");
         
         QueryResponse rsp = server.query( query );
         SolrDocumentList results = rsp.getResults();
         System.out.println(results.getNumFound());//查询总条数
         for(SolrDocument doc:results){
             String id = (String) doc.getFieldValue("id"); //id is the uniqueKey field
             if(rsp.getHighlighting().get(id)!=null){
                 //高亮必须要求存储 不存储的话 没法添加高亮
                 System.out.println(rsp.getHighlighting().get(id).get("msg_title"));
             }
         }
    }

 

ok,solr的基本使用就完成了

 

posted on 2016-09-30 23:04  古越剑箫  阅读(7677)  评论(0编辑  收藏  举报