Use LAYUI+SPRINGBOOT+SOLR imitate Baidu search engine

Source: Internet
Author: User
Tags key string solr
Preface

Full-text search in SQL Fuzzy query, the biggest difference lies in
① the former can be a key string to query the first flexible participle, and then to match ,
② the latter will only be directly rigid matching .
③ Many sites have site search, each backstage should be, so did a demo share

There are a lot of advantages, I believe we have learned the summary of the separation of the front and back

① Front End frame: jQuery, Layui

② background frame: Springboot, SOLR

Imitate Baidu search , though small, spite

Many of the basics of this article are from the author's previous articles
①SOLR Foundation: [Additions and deletions] springboot integrate SOLR solrclient implement CRUD, paging interface, highlighting
② cross-Domain Problem resolution: setting allow Cross-domain requests in springboot
③layui Foundation: Use new, hot, lightweight, simple, powerful, graceful layui frame to build the front page effect gif graphic READ:

① Search Java Crawler Technology
② Search Springboot Consolidated SOLR
③ Click on a link to search results

thinking:

If you do not use the SOLR Full-text search, the search term Java learning, because of the first letter in uppercase, followed by Chinese, can not match the Java Crawler. Front-end code

<! DOCTYPE html>  
 Background   
Package Com.cun.controller;
Import java.io.IOException;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import Java.util.HashMap;
Import java.util.List;

Import Java.util.Map;
Import org.apache.solr.client.solrj.SolrClient;
Import Org.apache.solr.client.solrj.SolrQuery;
Import org.apache.solr.client.solrj.SolrServerException;
Import Org.apache.solr.client.solrj.response.QueryResponse;
Import org.apache.solr.common.SolrDocumentList;
Import org.apache.solr.common.SolrInputDocument;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.web.bind.annotation.DeleteMapping;
Import org.springframework.web.bind.annotation.GetMapping;
Import org.springframework.web.bind.annotation.PathVariable;
Import org.springframework.web.bind.annotation.PostMapping;
Import org.springframework.web.bind.annotation.RequestMapping;

Import Org.springframework.web.bind.annotation.RestController;

Import Springfox.documentation.swagger2.annotations.EnableSwagger2; /**
 * Optimization: Extract Id, text for a JavaBean * @author linhongcun * */@RestController @RequestMapping ("/test") @EnableSwagger2 Publi

    C class Solrcontroller {@Autowired private solrclient client;
    /** * 1, add * @param message * @return * @throws IOException * @throws solrserverexception * *
        @PostMapping ("/insert") Public String Insert (string title, String href) throws IOException, Solrserverexception {
        SimpleDateFormat SDF = new SimpleDateFormat ("Yyyymmddhhmmss");
        String datestring = Sdf.format (New Date ());
            try {solrinputdocument doc = new solrinputdocument ();
            Doc.setfield ("id", datestring);
            Doc.setfield ("title", title);
            Doc.setfield ("href", href); * * If the spring.data.solr.host inside the core, then there is no need to pass Collection1 This parameter is the same as the * CLIENT.COMMIT (
             );
            * * Client.add ("Itaem", Doc);
     Client.commit ("Itaem");       return datestring;
        catch (Exception e) {e.printstacktrace ();
    return "error"; /** * 4, delete all * @return/@DeleteMapping ("DeleteAll") public String DeleteAll () {T
            ry {client.deletebyquery ("Itaem", "*:*");
            Client.commit ("Itaem");
        Return "Success";
        catch (Exception e) {e.printstacktrace ();
    return "error"; /** * 7, check + +: keywords, highlighting, paging ✔* @return * @return * @throws solrserverexception * @throws IO Exception */@GetMapping ("/select/{q}/{page}/{size}") Public map<string, object> Select (@PathVariable S
        Tring q, @PathVariable integer page, @PathVariable integer size) throws Solrserverexception, IOException {

        Solrquery params = new Solrquery ();

        Query conditions Params.set ("q", q); Sort, the actual should be based on the score, but it doesn't matter, all the records found are relevant and irrelevant to the exclusion of params.addsorT ("id", SOLRQUERY.ORDER.DESC);
        Paging Params.setstart (page);

        Params.setrows (size);

        Default Domain Params.set ("df", "title");

        Query only the specified domain Params.set ("fl", "href");
        Open the Highlight params.sethighlight (true);
        Set prefix params.sethighlightsimplepre ("<span style= ' color:red ' >");

        Set suffix params.sethighlightsimplepost ("</span>");
        The SOLR database is Itaem queryresponse queryresponse = client.query ("Itaem", params);
        Solrdocumentlist results = Queryresponse.getresults (); Number, paging with long total = Results.getnumfound ();/JS uses SIZE=MXA and data.length to know the length (but not reasonable)//get the highlighted result, Highlighted results and query results are divided into open map<string, map<string, list<string>>> highlight = Queryresponse.gethighlighti
        Ng ();
        map<string, object> map = new hashmap<string, object> ();
        Map.put ("Total", total);
        Map.put ("href", results); Map.put ("Highlight", highlight);

    return map; }

}

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.