Mailing List
Home
Forum Home
Maven - Project building tool
Axis - Java SOAP implementation
Cocoon - MVC web framework based on XML/XSL
Lucene - Full-featured text search engine APIs
Log4J - A log library
Fop - Create PDF, PCL, PS, SVG, XML driven by XSL formatting objects.
POI - Java Excel, Word and other Microsoft Office files manipulating library
Oracle database error code ...
Subjects
log4j warning: No appenders could be found
java security AccessControlException: access denied (java io FilePermission clie
java lang InstantiationException: org apache tools ant Main
Apache Axis Tutorial
Struts <logic iterate >
log4j properties How to parse outpu to multiple files
configuring log4j with BEA Weblogic 8 1
How to use XSL FOP Java together
JSP precompile
Servlet File Download dialog problem (IE6,Adobe 6 0)
Proposal: Adding jar manifest classpath in jar and war plugins
Unsupported major minor version 48 0 problem while running the an
   telope task
java security AccessControlException: access denied (java io FilePermission
axis wsdl2java Ant Task usage
net sf hibernate MappingException: Error reading resource: test/User hbm xml
Building EAR ANT Script for websphere 5 0
CREATING WAR Files
jsp data into Excel
Classpath problem
Jboss 3 2 3+ vs Tomcat Axis Question
RE: How to include jars and add them into the MANIFEST MF/Class Path
attribute
Printing problem
InstantiationException
Couldn 't find trusted certificate
Please : How can one install ant 1 6 0 under Eclipse 2 1 ?
Excel: Too many different cell formats
Running junit tests fails
XDoclet, Struts and Maven: Where to start? SOLUTION
1 3 final: now giving me java io FileNotFoundException (Too many
open files)
AXIS: tomcat timeout ?
 
Search:  
Power your search with and, or, +, -, or "some phrase" operators.
PHP-Lucene Integration

PHP-Lucene Integration

2005-02-07       - By amigo@(protected)

 Back
Reply:     1     2     3     4     5     6     7     8     9     10     >>  

Howdy,

For starters, compile and install the java bridge (and if necessary
recompile PHP and Apache2) and make sure it works (there's a test php
file supplied).

Then, here's a simplified part of my code, just to give you an example
how it works. This is the part that does the searching, indexing is done
in a similar way.

PHP:

...some code here for HTML page setup etc...

$lucene_dir = $GLOBALS["lucene_dir"];
java_set_library_path("/path/to/your/custom/lucene-classes.jar");
$obj = new Java("searcher"); // searcher is the custom written class
that does actual searching and data output
$writer = new Java("java.io.StringWriter Source code of java.io.StringWriter");
$obj->setWriter($writer);
$obj->initSearch($lucene_dir);
$obj->getQuery($query); // $query is the user supplied query from the
HTML form, not visible here

// get the last exception
$e = java_last_exception_get();

if ($e) {
   // print error
   echo $e->toString();
} else {
   echo $writer->toString();
   $writer->flush();
   $writer->close();
}
java_last_exception_get();
// clear the exception
java_last_exception_clear();

-- ---- ---- ---- ---- ---- -----

JAVA (custom written class located in the
/path/to/your/custom/lucene-classes.jar):

import ...whatever is needed here for the class...

public class searcher {

  IndexReader reader     = null;
  IndexSearcher s        = null;          //the searcher used to
open/search the index
  Query q                = null;          //the Query created by the
QueryParser
  BooleanQuery query  = new BooleanQuery();
  Hits hits              = null;          //the search results
 
  public Writer out;

  public void setWriter(Writer out) {
               this.out=out;
  }

 public void initSearch(String indexName) throws Exception {
       try {
               File indexFile            = new File(indexName);
               Directory activeDir       =
FSDirectory.getDirectory(indexFile, false);
               if(reader.isLocked(activeDir)) {
                       //out.write("Lucene index is locked, waiting 5
sec.");
                       Thread.sleep(5000);
               }
               reader = IndexReader.open(indexName);
               s = new IndexSearcher(reader);
               //out.write("Index opened");
       } catch (Exception e) {
               throw new Exception(e.getMessage());
       }
  }

  public void getQuery(String queryString) throws Exception {

       int totalhits   = 0;
       Analyzer analyzer = new StandardAnalyzer();
     
       String[] queryFields =
{"field1","field2","field3","field4","field5};
       float[] boostFields = {10, 6, 2, 1, 1};

       try {
               for ( int i = 0; i < queryFields.length; i++)
               {
               q = QueryParser.parse(queryString, queryFields[i],
analyzer);
               if (boostFields[i] > 1)
                       q.setBoost(boostFields[i]);
               query.add(q, false, false);
               }
       } catch (ParseException e) {
               throw new Exception(e.getMessage());
       }

       try {
               hits = s.search(query);
       } catch (Exception e) {
               throw new Exception(e.getMessage());
       }
     
       totalhits = hits.length();

       if (totalhits == 0) {                             // if we find
no hits, tell the user
               out.write("<br>I'm sorry I couldn't find your query: " +
queryString);
       } else {

       for (int i = 0; i < totalhits; i++) {
           Document doc = hits.doc(i);
           String field1 = doc.get("field1");
           String field2 = doc.get("field2");
           String field3 = doc.get("field3");
           String field4 = doc.get("field4");
           String field5 = doc.get("field5");
           out.write("Field1: " + field1 + ", Field2: " + field2 + ",
Field3: " + field3 + ", Field4: " + field4 + ", Field5: " + field5 +
"<br>");
       }
 }
}



Sanyi said the following on 2/7/2005 3:54 AM:

>Hi!
>
>Can you please explain how did you implement the java and php part to let them
communicate through
>this bridge?
>The brige's project summary talks about "java "application-server" or a
dedicated java process"
>and I'm not into Java that much.
>Currenty I'm using a self-written command-line search program and it outputs
its results to the
>standard output.
>I guess your solution must be better ;)
>
>If the "communication parts" of your code aren't top secret, can you please
share them with me/us?
>
>Regards,
>Sanyi
>
>
>
>    
>__ ____ ____ ____ ____ ____ ______
>Do you Yahoo!?
>Read only the mail you want - Yahoo! Mail SpamGuard.
>http://promotions.yahoo.com/new_mail
>
>-- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ------
>To unsubscribe, e-mail: lucene-user-unsubscribe@(protected)
>For additional commands, e-mail: lucene-user-help@(protected)
>
>
>
>  
>

-- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ------
To unsubscribe, e-mail: lucene-user-unsubscribe@(protected)
For additional commands, e-mail: lucene-user-help@(protected)


Earn $52 per hosting referral at Lunarpages.