搜索引擎
引入pom
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-solr</artifactId> </dependency>
|
设置application.yml
1 2 3 4
| spring: data: solr: host: http://192.168.1.210:8983/solr
|
使用
service接口
1 2 3 4 5 6
| public interface IUserService { String add(); SolrDocumentList selectData(String condition); }
|
service实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| @Service public class UserServiceImpl implements IUserService{
@Autowired SolrClient solrClient;
@Override public String add() { SolrInputDocument document = new SolrInputDocument(); document.addField("bookName","《java数据结构》"); document.addField("bookPrice","32.9"); document.addField("bookAmount","32"); try { solrClient.add("jtxyh",document); solrClient.commit("jtxyh"); return "ok"; } catch (Exception e) { e.printStackTrace(); } return "error"; }
@Override public SolrDocumentList selectData(String condition) { SolrQuery queryCondition = new SolrQuery(); queryCondition.setQuery(condition); try { QueryResponse queryResponse = solrClient.query("jtxyh",queryCondition); SolrDocumentList results = queryResponse.getResults(); results.forEach(System.out::println); return results; } catch (Exception e) { e.printStackTrace(); } return null; } }
|
Handler
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| @RestController public class UserHandler {
@Autowired private IUserService userService;
@GetMapping("add") public String add(){ return userService.add(); }
@GetMapping("select/{condition}") public SolrDocumentList selectData(@PathVariable("condition") String condition){ return userService.selectData(condition); } }
|
相关文章
数据库连接池
SpringIOC
Junit和Spring
Tomcat
Servlet
Request,Response和ServletContext
Cookie和Session
JSP和EL和Jstl
Filter和Listener
Mybatis