1、添加pom.xml
4.0.0 com.yangwj demo 0.0.1-SNAPSHOT jar demo Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.1.0.RELEASE UTF-8 UTF-8 1.8 Greenwich.M1 org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.boot spring-boot-starter-test test org.apache.solr solr-solrj 7.7.1 org.apache.httpcomponents httpmime 4.5.7 org.apache.httpcomponents httpclient 4.5.6 org.apache.httpcomponents httpcore 4.4.10 org.noggit noggit 0.8 commons-io commons-io 1.4 org.codehaus.woodstox stax2-api 3.1.4 org.codehaus.woodstox woodstox-core-asl 4.4.1 org.slf4j slf4j-simple 1.7.7 commons-logging commons-logging 1.0.4 org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin spring-milestones Spring Milestones https://repo.spring.io/milestone false
2、创建Person.java类
package com.yangwj.demo.Solr;import org.apache.solr.client.solrj.beans.Field;public class Person { @Field(value="id") private String id; @Field(value="name") private String name; @Field(value="description") private String description; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; }}
3、创建SolrUtil.java文件
package com.yangwj.demo.Solr;import java.io.IOException;import java.util.List;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.impl.HttpSolrClient.Builder;import org.apache.solr.client.solrj.response.QueryResponse;import org.apache.solr.common.SolrDocument;import org.apache.solr.common.SolrDocumentList;import org.apache.solr.common.SolrInputDocument;public class SolrUtil { //指定solr服务器的地址 private final static String SOLR_URL = "http://192.168.56.130:8983/solr/"; /** * 创建SolrServer对象 * * 该对象有两个可以使用,都是线程安全的 * 1、CommonsHttpSolrServer:启动web服务器使用的,通过http请求的 * 2、 EmbeddedSolrServer:内嵌式的,导入solr的jar包就可以使用了 * 3、solr 4.0之后好像添加了不少东西,其中CommonsHttpSolrServer这个类改名为HttpSolrClient * * @return */ public HttpSolrClient createSolrServer(){ HttpSolrClient solr = null; solr = new HttpSolrClient.Builder(SOLR_URL).withConnectionTimeout(10000).withSocketTimeout(60000).build(); return solr; } /** * 往索引库添加文档 * @throws IOException * @throws SolrServerException */ public void addDoc() throws SolrServerException, IOException{ //构造一篇文档 SolrInputDocument document = new SolrInputDocument(); //往doc中添加字段,在客户端这边添加的字段必须在服务端中有过定义 document.addField("id", "9"); document.addField("name", "yangwenjie"); document.addField("description", "a code man"); //获得一个solr服务端的请求,去提交 ,选择具体的某一个solr core HttpSolrClient solr = new HttpSolrClient.Builder(SOLR_URL + "new_core").withConnectionTimeout(10000).withSocketTimeout(60000).build(); solr.add(document); solr.commit(); solr.close(); } /** * 根据id从索引库删除文档 */ public void deleteDocumentById() throws Exception { //选择具体的某一个solr core HttpSolrClient server = new HttpSolrClient.Builder(SOLR_URL + "new_core").withConnectionTimeout(10000).withSocketTimeout(60000).build(); //删除文档 server.deleteById("8"); //删除所有的索引 //solr.deleteByQuery("*:*"); //提交修改 server.commit(); server.close(); } /** * 查询 * @throws Exception */ public void querySolr() throws Exception{ HttpSolrClient solrServer = new HttpSolrClient.Builder(SOLR_URL + "new_core/").withConnectionTimeout(10000).withSocketTimeout(60000).build(); SolrQuery query = new SolrQuery(); //下面设置solr查询参数 //query.set("q", "*:*");// 参数q 查询所有 query.set("q","yangwenjie");//相关查询,比如某条数据某个字段含有周、星、驰三个字 将会查询出来 ,这个作用适用于联想查询 //参数fq, 给query增加过滤查询条件 query.addFilterQuery("id:[0 TO 9]");//id为0-4 //给query增加布尔过滤条件 //query.addFilterQuery("description:演员"); //description字段中含有“演员”两字的数据 //参数df,给query设置默认搜索域 query.set("df", "name"); //参数sort,设置返回结果的排序规则 query.setSort("id",SolrQuery.ORDER.desc); //设置分页参数 query.setStart(0); query.setRows(10);//每一页多少值 //参数hl,设置高亮 query.setHighlight(true); //设置高亮的字段 query.addHighlightField("name"); //设置高亮的样式 query.setHighlightSimplePre(""); query.setHighlightSimplePost(""); //获取查询结果 QueryResponse response = solrServer.query(query); //两种结果获取:得到文档集合或者实体对象 //查询得到文档的集合 SolrDocumentList solrDocumentList = response.getResults(); System.out.println("通过文档集合获取查询的结果"); System.out.println("查询结果的总数量:" + solrDocumentList.getNumFound()); //遍历列表 for (SolrDocument doc : solrDocumentList) { System.out.println("id:"+doc.get("id")+" name:"+doc.get("name")+" description:"+doc.get("description")); } //得到实体对象 ListtmpLists = response.getBeans(Person.class); if(tmpLists!=null && tmpLists.size()>0){ System.out.println("通过文档集合获取查询的结果"); for(Person per:tmpLists){ System.out.println("id:"+per.getId()+" name:"+per.getName()+" description:"+per.getDescription()); } } } public static void main(String[] args) throws Exception { SolrUtil solr = new SolrUtil(); //solr.createSolrServer(); //solr.addDoc(); //solr.deleteDocumentById(); solr.querySolr(); }}
4、如果出现错误,一般是pom.xml中的jar包太低,因此需要更高版本的包。