Java calls the solrj5.5.3 API to query data.
Prepare to build solr Service
Refer to the previous article to build the solr search service.
Add dependency
Add the following dependencies for the maven project,
<!-- https://mvnrepository.com/artifact/org.apache.solr/solr-solrj --><dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> <version>5.5.3</version></dependency>
You can also import the jar package by yourself.
Under the solr installation directory, find the solr-5.5.3 \ dist \ solrj-lib path, add all the jar packages in it to your project, don't forget to have a solr-solrj-5.5.3.jar package in the folder outside, copy it together.
Compile call code
Here is a simple example (including paging) of Spring mvc project:
@ResponseBody @RequestMapping(value = "/searchByKeyWord", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json; charset=utf-8") public String search(HttpServletRequest request, HttpServletResponse response, String keyWord, int pageSize, int pageOffset) { try { String urlString = ConfigUtil.getValueByKey("solrServiceUrl"); SolrClient solr = new HttpSolrClient(urlString); SolrQuery query = new SolrQuery(); query.set("q", URLDecoder.decode(keyWord, "UTF-8")); query.set("start", String.valueOf(pageOffset * pageSize)); query.set("rows", String.valueOf(pageSize)); QueryResponse queryResponse = solr.query(query); // SolrDocumentList list = queryResponse.getResults(); Map<String, Object> result = new HashMap<String, Object>(); List<OCSearchObject> rstList = queryResponse.getBeans(OCSearchObject.class); result.put("success", true); result.put("key", URLDecoder.decode(keyWord, "UTF-8")); result.put("list", rstList); result.put("totalCount", queryResponse.getResults().getNumFound()); solr.close(); return json(result); } catch (Exception e) { LOGGER.error(e.toString(), e); return jsonpFalse(request, response, "code_error"); } }
The following is the OCSearchObject class, pay attention to the data-config.xml configuration fields correspond to, there is also do not forget to add @ Field annotation in front of the Field (org. apache. solr. client. solrj. beans. field ).
public class OCSearchObject{ private Long id; private String name; private String fullText; private String type; /** * @return the id */ public Long getId() { return id; } /** * @param id the id to set */ @Field("id") public void setId(Long id) { this.id = id; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ @Field("name") public void setName(String name) { this.name = name; } /** * @return the fullText */ public String getFullText() { return fullText; } /** * @param fullText the fullText to set */ @Field("fullText") public void setFullText(String fullText) { this.fullText = fullText; } /** * @return the type */ public String getType() { return type; } /** * @param type the type to set */ @Field("type") public void setType(String type) { this.type = type; }}Front-end example
For other specific extensions based on business needs, you can customize the displayed data on the front-end (highlighted in red with keywords, each record category, and click to jump to the record details page ).