Using SOLR in Web applications (i)

Source: Internet
Author: User
Tags object model pack solr

The following is the use of SOLR requirements in a real web application

Use SOLR to achieve the e-commerce website of the product information search function, according to keywords, classification, price search product information, can be sorted by price

Business Process

Functional Analysis 1. Dao

Function: Implement a function called SOLR Service, request service layer pass over a Solrquery object, execute query directly, get return result. Returns the list of items and the total number of query results.
Parameter: Solrquery Object
return Result: Resultmodel

Method definition: Resultmodel queryproduct (solrquery query) throws Exception;

1. Commodity entity class

public class ProductModel {
    //Product Code
    private String pid;
    Commodity name
    private String name;
    Commodity classification name
    private String catalog_name;
    Price
    private float;
    Product Description
    Private String description;
    Image name
    private String picture;
}

2. Return value object model

public class Resultmodel {
    //commodity list
    private list<productmodel> productlist;
    Total Merchandise
    private Long recordCount;
    Total pages
    private int pagecount;
    Current page
    private int curpage;
}
2.Service

Function
Receive the parameters passed by the performance layer, assemble the query object according to query query parameters. Call DAO to query the product list. Total pages are calculated based on the total number of items in the returned results.
Parameters
1. Query condition: String queryString
2, filter conditions, Commodity category name: String catalog_name
3, the price range of the filter conditions, price range format: 0-10,11-20,21-30,30-*:string prices
4, sorting conditions, according to the price order, you can only pass the Sort method 0: Ascending 1: Descending: String sort
5, paging conditions, only need to get the page number, each page shows the quantity of the product definition constants: Integer page
return value
Resultmodel
interface method definition
Resultmodel queryproduct (String queryString, String catalog_name, String price, string sort, Integer page) throws Exceptio N 3.Controller

Function: Receives the parameter which the page passes over, invokes the Service query product list. Pass the list of items to the page. You also need to echo the query criteria.
Parameters:
1. Query condition: String queryString
2, filter conditions, Commodity category name: String catalog_name
3, the price range of the filter conditions, price range format: 0-10,11-20,21-30,30-*:string prices
4 sorting criteria, sorted by price, can only be passed sort by 0: Ascending 1: Descending: String sort
5, paging conditions, only need to get the page number, each page shows the quantity of the product definition constants: Integer page
6, Model, the equivalent of request.
Return value: Returns a String that is the name of a JSP.
Method definition:
String Queryproduct (String queryString, String catalog_name, String price, string sort, Integer page, model model); Environment Construction

Use the index library for the index library in this article
Create a MAVEN project
Jar Package Look Pom file

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < Modelversion>4.0.0</modelversion> <groupId>com.bjsxt</groupId> <artifactid>8_6_ Jdwithsolr</artifactid> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging&
    Gt <dependencies> <!--Spring Core Pack---<dependency> <groupid>org.springfram Ework</groupid> <artifactId>spring-core</artifactId> <version>4.3.7.releas
            E</version> </dependency> <!--SPRING-WEBMVC Package---<dependency>
            <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.7.RELEASE</version>
        </dependency> <!--SPRING-JDBC Package----<dependency> <groupid>org. Springframework</groupid> <artifactId>spring-jdbc</artifactId> <version>4.
            3.7.release</version> </dependency> <!--spring-aspects Package---<dependency> <groupId>org.springframework</groupId> <artifactid>spring-aspects</artifactid&
            Gt
        <version>4.3.7.RELEASE</version> </dependency> <!--servlet Package Note range: Provided-- <dependency> <groupId>javax.servlet</groupId> <artifactid>javax.servlet
        -api</artifactid> <version>3.1.0</version> <scope>provided</scope> </dependency> <!--JSP attention range provided--<dependency> <groupid>ja Vax.servLet.jsp</groupid> <artifactId>javax.servlet.jsp-api</artifactId> <version> 2.3.1</version> <scope>provided</scope> </dependency> <!--jstl jar Package--<dependency> <groupId>jstl</groupId> <artifactId>jstl<
        /artifactid> <version>1.2</version> </dependency> <!--Jackson Pack-- <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifacti
        D>jackson-databind</artifactid> <version>2.8.8</version> </dependency> <dependency> <groupId>org.apache.solr</groupId> &LT;ARTIFACTID&GT;SOLR-SOLRJ </artifactId> <version>4.10.3</version> </dependency> </dependencies&gt

; </project>

Dao Code

public class Productdaoimpl implements Productdao {@Autowired private solrserver solrserver; @Override public Resultmodel queryproduct (solrquery query) throws Exception {//execute query queryresponse que
        Ryresponse = solrserver.query (query);
        Fetch list Solrdocumentlist solrdocumentlist = Queryresponse.getresults ();
        Product List list<productmodel> productlist = new arraylist<> (); Traverse Commodity list for (solrdocument solrdocument:solrdocumentlist) {ProductModel ProductModel = new PRODUCTM
            Odel ();
            Productmodel.setpid (String) solrdocument.get ("id")); Take highlight map<string, map<string, list<string>>> highlighting = queryresponse.gethighlighting (
            );
            list<string> list = Highlighting.get (Solrdocument.get ("id")). Get ("Product_Name");
            String productName = ""; if (null! = List && list.size () > 0) {pRoductname = list.get (0);
            } else {productName = (String) solrdocument.get ("Product_Name");
            } productmodel.setname (ProductName);
            Productmodel.setcatalog_name (String) solrdocument.get ("Product_catalog_name");
            Productmodel.setprice ((float) solrdocument.get ("Product_price"));
            Productmodel.setpicture (String) solrdocument.get ("Product_picture");
        Add to Product List Productlist.add (ProductModel);
        }//Return value object Resultmodel Resultmodel = new Resultmodel ();
        Resultmodel.setproductlist (productlist);
        Resultmodel.setrecordcount (Solrdocumentlist.getnumfound ());
    return Resultmodel;
 }

Service Code

@Service public class Productserviceimpl implements Productservice {@Autowired private Productdao Productdao; @Override Public Resultmodel queryproduct (string queryString, String catalog_name, string price, Strin
        G Sort, Integer page) throws Exception {//assemble query condition Solrquery = new Solrquery (); Query condition if (null! = queryString &&! ").
        Equals (queryString)) {query.setquery (queryString);
        } else {query.setquery ("*:*"); }//Commodity category Filter if (null! = Catalog_name &&! ").
        Equals (Catalog_name)) {query.addfilterquery ("Product_catalog_name:" + catalog_name); }//Price filter if (null!! = &&! ").
            Equals (Price)) {string[] strings = Price.split ("-");
        Query.addfilterquery ("product_price:[" +strings[0]+ "to" +strings[1]+ "]"); }//Sort criteria if ("1". Equals (sort)) {Query.setsort ("PROduct_price ", Order.desc);
        } else {query.setsort ("Product_price", ORDER.ASC);
        }//pagination processing if (null = = page) page = 1;
        int start = (page-1) * global.page_size;
        Query.setstart (start);
        Query.setrows (global.page_size);
        Set the default search domain Query.set ("df", "product_keywords");
        Highlight set Query.sethighlight (true);
        Query.addhighlightfield ("Product_Name");
        Query.sethighlightsimplepre ("<span style=\" color:red\ ">");

        Query.sethighlightsimplepost ("</span>");
        Execute query Resultmodel resultmodel = productdao.queryproduct (query);
        Calculates the total number of pages Long RecordCount = Resultmodel.getrecordcount ();
        int pages = (int) (recordcount/global.page_size);
        if (recordCount% global.page_size > 0) {pages++;
        } resultmodel.setpagecount (pages);

        Resultmodel.setcurpage (page);
    return Resultmodel;
 }

}

Controller Code

 @Controller public class Productcontroller {@Autowired private productservice service;
            @RequestMapping ("/list") Public String queryproduct (string queryString, String catalog_name, String price,
        String Sort, Integer page, model model) {//execute query Resultmodel Resultmodel = null;
        try {Resultmodel = service.queryproduct (queryString, catalog_name, Price, sort, page);
        } catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();
        }//Pass query Results to page model.addattribute ("result", Resultmodel);
        Parameters echo Model.addattribute ("queryString", queryString);
        Model.addattribute ("Catalog_name", catalog_name);
        Model.addattribute ("Price", price);
        Model.addattribute ("Sort", sort);
        Model.addattribute ("page", page);
    Returns the name of the JSP return "Product_list"; }
}

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.