简单介绍solrj的使用

Solrj是使用java编写的一个操作Solr的工具,方便于进行索引的更新、搜索结果的获取等等
在使用之前需要引入相应的jar包, 由于我使用的是maven构建项目的,因此贴出maven的pom文件:

<!-- solr客户端 -->
        <dependency>
            <groupId>org.apache.solr</groupId>
            <artifactId>solr-solrj</artifactId>
            <version>4.10.3</version>
        </dependency>
package demo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;

public class SolrJTest {

    /**
     * 更新+添加
     * @throws SolrServerException
     * @throws IOException
     */
    @Test
    public void addDocument() throws SolrServerException, IOException {
        // 建连接;
        SolrServer solrServer = new HttpSolrServer("http://10.198.1.2:8080/solr");
        // 建文档对象;
        SolrInputDocument document = new SolrInputDocument();
        document.addField("id", "ok123");
        document.addField("title", "今天心情不错,写博客几百篇!");
        // document.addField("color", "green");
        // 文档写入索引库
        solrServer.add(document);
        // 提交
        solrServer.commit();

    }


    @Test
    public void deleteDocument() throws SolrServerException, IOException {
        // 两种删除的方式
        List<String> a  = new ArrayList<String>();
        a.add("test0021");
        a.add("ok123");
        SolrServer solrServer = new HttpSolrServer("http://10.198.1.2:8080/solr");
        solrServer.deleteById(a);
        solrServer.commit();
        //solrServer.deleteByQuery("*:*");  
    }

}

其中,需要注意的是:document.addField(String name , Object value)中的第一个参数的值必须在自己的core下面的schema.xml中有定义,如:

<fieldtype name="string"  class="solr.StrField" sortMissingLast="true" omitNorms="true"/>
   <fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/>
   <fieldType name="int" class="solr.TrieIntField" precisionStep="0" omitNorms="true" positionIncrementGap="0"/>
   <!-- 我添加的IK分词 -->
     <fieldType name="text_ik" class="solr.TextField">   
               <analyzer type="index" isMaxWordLength="false" class="org.wltea.analyzer.lucene.IKAnalyzer"/>   
               <analyzer type="query" isMaxWordLength="true" class="org.wltea.analyzer.lucene.IKAnalyzer"/>   
     </fieldType>
  <!-- general -->
  <field name="id"        type="text_ik"   indexed="true"  stored="true"  multiValued="false" required="true"/>
  <field name="type"      type="string"   indexed="true"  stored="true"  multiValued="false" /> 
  <field name="name"      type="string"   indexed="true"  stored="true"  multiValued="false" /> 
  <field name="core0"     type="string"   indexed="true"  stored="true"  multiValued="false" /> 
  <field name="_version_" type="long"     indexed="true"  stored="true"/>

   <field name="title" type="text_ik" indexed="true" stored="true" required="true" multiValued="false" />
   <field name="category" type="int" indexed="true" stored="true" required="false" multiValued="false" />
   <field name="content" type="text_ik" indexed="true" stored="true" required="false" multiValued="false" />
   <field name="color" type="string" indexed="true" stored="true" required="false" multiValued="false" />
   <field name="orderBy" type="int" indexed="true" stored="true" required="false" multiValued="false" />

我上面程序中添加域的时候包含id和title,在我的schema.xml中都可以找到,当然也是可以添加content,color等等的域;

简单介绍了一下solrj的使用,希望对大家有帮助!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值