solr 6.3.0 使用实例

just do it

创建core

  1. 找到安装目录…\solr-6.3.0\server\solr\configsets\sample_techproducts_configs下的conf文件夹,拷贝。
    回退到目录…\solr-6.3.0\server\solr,新建文件夹new_core,进入,将拷贝的conf文件夹粘贴在这里。
    这里写图片描述

  2. 浏览器访问solr首页,core admin——add core——add core——core selector——new_core
    这里写图片描述

  3. 查询,query——q:something——excute query
    这里写图片描述
    因为是新建的core,没有数据,所以response为0。接下来就是导入数据了

在java web项目中集成solr

  1. pom.xml中配置依赖
<!--solr相关包 -->
        <dependency>
            <groupId>org.apache.solr</groupId>
            <artifactId>solr-solrj</artifactId>
            <version>6.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.7.7</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.woodstox</groupId>
            <artifactId>woodstox-core-asl</artifactId>
            <version>4.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.noggit</groupId>
            <artifactId>noggit</artifactId>
            <version>0.6</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.woodstox</groupId>
            <artifactId>stax2-api</artifactId>
            <version>3.1.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
        </dependency>
        <!-- 分词器 -->
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-analyzers-common</artifactId>
            <version>6.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-analyzers-kuromoji</artifactId>
            <version>6.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-analyzers-phonetic</artifactId>
            <version>6.3.0</version>
        </dependency>

        <!-- solr相关的包结束 -->
  1. 编码solrClient.java
public class SolrClient {
    // solr所在服务器链接
    private String solrURL;

    // core名
    private String coreName;

    // 单节点连接对象
    private SolrClient solrClient;

    // 高亮标记开始符
    private String lightSimplePre = "<em>";

    // 高亮标记结束符
    private String lightSimplePost = "</em>";

    BlockingQueue<HttpSolrClient> httpSolrClientQueue = null;

    Producer producer = null;

    /**
     * <默认构造函数>
     */
    public SolrClient(String solrURL, String coreName, int maxClient) {
        this.solrURL = solrURL;
        this.coreName = coreName;
        // 初始化阻塞对了,用于存放solr单节点连接对象
        httpSolrClientQueue = new LinkedBlockingQueue<HttpSolrClient>(maxClient);
        producer = new Producer();
        producer.start();
        solrClient = this;
    }

    /**
     * 创建单节点客户端
     * 
     * @param solrURL
     * @param coreName
     */
    private HttpSolrClient loadHttpSolrClient(String solrURL, String coreName) {
        // 单节点客户端
        HttpSolrClient httpSolrClient = null;
        String URL = solrURL + "/" + coreName;
        httpSolrClient = new HttpSolrClient.Builder(URL).build();
        httpSolrClient.setConnectionTimeout(600000);
        // 正常情况下,以下参数无须设置
        // 使用老版本solrj操作新版本的solr时,因为两个版本的javabin incompatible,所以需要设置Parser
        httpSolrClient.setParser(new XMLResponseParser());
        // socket read timeout
        httpSolrClient.setSoTimeout(600000);
        httpSolrClient.setDefaultMaxConnectionsPerHost(100);
        httpSolrClient.setMaxTotalConnections(100);
        // defaults to false
        httpSolrClient.setFollowRedirects(false);
        // allowCompression defaults to false.
        // Server side must support gzip or deflate for this to have any effect.
        httpSolrClient.setAllowCompression(true);
        // 使用SolrQuery传递参数,SolrQuery的封装性更好
        httpSolrClient.setRequestWriter(new BinaryRequestWriter());
        return httpSolrClient;
    }

    /**
     * 获取solr连接
     * 
     * @return solr连接对象
     * @author HuBin
     * @time 2017年11月2日 上午9:09:16
     * @return HttpSolrClient [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public HttpSolrClient getHttpSolrClient() {
        HttpSolrClient httpSolrClient = null;
        try {
            httpSolrClient = httpSolrClientQueue.take();
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
        return httpSolrClient;
    }

    /**
     * 内部类,一个生产者线程,不停的生产HttpSolrClient保存到阻塞队列中。
     * 
     * @author HuBin
     * @version [版本号, 2017年10月30日]
     * @see [相关类/方法]
     * @since [产品/模块版本]
     */
    class Producer extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    // 队列装满后自动处于阻塞状态
                    httpSolrClientQueue.put(solrClient.loadHttpSolrClient(solrURL, coreName));
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 高亮配置
     * 
     * @param query 搜索参数对象
     * @author HuBin
     * @time 2017年11月2日 上午9:27:23
     * @return void [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    private void hightLightConfig(SolrQuery query) {
        // 开启高亮组件
        query.setHighlight(true);
        // 高亮字段(多个字段用逗号隔开)
        query.addHighlightField("titles,targeturl,realIP,content");
        // 设置高亮标记符
        query.setHighlightSimplePre(lightSimplePre);
        query.setHighlightSimplePost(lightSimplePost);
        // 结果分片数,默认为1
        query.setHighlightSnippets(3);
        // 每个分片的最大长度,默认为100
        query.setHighlightFragsize(60);
    }

    /**
     * 查询操作
     * 
     * @param message 查询的信息
     * @param starRow 从多少行开始
     * @param pageSize 每页显示多少条信息
     * @return
     * @throws SolrServerException
     * @throws RemoteSolrException
     * @throws SolrException
     * @throws IOException [参数说明]
     * @author HuBin
     * @time 2017年11月2日 上午9:39:34
     * @return JSONObject [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public JSONObject search(String message, int startRow, int pageSize)
            throws SolrServerException, RemoteSolrException, SolrException, IOException {
        SolrQuery query = new SolrQuery();
        // 设置查询内容
        query.setParam("q", filtMessage(message));
        // 设置默认查询字段
        query.setParam("df", "content");
        query.setStart(startRow);
        query.setRows(pageSize);
        // 设置高亮
        hightLightConfig(query);
        query.setParam("tf", "1");
        HttpSolrClient httpSolrClient = solrClient.getHttpSolrClient();
        QueryResponse response = httpSolrClient.query(query);
        httpSolrClient.close();
        JSONObject result = analyseResult(response);
        return result;
    }

    /**
     * 结果进行分析
     * 
     * @param response
     * @return [参数说明]
     * @author HuBin
     * @time 2017年11月2日 上午9:38:39
     * @return JSONObject [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    private JSONObject analyseResult(QueryResponse response) {
        // 获取高亮的结果
        // 获取高亮的结果
        Map<String, Map<String, List<String>>> highlightMap = response.getHighlighting();
        // 获取查询的结果
        SolrDocumentList docs = response.getResults();
        JSONObject json = new JSONObject();
        json.put("highlight", highlightMap);
        json.put("docs", docs);
        json.put("total", docs.getNumFound());
        return json;
    }

    private String filtMessage(String message) {
        message = message.replaceAll(":", " ")
                .replaceAll("\\?", " ")
                .replaceAll("\\+", " ")
                .replaceAll("-", " ")
                .replaceAll("~", " ")
                .replaceAll("!", " ")
                .replaceAll("\\^", " ")
                .replaceAll("/", " ")
                .replaceAll("\\{", " ")
                .replaceAll("}", " ")
                .replaceAll("\\[", " ")
                .replaceAll("]", " ")
                .replaceAll("\\(", " ")
                .replaceAll("\\)", " ")
                .replaceAll("\"", " ")
                .replaceAll("\\\\", " ")
                .replaceAll("\\&", " ")
                .replaceAll("\\*", " ")
                .replaceAll("\\|", " ")
                .trim();
        return message;
    }

    /**
     * 向solr中添加一条记录
     * 
     * @param obj
     * @throws IOException
     * @throws SolrServerException [参数说明]
     * @author HuBin
     * @time 2017年11月2日 上午10:09:40
     * @return void [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public void addField(Object obj) throws IOException, SolrServerException {
        HttpSolrClient httpSolrClient = solrClient.getHttpSolrClient();
        httpSolrClient.addBean(obj);
        httpSolrClient.commit();
        httpSolrClient.close();
    }

    public static void main(String[] args) throws RemoteSolrException, SolrException, SolrServerException, IOException {
        String solrURL = "http://localhost:8983/solr";
        String coreName = "aepcore1";
        SolrClient solrClient = new SolrClient(solrURL, coreName, 20);
        TFulltext tf = new TFulltext();
        tf.setContent("美国 10.0.1.28 http://www.qq.com 云存储");
        tf.setCreateTime(new Date());
        tf.setLocation("美国");
        tf.setRealIP("10.0.1.28");
        tf.setTitles("云存储");
        tf.setTargetId(520);
        tf.setTargeturl("http://www.qq.com");
        // solrClient.addField(tf);
        JSONObject json = solrClient.search("驱动", 0, 10);
        System.out.println(json);
        solrClient.producer.stop();
    }
}
  1. dispatcher-servlet.xml中配置bean
<!-- solr配置 -->
    <bean id="solrClient" class="com.kylin.aep.solr.SolrClient"
        scope="singleton">
        <constructor-arg name="solrURL" value="${solr.url}" />
        <constructor-arg name="coreName" value="${solr.core}" />
        <constructor-arg name="maxClient" value="${solr.max.client}" />
    </bean>

solr服务器的相关配置,配置在app.properties中
dispatcher-servlet.xml:

<bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="true" />
        <property name="locations">
            <list>
                <!-- 这里支持多种寻址方式:classpath和file -->
                <value>classpath:config/app.properties</value>
                <!-- 推荐使用file的方式引入,这样可以将配置和代码分离 -->
                <!--<value>file:/opt/demo/config/demo-remote.properties</value> -->
            </list>
        </property>
    </bean>

app.properties:

#config of solr
solr.url=http://10.0.1.231:8983/solr
solr.core=aepcore1
solr.max.client=300
  1. 录入数据与检索
public static void main(String[] args) throws RemoteSolrException, SolrException, SolrServerException, IOException {
        String solrURL = "http://localhost:8983/solr";
        String coreName = "aepcore1";
        SolrClient solrClient = new SolrClient(solrURL, coreName, 20);
        TFulltext tf = new TFulltext();
        tf.setContent("美国 10.0.1.28 http://www.qq.com 云存储");
        tf.setCreateTime(new Date());
        tf.setLocation("美国");
        tf.setRealIP("10.0.1.28");
        tf.setTitles("云存储");
        tf.setTargetId(520);
        tf.setTargeturl("http://www.qq.com");
        // solrClient.addField(tf);
        JSONObject json = solrClient.search("驱动", 0, 10);
        System.out.println(json);
        solrClient.producer.stop();
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值