SOLR Advanced Eight: JQuery UI autocomplete combined with SOLR search

Source: Internet
Author: User
Tags solr jquery ui autocomplete

The approximate process:

Page Capture to Text -Uploadservlet(Controller) layer,servletlayer Invoke Background -background based onservletthe parameters of the layer are transmitted dynamically from theSOLRget data in- SOLRdata returned toservletlayer, parsing -displayed on the page.

Create a new corein SOLR , create a new tablein the MySQL database, import data from this table to SOLR of the Core , the specific steps can be online check or see my previous tutorial.

SQL Statements:


SET foreign_key_checks=0;--------------------------------Table structure for lifeixroles-------------------------- ----DROP TABLE IF EXISTS ' lifeixroles '; CREATE TABLE ' lifeixroles ' (' id ' int (one) not null auto_increment, ' accountId ' int (one) DEFAULT NULL, ' level ' int (one) DE FAULT null, ' name ' varchar (255) default NULL, ' accountname ' varchar (255) default NULL, ' namepinyin ' varchar (255) Defaul T null, ' l99no ' int (one) default null, ' Photopath ' varchar (255) default NULL, PRIMARY KEY (' id ')) engine=innodb Auto_inc Rement=6 DEFAULT Charset=utf8;--------------------------------Records of lifeixroles------------------------------ INSERT into ' lifeixroles ' VALUES (' 1 ', ' 4 ', ' 38 ', ' precipitous peak ' feet ', ' Peter ', ' Peter ', ' 150104 ', ' 02/mjawotazmjgymtm1ntdfmjiwlji0os4 3os4znv8zmzm2mdi=.jpg '); INSERT into ' lifeixroles ' VALUES (' 2 ', ' 8 ', ' 1 ', ' small shrimp ', ' cubic coffee ', ' Lifangkafei ', ' 150108 ', ' 20/mjaw Otaymtywmzqxmjbfmjiwlji0os43os4znv82mdywntc=. JPG '); INSERT into ' lifeixroles ' VALUES (' 3 ', ' 11 ', ' 46 ', ' The name of the lake ', ' Nick ',' Nick ', ' 150121 ', ' 10/mjaxnda2mtmxmtq2mdvfmtkylje2oc4xotkuntdfnta0ndcy.jpg '); INSERT into ' lifeixroles ' VALUES (' 4 ', ' 10 ', ' 1 ', ' Shrimp ', ' oz ', ' oz ', ' 150130 ', ' 21/mjaxmjeymjexnta5mzlfmtgzljm3ljm0lji5xzizmja1nw==.png '); INSERT into ' Lifeixroles ' VALUES (' 5 ', ' 49 ', ' 46 ', ' John Doe ', ' cubic radius ', ' li ', ' 150163 ', ' 10/mjawotaymdcxmjqwmzbfmjiwlji0os43os4znv83mjq3odi =.. JPG ');

Partial configuration reference for Schema.xml:


Word breaker:


Specify the database configuration file:


Since the new core is the core of the copy solr-4.10.2\example\multicore , So the configuration file is very simple. See if the word breaker is useful in your core, and the results are useful:


But to search inside the specified field to search for some words, can not search out, only search out the full name:


Find fault for a long time can not find out why, including the multivalued to true, although not know the specific reason, but changed this, but no use, showing the results with square brackets only.


Later, the data into the collection1 is Normal, may be some of the key things are not configured, because the original core configuration is the simplest basic configuration, which needs to be carefully studied in the later study. Search out the data for the keywords:

This is good, start coding!

Background or servlet and json jar packages generate JSON data back to the foreground page, Specific steps can be seen in my previous summary tutorial.

Tool class for converting objects to JSON statements:Src\com\lifeix\util\fastjsonutil.java

Package Com.lifeix.util;import com.alibaba.fastjson.json;/** * Created by Lhx on 14-12-10 pm 4:15 * * @project Jspproject *  @package Com.lifeix.util * @blog http://blog.csdn.net/u011439289 * @email [email protected] * @Description */public class Fastjsonutil {    /**     * Object entity converted to JSON     * @param object     * @return */public    static String Object2json (Object object) {        json JSON = (JSON) Json.tojson (object);        return json.tojsonstring ();    }}

Processing class for retrieving data from the SOLR server in the background:\src\com\lifeix\util\solrgetfttopic2.java

Package Com.lifeix.util;import Org.apache.solr.client.solrj.solrserver;import Org.apache.solr.client.solrj.solrserverexception;import Org.apache.solr.client.solrj.impl.httpsolrserver;import Org.apache.solr.client.solrj.response.queryresponse;import Org.apache.solr.common.solrdocument;import Org.apache.solr.common.solrdocumentlist;import Org.apache.solr.common.params.modifiablesolrparams;import Java.util.arraylist;import java.util.hashmap;import java.util.list;import java.util.map;/** * Created by LHX on 14-12-9 a.m. * * @project Jspproject * @package com.lifeix.util * @blog http://blog.csdn.net/u011439289 * @email [EMAIL&NBSP;PR Otected] * @Description */public class SolrGetFtTopic2 {private static final String Solr_url = "http://localhost:8080/    Solr/collection1 ";        public string Queryall (string htmlword) {modifiablesolrparams params = new Modifiablesolrparams ();        Params.set ("Q", "AccountName:" +htmlword);        Params.set ("Start", 0);    Params.set ("Rows", 10);    Params.set ("Sort", "score desc");        Params.set ("F1", "*,score");        Solrserver Server = new Httpsolrserver (Solr_url);        list<map<string,object>> Listword = new arraylist<map<string, object>> ();        Map<string,object> map = null;            try {queryresponse response = Server.query (params);            Solrdocumentlist list = Response.getresults ();                for (int i = 0; i < list.size (); i++) {map = new hashmap<string, object> ();                Solrdocument document = List.get (i);                Map.put ("label", Document.getfieldvalue ("l99no"));                Map.put ("Value", Document.getfieldvalue ("AccountName"));            Listword.add (map);        } return Fastjsonutil.object2json (Listword);        } catch (Solrserverexception e) {e.printstacktrace ();    } return null; }}

The Servlet layer gets the input value from the foreground page and then gives it back to the background processing

\src\com\lifeix\servlet\jsonsolrservlet.java

Package Com.lifeix.servlet;import Com.lifeix.util.solrgetfttopic2;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import java.io.ioexception;import java.io.printwriter;/** * Created by LHX on 14-12-10 pm 5:29 * * @project jspproject * @package ${package_name} * @blog http://blog.csdn.net/u011439289 * @email [email  protected] * @Description */public class Jsonsolrservlet extends HttpServlet {protected void DoPost (HTTPSERVLETR Equest request, HttpServletResponse response) throws Servletexception, IOException {//Get the contents of a text box on a Web page input String        Htmlword = Request.getparameter ("term"). Trim ();        String jsonstr = "";        SolrGetFtTopic2 solrgetfttopic = new SolrGetFtTopic2 (); if (! "". Equals (Htmlword) && Htmlword! = null) {//Incoming parameters, search by parameter Jsonstr = Solrgetfttopic.queryall (HT        Mlword); } PrintWriter pw = null; try {response.setcontenttype ("Application/json;            Charset=utf-8 ");            Response.setcharacterencoding ("UTF-8");            Response.setheader ("Cache-control", "No-cache");            PW = Response.getwriter ();            Pw.print (JSONSTR);        Pw.flush ();        }catch (Exception e) {e.printstacktrace ();        } finally {if (PW = null) pw.close (); }} protected void Doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, ioexcept    Ion {this.dopost (request,response); }}

Now is the jQuery UI Autocomplete code for the foreground page , which can refer to the

JQuery UI Autocomplete 's remote cache processing example:

http://jqueryui.com/autocomplete/#remote-with-cache

jquery UI Autocomplete is the auto-complete component of the jquery UI

My code is as follows:

<! DOCTYPE html>


The final implementation results are as follows:

——————————————————————————

——————————————————————————————————————————


Postscript:

Because the value of the previous change multivalued is true , the value returned by the search is square brackets, resulting in a JSON statement that is not formal json, so the front page parsing can not, through the breakpoint debugging to find this problem:

Another drawback of this example is that Chinese input words can not trigger the event immediately, to hit the keyboard or hit a space bar,jQuery will be the text sent to the background processing, and English and numbers do not have this problem, this also to deal with!



Download information:Http://pan.baidu.com/s/1jG1gAHK


SOLR Advanced Eight: JQuery UI autocomplete combined with SOLR search

Related Article

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.