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工具类
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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); } }
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控制台查看