PHP implements the retrieval application by calling the Lucene library

Source: Internet
Author: User
Tags createindex install php stmt

PHP implements the retrieval application by invoking the Lucene library. Note Add the path path for the Php,tomcat,java,apache installed below.

The retrieved data is stored in the MySQL database, the retrieved data is crawled from the network using Python, is stored in JSON format, and can be inserted directly into the database by Java.

The jar package that is used.
1, Gson-2.2.1.jar (users directly to the JSON format into Java objects)
2, Lucene-core-3.0.2.jar (Lucene library)
3, Mysql-connector-java-5.1.37-bin.jar (Java connection MySQL driver library)
4, Luceneexample.jar (the use of Lucene's own sample library)

First step:
Install Java and configure environment variables. Copy the above jar package to the Java Virtual Machine run environment Jre\lib\ext directory so that the libraries are loaded automatically when the virtual runs.

Step Two:
Install Tomcat, because PHP calls Java requires the use of Php-java-bridge, The download is Javabridge.war, so you need to use Tomcat to download the Javabridge.war file resolution, the specific method is: Put Javabridge.war in the Webapps\ directory of Tomcat, start Tomcat, At this point Tomcat will automatically parse the Javabridge.war in Webapps\, production Javabridge folder, copy this folder to the second step of the Apache run directory.

Step Three:
Install PHP, install Apache, and copy the Javabridge folder from the first step to the Htdocs\ directory. (Javabridge folder contains something similar to a header file.)

Step Fourth:
The environment has been basically set up, and the following is the beginning of concrete implementation.
1, login to the MySQL terminal, the use of SQL instructions to establish Tiku database, set up the Math database table.
Create DATABASE Tiku;
Use Tiku;
CREATE TABLE math (index_num int (one) primary key not NULL auto_increment,question text is not null,answer text);
You can desc math when you finish building the table.
2, the terminal into the loading data code directory D:\SOFT\YANGYANG\LUC, the data file is Out.data, in JSON format.
Compile Loaddata.java and execute,
Javac loaddata.java
Java loaddata
When you view the math database in the terminal, you can see that the data is stored in the database table math.
SELECT * from math limit 2;
3, compiling a sample library of the production itself that uses Lucene
also executes the following command in the above directory
Javac Luceneexample.java compile generation class file
JAR-CVF Luceneexample.ja R Luceneexample.class the package class file
then copies the Luceneexample.jar file to the Jre\lib\ext directory mentioned above.
4, write the service file
into the Apache running directory htdocs\ write test.php file to invoke the above Luceneexample.jar Library implementation search. See appendix

for specific code

Fifth Step:
Open the service environment and view the results of the operation:
1, the first guarantee to open the MySQL service, you can run in cmd command net start MySQL
2, enter the third step of the Javabridge\web-inf\lib directory, double-click the run Javabridge.jar program (if not run, manually enter the directory to open Java-jar Javabridge.jar, select 8080 Port, OK)
3, into the Apache bin directory double-click ApacheMonitor.exe to open the Apache service.
4, at this time in the browser input http:\localhost\test.php can see the results of the query (if garbled, please modify the encoding method for UTF-8).

Loaddata.java Source

Import Java.io.BufferedReader;
Import Java.io.InputStreamReader;
Import Java.io.FileInputStream;
Import Com.google.gson.reflect.TypeToken;

Import Java.lang.reflect.Type;
Import Com.google.gson.Gson;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import java.util.ArrayList;


Import java.util.List;  public class LoadData {public static void Loadjson (String fileName) {try{BufferedReader br = new
            BufferedReader (New InputStreamReader (FileName), "Utf-8");
            String data = Br.readline ();
            Gson Gson = new Gson ();
            /* list<timu> TT = new arraylist<timu> ();
            Tt.add (New Timu ("FFF", "FSDFSD"));
            Tt.add (New Timu ("444", "3333232"));
            System.out.println (Gson.tojson (TT));
            */Type type = new Typetoken<list<timu>> () {}.gettype (); list<timu> PS = Gson.fromjson (data, type);
            System.out.println (Ps.tostring ());
            JDBC section class.forname ("Com.mysql.jdbc.Driver"). newinstance ();
            Connection conn = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/tiku", "root", "BUPTMM");  
            String sql= "INSERT into math (question,answer) VALUES (?,?)";

            PreparedStatement prestmt =conn.preparestatement (SQL);  
                for (Timu ti:ps) {prestmt.setstring (1,TI.GETQ ());
                Prestmt.setstring (2,ti.geta ());
            Prestmt.executeupdate ();
        } catch (Exception e) {e.printstacktrace ();   
    } public static void Main (string[] args) {Loaddata.loadjson ("Out.data");
    } public class timu{private String Q;

    Private String A; Public Timu () {} public Timu (string q, String a) {this.
        Q = q; This.
    A = A;
    Public String Getq () {return Q; } public void Setq(String Q) {this.
    Q = q;
    Public String Geta () {return A; public void SetA (String A) {this.
    A = A;
 }
}

Luceneexample.java Source code

Import Java.io.File;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;


Import java.sql.Statement;
Import Org.apache.lucene.analysis.Analyzer;
Import Org.apache.lucene.analysis.standard.StandardAnalyzer;
Import org.apache.lucene.document.Document;
Import Org.apache.lucene.document.Field;
Import Org.apache.lucene.index.IndexReader;
Import Org.apache.lucene.index.IndexWriter;
Import Org.apache.lucene.index.IndexWriter.MaxFieldLength;
Import Org.apache.lucene.index.Term;
Import Org.apache.lucene.queryParser.QueryParser;
Import Org.apache.lucene.search.IndexSearcher;
Import Org.apache.lucene.search.Query;
Import Org.apache.lucene.search.FuzzyQuery;
Import Org.apache.lucene.search.TopDocs;
Import Org.apache.lucene.store.Directory;
Import Org.apache.lucene.store.FSDirectory;
Import Org.apache.lucene.store.SimpleFSDirectory;


Import org.apache.lucene.util.Version; public class Luceneexample {public static final File index_directory = new File ("./");

    public void CreateIndex () {System.out.println ("--indexing--");
            try {//jdbc section class.forname ("Com.mysql.jdbc.Driver"). newinstance (); Assuming database bookstore exists Connection conn = drivermanager.getconnection ("jdbc:mysql://localhost:330
            6/tiku "," root "," BUPTMM ");
            Statement stmt = Conn.createstatement ();
            String sql = "Select question from Math";

            ResultSet rs = stmt.executequery (SQL);
            Lucene section Directory directory = new simplefsdirectory (index_directory);
            Analyzer Analyzer = new StandardAnalyzer (version.lucene_30);

            IndexWriter iwriter = new IndexWriter (directory, analyzer, true,maxfieldlength.unlimited);
            Looping through resultset and adding to index file int count = 0;
                while (Rs.next ()) {Document doc = new Document (); System.out.println ("Question= "+ rs.getstring (" question "));
                Doc.add (New Field ("Question", rs.getstring ("question"), Field.Store.YES, Field.Index.ANALYZED));
                Doc.add (New Field ("Book_title", Rs.getstring ("Book_title"), Field.Store.YES, Field.Index.ANALYZED));

                Doc.add (New Field ("Book_details", Rs.getstring ("Book_details"), Field.Store.YES, Field.Index.ANALYZED));
                Adding doc to Iwriter iwriter.adddocument (DOC);
            count++;

            } System.out.println (count+ "record indexed");
            Closing iwriter iwriter.optimize ();
            Iwriter.commit ();

            Iwriter.close ();
            Closing JDBC Connection rs.close ();
            Stmt.close ();

        Conn.close ();
        catch (Exception e) {e.printstacktrace ();
        } public string search (string keyword) {System.out.println ("--seaching--"); StrinG result = ""; 
            try {//searching Indexreader reader = Indexreader.open (Fsdirectory.open (index_directory), true);
            Indexsearcher searcher = new Indexsearcher (reader);
            Analyzer Analyzer = new StandardAnalyzer (version.lucene_30);
            Multifieldqueryparser is used to search multiple fields//string[] Filestosearch = {"Question"};

            Queryparser MQP = new Queryparser (version.lucene_30, "question", analyzer); Query query = mqp.parse (keyword);//search the given keyword//query query = new Fuzzyquery ("Term"
            , keyword), 0.01f);

            SYSTEM.OUT.PRINTLN ("Query >>" + keyword); Topdocs hits = searcher.search (query, 5);

            Run the query System.out.println ("Results found >>" + hits.totalhits);  for (int i = 0; i < hits.totalhits i++) {Document doc = Searcher.doc (hits.scoredocs[i].doc);//get the Next  Document SYSTEM.OUT.PRINTLN (Doc.get ("question"));
                result = Doc.get ("question");
            Break
        } catch (Exception e) {e.printstacktrace ();
    return result;
        public string GetResult (string que) {//string que = search (question);
        if (Que.equals ("")) return "";
            try{//jdbc Section class.forname ("Com.mysql.jdbc.Driver"). newinstance ();
            Connection conn = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/tiku", "root", "BUPTMM");
            Statement stmt = Conn.createstatement ();
            String sql = "Select answer from math where question= '" + Que + "'";
            SYSTEM.OUT.PRINTLN ("sql =" + sql);
            ResultSet rs = stmt.executequery (SQL);
            while (Rs.next ()) {return rs.getstring ("answer");
  } catch (Exception e) {e.printstacktrace ();      Return "";

        public static void Main (string[] args) {luceneexample obj = new Luceneexample ();

        Creating index Obj.createindex ();


        searching keyword//system.out.println ("A1:" + Obj.search (""));
        Using wild card serach String question = Obj.search ("5");
        System.out.println ("A1:" + question);


        System.out.println ("A1:" + Obj.getresult (question));
        Using logical operator//obj.search ("Data1 OR data2");

    Obj.search ("Data1 and Data2");
 }
}

test.php Source

<?php
    require_once ("Javabridge/java/java.inc");

    #print "fffff<br/>";
    # $param = $_post[' question '];    
    $TF = new Java (' luceneexample ');
    $TF->createindex ();
    $q = Java_values ($TF->search ("Aunt Wang"));
    # $s = $TF->test ();
    Print "topic:". $q;
    print "<br/>";
    $a = java_values ($TF->getresult ($q));
    Print "Answer:". $a;
? >

JSON data

[{"Q": "Aunt Wang bought 3 kilograms Longan and 8 kilograms watermelon spent 46 yuan." The price of the known 1 kilograms watermelons is exactly 1/5 of the 1 kilograms Longan. The unit price of Longan and watermelon is how many yuan respectively. (5 points) "," A ":" Set 1 kilograms watermelon price is x, then the price of Longan is 5x,3*5x+8x=46,x=2, so the unit price of Longan is 10 yuan, the price of watermelon is 2 Yuan "},{" Q ":" There are 13 table tennis, 12 of the same quality, another a lighter, if the balance is said , at least the guarantee can find the ping-pong ball. "," a ":" Http://www.tiku.cn/q/1010405.html "}, {" Q ":" There are 9 bottles of calcium, defective bottle 4 less. " Using a balance to weigh at least once will ensure that defective goods are found. "," A ":" Http://www.tiku.cn/q/1010406.html "}]

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.