Menu

[r489]: / trunk / php-java-bridge / examples / search / lucene_search.php  Maximize  Restore  History

Download this file

109 lines (98 with data), 3.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
<?php
require_once('rt/java_io_File.php');
require_once('rt/java_lang_System.php');
require_once('rt/java_util_LinkedList.php');
require_once('lucene/All.php');
try {
echo "indexing ... ";
/* Create an index */
$cwd=getcwd();
/* create the index files in the tmp dir */
$tmp = create_index_dir();
$analyzer = new org_apache_lucene_analysis_standard_StandardAnalyzer();
$writer = new org_apache_lucene_index_IndexWriter($tmp, $analyzer, true);
$file = new java_io_File($cwd);
$files = $file->listFiles();
if(is_null($files)) {
$user = java_lang_System()->getProperty("user.name");
echo("$cwd does not exist or is not readable.\n");
echo("The directory must be readable by the user $user and it must not\n");
echo("be protected by a SEL rule.\n");
exit(1);
}
foreach($files as $f) {
$doc = new org_apache_lucene_document_Document();
$doc->add(new org_apache_lucene_document_Field(
"name",
$f->getName(),
org_apache_lucene_document_Field__Store()->YES,
org_apache_lucene_document_Field__Index()->UN_TOKENIZED));
$writer->addDocument($doc);
}
$writer->optimize();
$writer->close();
echo "done\n";
echo "searching... ";
/* Search */
$searcher = new org_apache_lucene_search_IndexSearcher($tmp);
$phrase = new org_apache_lucene_search_MatchAllDocsQuery();
$hits = $searcher->search($phrase);
/* Print result */
$iter = $hits->iterator();
$n = $hits->length();
echo "done\n";
echo "Hits: $n\n";
/* Instead of retrieving the values one-by-one, we store them into a
* LinkedList on the server side and then retrieve the list in one
* query:
*/
$resultList = new java_util_LinkedList();
// create an XML document from the
// following PHP code, ...
java_lang_System::javaBeginDocument();
while($n--) {
$next = $iter->next();
$name = $next->get("name");
$resultList->add($name);
}
// ... execute the XML document on
// the server side, ...
java_lang_System::javaEndDocument();
// .. retrieve the result, ...
$result = java_lang_System::javaValues($resultList);
// ... print the result array
print_r($result);
delete_index_dir();
} catch (JavaException $e) {
echo "Exception occured: {$e->__toString()}<br>\n";
}
/** helper functions */
$tmp_file=null;
$tmp_dir=null;
/** create a temporary directory for the lucene index files. Make sure
* to create the tmpdir from Java so that the directory has
* javabridge_tmp_t Security Enhanced Linux permission. Note that PHP
* does not have access to tempfiles with java_bridge_tmp_t: PHP
* inherits the rights from the HTTPD, usually httpd_tmp_t.
*/
function create_index_dir() {
global $tmp_file, $tmp_dir;
$javaTmpdir = java_lang_System()->getProperty("java.io.tmpdir");
$tmpdir = java_values($javaTmpdir);
$tmp_file=tempnam($tmpdir, "idx");
$tmp_dir=new java_io_File("${tmp_file}.d");
$tmp_dir->mkdir();
return java_values($tmp_dir->toString());
}
/** delete the lucene index files */
function delete_index_dir() {
global $tmp_file, $tmp_dir;
$files = $tmp_dir->listFiles();
foreach($files as $f) {
$f->delete();
}
$tmp_dir->delete();
unlink($tmp_file);
$tmp_file=$tmp_dir=null;
}
?>