solrJ

solrJ 基本使用

  • 周末,随便写点东西,介绍介绍solrJ的基本使用,直接上代码:
  • 创建索引:

    public static void main(String[] args) throws IOException, SolrServerException {
        //初始化索引连接的时候,可以根绝实际情况优化连接,这边只是一个main方法demo
        HttpSolrServer server = new HttpSolrServer("http://localhost:8180/solr/xmcore");
        for (int i = 0; i < 10; ++i) {
            SolrInputDocument doc = new SolrInputDocument();
            //与schame.xml中field对应
            doc.addField("id", "100"+i);
            doc.addField("name", "xiaomengge"+i);
            doc.addField("age", 30);
            doc.addField("address", "江苏南京苏宁易购物品质量很好"+i);
            server.add(doc);
            if (i % 100 == 0)
                server.commit(); // periodically flush
        }
        //      SolrInputDocument doc = new SolrInputDocument();
        //      doc.addField("id", "1000");
        //      doc.addField("name", "测试分词");
        //      doc.addField("age", 30);
        //      doc.addField("address", "江苏南京苏宁易购物品质量很好");
        //      server.add(doc);
    
        //      SolrInputDocument doc = new SolrInputDocument();
        //      doc.addField("id", "1001");
        //      doc.addField("name", "测试分词1");
        //      doc.addField("age", 30);
        //      doc.addField("address", "Pigeon贝亲奶瓶清洁剂150ml");
        //      server.add(doc);
        server.commit();
    }
    
  • 查询索引:

    public static void main(String[] args) throws MalformedURLException, SolrServerException {
        HttpSolrServer solr = new HttpSolrServer("http://localhost:8180/solr/xmcore");  
        SolrQuery params = new SolrQuery();
    
        //两种查询方式,一种是params.set("q", "address:钢铁侠"); qf,defType可以忽略 ,推荐下面这种查询方式,可以设置field的权重
        params.set("q", "钢铁侠");
        params.set("qf", "address^0.8 book_name^0.7");
        params.set("defType", "edismax");
        params.set("start", "0");
    
        params.addFilterQuery("suggest:钢铁侠  AND suggest:复仇者");
        params.addFilterQuery("querytext:aaa  OR querytext:bbb");
    
        QueryResponse response = solr.query(params);
        SolrDocumentList results = response.getResults();
        for (int i = 0; i < results.size(); ++i) {
        System.out.println(results.get(i));
        }
    }
    
  • 单例方式初始化SolrCloud连接

    import org.apache.solr.client.solrj.impl.CloudSolrServer;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    
    public class SolrClientFactory {
    
         private static final Logger LOGGER = LoggerFactory.getLogger(SolrClientFactory.class);
    
           private static SolrClientFactory solrClientFactory = null;
    
            private static CloudSolrServer cloudSolrServer = null;
    
            private SolrClientFactory() {}
    
            public static synchronized SolrClientFactory getInstance() {
                if (solrClientFactory == null) {
                    solrClientFactory = new SolrClientFactory();
                }
                return solrClientFactory;
            }
    
        public void initSolrClient() {
            LOGGER.info("init solr client");
            //ase.query.solrclient.url 为集群中zookeeper的IP与端口
            //ase.query.solrclient.url=172.172.177.47:2181,172.172.177.47:2281,172.172.177.47:2381
            //ase.product.collection=product
            //ase.product.zkclienttimeout=2000000
            //ase.product.zkconnectiontimeout=2000000
            cloudSolrServer = new CloudSolrServer(PropertyUtil.getValue("ase.query.solrclient.url"));
            cloudSolrServer.setDefaultCollection(PropertyUtil.getValue("ase.product.collection"));
            cloudSolrServer.setZkClientTimeout(Integer.parseInt(PropertyUtil.getValue("ase.product.zkclienttimeout")));
            cloudSolrServer.setZkConnectTimeout(Integer.parseInt(PropertyUtil.getValue("ase.product.zkconnectiontimeout")));
        }
    
        public static CloudSolrServer getCloudSolrServer(){
    
            if( null == cloudSolrServer){
                getInstance().initSolrClient();
            }
            return cloudSolrServer;
        }
    }
    
  • SolrCloud调用:

    SolrClientFactory.getInstance().initSolrClient();  //可以容器启动时候初始化
    
    //查询索引
    CloudSolrServer cloudSolrServer = SolrClientFactory.getCloudSolrServer();
    response = cloudSolrServer.query(sQuery);
    
    //添加索引
    Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
    //......设置docs 的值
    CloudSolrServer cloudSolrServer = SolrClientFactory.getCloudSolrServer();
    cloudSolrServer.add(docs);
    cloudSolrServer.commit();
    

solr使用过程中发现的问题:

  • IK扩展词库中添加单词,做Analysis分析的时候完全正确,可以是就是查询不出索引记录,后来试验了一晚上。终于在一些大神的指导下,发现了问题的所以,词库添加扩展之后,一定要把之前创建的索引删除,重新创建索引
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值