码迷,mamicode.com
首页 > 编程语言 > 详细

java中使用solr

时间:2018-07-06 15:52:57      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:stack   catch   domain   新建   数据   jar包   log4j   ddb   sch   

1.pom文件中导入solr jar包

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.7</version>
</dependency>

2.新建实体类message

package com.example.demo.domain;

import lombok.Data;
import org.apache.solr.client.solrj.beans.Field;

import java.io.Serializable;

@Data
public class Message implements Serializable{

    private static final long serialVersionUID = 1L;

    @Field("id")
    private Integer id;
    @Field("titleName")
    private String titleName;
    @Field("label")
    private String label;
}

3.修改solr core下的managed-schema配置文件   增加你需要的field  注意  需与你实体类中的字段对应 type则为ik中文分词器

技术分享图片

4.新建solrUtil工具类

技术分享图片
package com.example.demo.util;

import com.example.demo.domain.Message;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;

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

public class SolrUtil {

    private String solrUrl = "http://localhost:8983/solr/newcore";

    /**
     * 添加数据
     * @param message
     * @throws IOException
     * @throws SolrServerException
     */
    public void add(Message message) throws IOException, SolrServerException {
        HttpSolrClient client = new HttpSolrClient(solrUrl);
        client.addBean(message);
        client.commit();
    }

    /**
     * 搜索数据
     * @param keyword
     * @param page
     * @param rows
     * @return
     * @throws IOException
     * @throws SolrServerException
     */
    public List<Message> search(String keyword, Integer page, Integer rows) throws IOException, SolrServerException{
        HttpSolrClient client = new HttpSolrClient(solrUrl);
        SolrQuery query = new SolrQuery();
        query.set("q","titleName:"+keyword+"*");//搜索关键字
        query.setStart((page-1) * rows);//设置分页
        query.setRows(rows);//设置查询条数

        QueryResponse response = client.query(query);//执行搜索
        SolrDocumentList resultList = response.getResults();//搜索结果

        List<Message> list = new ArrayList<>();
        for(SolrDocument document:resultList){
            Message message = new Message();
            message.setId(Integer.parseInt(document.get("id").toString()));
            message.setLabel(document.get("label").toString());
            message.setTitleName(document.get("titleName").toString());
            list.add(message);
        }
        return list;
    }

    /**
     * 删除数据
     * @param id
     * @throws IOException
     * @throws SolrServerException
     */
    public void delete(String id) throws IOException, SolrServerException {
        HttpSolrClient client = new HttpSolrClient(solrUrl);
        client.deleteById(id);
    }
}
View Code

5.测试

插入

package com.example.demo.test;

import com.example.demo.domain.Message;
import com.example.demo.util.SolrUtil;

import java.util.List;

public class Test {

    public static void main(String[] args) {
        try{
            SolrUtil util =new SolrUtil();
            Message message = new Message();
            message.setId(2);
            message.setTitleName("标题2");
            message.setLabel("java语言");
            util.add(message);

        }catch (Exception e){
            e.printStackTrace();
        }

    }
}

查询

package com.example.demo.test;

import com.example.demo.domain.Message;
import com.example.demo.util.SolrUtil;

import java.util.List;

public class Test {

    public static void main(String[] args) {
        try{
            SolrUtil util =new SolrUtil();

            List<Message> list = util.search("标",1,5);
            for(Message age : list){
                System.out.println(age.getLabel());
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}

技术分享图片

也可在solr控制台查看

技术分享图片

 

java中使用solr

标签:stack   catch   domain   新建   数据   jar包   log4j   ddb   sch   

原文地址:https://www.cnblogs.com/tony-zt/p/9273647.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!