Solr7.2 single-host code query instance, solr7.2 single-host instance

Source: Internet
Author: User
Tags solr

Solr7.2 single-host code query instance, solr7.2 single-host instance
Solr uses code instances

Solr is well-known in the search field. I 've been reading this part on and off recently. I don't want to talk about the theory first. I'll record the code on the disk:

Spring configuration file

     
         
      
  
 

Controller

@ Autowired private SearchService searchService; @ RequestMapping ("/q") @ ResponseBody public TaotaoResult search (@ RequestParam (defaultValue = "") String keyword, @ RequestParam (defaultValue = "1 ") integer page, @ RequestParam (defaultValue = "30") Integer rows) {// convert String try {keyword = new String (keyword. getBytes ("iso8859-1"), "UTF-8");} catch (UnsupportedEncodingException e) {e. printStackTrace ();} SearchResult searchResult = null; try {searchResult = searchService. search (keyword, page, rows);} catch (Exception e) {e. printStackTrace (); return TaotaoResult. build (500, ExceptionUtil. getStackTrace (e);} return TaotaoResult. OK (searchResult );}

Service implementation

@ Servicepublic class SearchServiceImpl implements SearchService {@ Autowired private SearchDao searchDao; @ Override public SearchResult search (String queryString, int page, int rows) throws Exception {// create a query condition SolrQuery query = new SolrQuery (); // set a query condition. setQuery (queryString); // sets the paging condition query. setStart (page-1) * rows); query. setRows (rows); // sets the default search domain query. set ("df", "item_title"); // sets the highlighted query. setHighlight (true); query. addHighlightField ("item_title"); query. setHighlightSimplePre (""); query. setHighlightSimplePost (""); // execute the SearchResult searchResult = searchDao. search (query); // calculate the total number of pages Long recordCount = searchResult. getRecordCount (); int pageCount = (int) (recordCount/rows); if (recordCount % rows> 0) {pageCount ++;} searchResult. setPageCount (pageCount); searchResult. setCurPage (page); return searchResult ;}}

* Dao implementation

@ Repositorypublic class SearchDaoImpl implements SearchDao {@ Autowired private SolrServer solrServer; @ Override public SearchResult search (SolrQuery query) throws Exception {// execute QueryResponse response = solrServer. query (query); // obtain the query result list SolrDocumentList solrDocumentList = response. getResults (); List
 
  
ItemList = new ArrayList <> (); for (SolrDocument solrDocument: solrDocumentList) {// create a SearchItem object SearchItem item = new SearchItem (); item. setNameCategoryName (String) solrDocument. get ("item_category_name"); item. setId (String) solrDocument. get ("id"); item. setImage (String) solrDocument. get ("item_image"); item. setPrice (Long) solrDocument. get ("item_price"); item. setSellPoint (String) solrDocument. get ("item_sell_point"); // retrieves the highlighted Map
  
   
> Highlighting = response. getHighlighting (); List
   
    
List = highlighting. get (solrDocument. get ("id"). get ("item_title"); String itemTitle = ""; if (list! = Null & list. size ()> 0) {// obtain the highlighted result itemTitle = list. get (0);} else {itemTitle = (String) solrDocument. get ("item_title");} item. setTitle (itemTitle); // Add it to the itemList. add (item) ;}searchresult result = new SearchResult (); result. setItemList (itemList); // The total number of query results. setRecordCount (solrDocumentList. getNumFound (); return result ;}}
   
  
 

* Test class

@ Test public void testSolrJ () {// create a connection to SolrServer solrServer = new HttpSolrServer ("https: // 192.168.6.57: 8983/solr/core2 "); // create a document Object SolrInputDocument document = new SolrInputDocument (); // Add the domain document. addField ("id", "solrtest01"); document. addField ("item_title", "test item"); document. addField ("item_sell_point", ""); // Add it to the index library try {solrServer. add (document); // submit solrServer. commit ();} catch (SolrServerException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace () ;}@ Test public void testQuery () {SolrServer solrServer = new HttpSolrServer ("https: // 192.168.6.57: 8983/solr/core2 "); // create a query object SolrQuery query = new SolrQuery (); query. setQuery ("*: *"); // execute the query try {QueryResponse response = solrServer. query (query); SolrDocumentList solrDocumentsList = response. getResults (); for (SolrDocument solrdocument: solrDocumentsList) {System. out. println (solrdocument. get ("id"); System. out. println (solrdocument. get ("item_title"); System. out. println (solrdocument. get ("item_sell_point");} catch (SolrServerException e) {e. printStackTrace ();}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.