java中使用solr

java中使用solr

1.pom文件中导入solr jar包

     org.slf4j

     slf4j-log4j12

      1.7.7

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中文分词器

java中使用solr_第1张图片

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 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 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);
    }
}

 

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

    }
}

也可在solr控制台查看

java中使用solr_第2张图片

 

你可能感兴趣的:(java)