I. Introduction
solr mode of operation there are three:
1, using Solrj operation, for JavaSE program development .
2, using the spring-data-solr operates for program development SSM .
3, using springboot plug springboot-starter-data-solr operates for springboot program development .
Second, develop code using Solrj
1, support the introduction of Maven
<dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> <version>8.2.0</version> </dependency>
2, for the preparation of Solr CRUD code. Examples are as follows:
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 org.apache.solr.common.SolrInputDocument; import java.io.IOException; /** * Created by David on 2019/9/6. */ public class TestSolr { private static final String solrUrl = "http://localhost:8983/solr/articles"; // add information & modification information public void addSolr () throws SolrServerException, IOException { HttpSolrClient solrClient = new new HttpSolrClient.Builder (solrUrl) .withConnectionTimeout ( 10000 ) .withSocketTimeout ( 60000 ) .build (); SolrInputDocument Document = new new SolrInputDocument () ; // NOTE: id field could not less document.addField ( "ID", "10" ); document.addField ( "title", "java-based object-oriented programming" ); solrClient.add (Document); solrClient.commit (); } // delete information public void delSolr () throws IOException, SolrServerException { HttpSolrClient solrClient = new new HttpSolrClient.Builder (solrUrl) .withConnectionTimeout ( 10000 ) .withSocketTimeout ( 60000 ) .build (); solrClient.deleteById ( "10"); // delete solrClient.commit (); // submit } // query information public void searchSolr () throws IOException, SolrServerException { HttpSolrClient solrClient = new new HttpSolrClient.Builder (solrUrl) .withConnectionTimeout ( 10000 ) .withSocketTimeout ( 60000 ) .build (); SolrQuery Query = new new SolrQuery (); query.set ( "Q", " name: programming " ); // call query method server, query index database QueryResponse the Response = solrClient.query (query); // query results SolrDocumentList results = response.getResults (); // query total number of results Long CNT = results.getNumFound (); System.out.println ( "Total Results:" + CNT); for (SolrDocument solrDocument: Results) { System.out.println ( solrDocument.get ( "ID" )); System.out.println (solrDocument.get ( "title" )); } } }
三、使用spring-data-solr开发代码
1, the introduction Maven support different versions of spring-data-solr operation codes do not match, the article with the latest version 4.0.
Note: the Spring version 4.0 for the corresponding version 5.1.9, springMVC with the same version to be, otherwise it will report an error.
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-solr</artifactId> <version>4.0.10.RELEASE</version> </dependency>
2, the preparation POJOs, wherein @Id primary key identifier, @ Indexed Field is filled in Solr entry identifier. Early in spring-data-solrs and annotations solrj used are: @Field
@SolrDocument(solrCoreName = "articles") public class Articles implements Serializable { @Id @Indexed private int id; @Indexed private String title; private String content; @Indexed private Date createDate; }
3, the preparation of spring profile spring-solr.xml, note addresses only references to http: // localhost: 8983 / solr, because the project may require multiple Collection.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- solr服务器地址 --> <bean id="solrClientFactory" class="org.springframework.data.solr.server.support.HttpSolrClientFactoryBean"> <property name="url" value="http://localhost:8983/solr"/> <property name="timeout" value="5000"/> <property name="maxConnections" value="100"/> </bean> <!-- solr模板 --> <bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate"> <constructor-arg index="0" ref="solrClientFactory"/> </bean> </beans>
4, controller codes, when attention need to specify the operation Collection.
import org.apache.solr.client.solrj.response.UpdateResponse; import org.lanqiao.entity.Articles; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Sort; import org.springframework.data.solr.core.SolrTemplate; import org.springframework.data.solr.core.query.Criteria; import org.springframework.data.solr.core.query.Query; import org.springframework.data.solr.core.query.SimpleQuery; import org.springframework.data.solr.core.query.result.ScoredPage; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.Date; import java.util.List; import java.util.Optional; /** * Created by David on 2019/9/7. */ @Controller public class ArticlesController { @Autowired private SolrTemplate solrTemplate; @RequestMapping("/save") @ResponseBody public String keywordsSearchSpring() { Articles articles = new Articles(); articles.setId(11); articles.setTitle("山东济南商城"); articles.setCreateDate(new Date()); solrTemplate.saveBean("articles",articles); solrTemplate.commit("articles"); return "success save !"; } @RequestMapping("/get") @ResponseBody public Articles selectById() { Optional<Articles> optional = solrTemplate.getById("articles", 1, Articles.class); return optional.get();//optional jdk1.8+ null pointer solve the problem. } @RequestMapping("/del") @ResponseBody public int delById() { UpdateResponse updateResponse = solrTemplate.deleteByIds("articles","11"); return updateResponse.getStatus();//0 表示成功 } @RequestMapping("/select") @ResponseBody public List<Articles> select() { // 查询所有 Query query = new SimpleQuery(); // 设置条件 Criteria criteria = new Criteria("title").is("Jilin " ); query.addCriteria (Criteria); // set page query.setOffset (0L); // start index (default 0) query.setRows (2); // Hits per page (default 10) // sets the collation Sort sort = new new the Sort (Sort.Direction.ASC, "createDate" ); query.addSort (Sort); // query ScoredPage <Articles> = solrTemplate.queryForPage Pages ( "Articles", query, Articles. class ); the System.out. the println ( "pages.getTotalElements () =" + pages.getTotalElements ()); List <Articles>content =pages.getContent (); return Content; } }
Knowledge is not described in detail here SpringMVC part of the program list is as follows:
Three, springboot Plug-in Development
springboot plug-in default is to use solrj of SolrClient object implementation solr operation. See blog: https://www.jianshu.com/p/05a161add1a6
Personally I think spring-data-solr of SolrTemplate more suitable for development, so the focus here talk about how to change the default solrj spring-data-solr.
1, support the introduction of Maven
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-solr</artifactId> </dependency>
I.e. it can be seen that the introduction of support Solrj also supported spring-data-solr Solr implemented operations.
2, application.properties configuration file, using the spring-data-solr arranged only path to the root.
# solr配置 spring.data.solr.host=http://localhost:8983/solr
3, the preparation of config files, the package is Solrj the SolrClinet SolrTemplate spring-data-solr of.
import org.apache.solr.client.solrj.SolrClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.solr.core.SolrTemplate; @Configuration public class MySolrConfig { @Autowired SolrClient solrClient; @Bean public SolrTemplate getSolrTemplate() { return new SolrTemplate(solrClient); } }
这样就可以使用SolrTemplate对象来操作Solr了,controller中的操作方法和entity中的对象都不用改变,同上面的代码。
程序清单:
POJO代码:
controller代码:
参考文章:
https://blog.csdn.net/likemebee/article/details/78469002
https://www.jianshu.com/p/05a161add1a6
https://blog.csdn.net/zhuzg2005/article/details/89598925