Solr语法解析及SolrJ使用

本文详细介绍了SolrJ的使用,包括HttpSolrClient的替代、索引的添加、查询和删除,SolrBean的运用,以及SolrCloud的配置和操作。此外,还探讨了Solr的Facet查询和主从同步策略,提供了丰富的示例代码和Solr查询运算符。
摘要由CSDN通过智能技术生成

Solr语法解析及SolrJ使用

标签(空格分隔): Solr


官方指南:http://lucene.apache.org/solr/quickstart.html

Solrj 之HttpSolrClient

旧版本的连接服务HttpSolrServer在solr5版本后已经停用,被新的HttpSolrClient取代,大家可以从下面的url中查看相关的API,了解更详细的变动。
API:http://lucene.apache.org/solr/5_3_1/solr-solrj/index.html

HttpSolrClient  server=new HttpSolrClient(url);
//设置对应请求的目标主机线程数为1000条
server.setDefaultMaxConnectionsPerHost(1000); server.setMaxTotalConnections(10000);
server.setConnectionTimeout(60000);//设置连接超时时间(单位毫秒) 1000
server.setSoTimeout(60000); 设置读数据超时时间(单位毫秒) 1000
server.setFollowRedirects(false);//遵循从定向
server.setAllowCompression(true);//允许压缩

若不使用Maven工程来操作Solr,须添加一下Jar依赖包
image_1b1is8e3b7rt1hlaa121ti1c79.png-62.9kB
若使用Maven工程,需在pom.xml文件中添加下面依赖

<dependency>  
     <artifactId>solr-solrj</artifactId>  
     <groupId>org.apache.solr</groupId>  
     <version>1.4.0</version>  
     <type>jar</type>  
     <scope>compile</scope>  
</dependency> 

定义一个solr server的java代码:

package com.teach.server;

import org.apache.solr.client.solrj.impl.HttpSolrClient;

public class SolrServer {
   

    private static SolrServer solrServer = null;
    private static HttpSolrClient server=null;

    private static String url="http://localhost:8085/solr/test";
    public static synchronized SolrServer getInstance(){
        if(solrServer==null){
            solrServer=new SolrServer();
        }
        return solrServer;
    }

    public static HttpSolrClient getServer(){
        if(server==null){
                server = new HttpSolrClient(url);
                server.setDefaultMaxConnectionsPerHost(1000); 
                server.setMaxTotalConnections(10000);
                server.setConnectionTimeout(60000);//设置连接超时时间(单位毫秒) 1000
                server.setSoTimeout(60000); 设置读数据超时时间(单位毫秒) 1000
                server.setFollowRedirects(false);//遵循从定向
                server.setAllowCompression(true);//允许压缩
        }
        return server;
    }
}

SolrJ添加索引

HttpSolrClient server = SolrServer.getServer();
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "123456");
doc.addField("title_s", "test");
doc.addField("des_s", "test des");
try {
server.add(doc);
server.commit();
} catch (Exception e) {
e.printStackTrace();
}

Id:索引中的唯一ID,定义见配置。
title_s:动态字符串域定义,静态见配置。

SolrJ查询索引

HttpSolrClient server = SolrServer.getServer();
SolrQuery sQuery = new SolrQuery();
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值