Using Solr to realize commodity search function (example explanation)
Later supplement:
Why use Solr service and lunnce?
The question is: when we visit the shopping website, we can input keywords according to the content we want at will to query the relevant content. How can we do this? These random data cannot be queried according to the fields of the database. How can they be queried? Why can all kinds of keywords be queried?
The answer is the implementation of full-text retrieval tool. Lunnce adopts word element matching and word segmentation. For example: Beijing Tiananmen Square ---- luncence participle: Beijing tiantiananmen square and so on. So we can retrieve it when we search.
One kind of word segmentation is ikanalyzer Chinese word segmentation, which has fine-grained segmentation and intelligent segmentation, that is, according to some intelligent algorithm.
This is the biggest advantage of using Solr: the implementation of retrieval function.
Use steps;
(1) Solr server is built. Because Solr is developed with java5, JDK and Tomcat are required for deployment
(2) After the construction is completed, we need to introduce the fields to be displayed into the Solr library. Configure the combination of spring and Solr, and start Solr when the project starts
(3) Import the query contents in the database into the Solr index library, which is implemented by the client of solrj. For specific use, please refer to the API
(4) Establish a search service for the client to call. Call Solr to query the content. There is the implementation of paging function in the middle. The highlighted implementation of Solr.
(5) The client receives the request parameters of the page and calls the search service to search.
Business field judgment criteria:
1. Whether to search on this field when searching. For example: product name, selling point and description
(these are equivalent to giving the label to Solr. After importing the commodity data, Solr will segment the specific contents of the corresponding commodities in these fields, and then we can search the relevant contents.)
2. Whether this field is required for subsequent business. For example: product ID.
Required fields:
1. Commodity ID
2. Commodity title
3. Selling point
4. Price
5. Product picture
6. Commodity classification name
7. Product description
Business fields in Solr:
1. ID -- commodity ID
Other corresponding fields create fields of Solr.
Restart Tomcat
Solr is a top open source project under Apache. It is developed in Java. It is a full-text search server based on Lucene. Solr provides a richer query language than Lucene, realizes configurable and extensible, and optimizes the index and search performance.
Solr is a full-text retrieval server, which only needs to be configured to implement the full-text retrieval service. Effectively reduce the pressure on the database caused by frequent access to the database.
Step 1: deploy Solr under Linux system.
Step 2: solrj is the client of Solr, and its use depends on the jar package of solrj.
Step 3: add the contents of the database to the index library of Solr, so that the query is in the index library instead of the database.
Controller layer:
mapper. xml
Step 4: write the logic of query from the index library:
Step 5: after the content of the index library is established, start to write an external service interface, that is, search for specific goods through conditions, such as mobile phones, and the total mobile phone list information, page number, total pages and total search results will be displayed
Requested URL:
/search/query? Q = {query criteria} & page = {page} & rows = {rows}
Returned result: taotaoresult packing item list.
Create a POJO corresponding to an SQL statement and create a POJO separately
List of contents to display:
Controller layer:
The premise is to write how to read data from the index library:
The following is the interface layer preparation of the service:
controller:
The client realizes the search function by inputting goods:
Controller layer:
@Controller
Service layer:
The above article using Solr to realize the commodity search function (example explanation) is all the content shared by Xiaobian. I hope it can give you a reference and support programming tips.