Login   Register  
PHP Classes
elePHPant
Icontem

File: example/IFile_Mysql_Ex10_SearchBoolean.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Giampaolo Losito  >  IFile  >  example/IFile_Mysql_Ex10_SearchBoolean.php  >  Download  
File: example/IFile_Mysql_Ex10_SearchBoolean.php
Role: Example script
Content type: text/plain
Description: Example script
Class: IFile
Index and search documents using Lucene or MySQL
Author: By
Last change:
Date: 2013-11-21 08:11
Size: 5,915 bytes
 

Contents

Class file image Download
<?php 
/**
 * IFile framework
 * 
 * @category   IndexingFile
 * @package    ifile.example
 * @author        Giampaolo Losito, Antonio Di Girolomo
 * @copyright  2011-2013 isApp.it (www.isapp.it)
 * @license    GNU LESSER GENERAL PUBLIC LICENSE Version 2.1, February 1999
 * @version    1.2.1
 * 
 */ 
 
/**
 * This script is a example how to search a grop of terms in boolean method with MySqli Interface
 * 
 * IMPORTANT:
 * For MySql is important define in the configuration (IFileConfig.xml)
 * Define Table name that IFile must used
 * The fields: name, path, filename as "Text":
 * 
   <table-name collation="utf8_general_ci">ifile_index_table</table-name>
   ...
   <zend-document>
        <fields>            
            <field name="name" type="Text" />
            <field name="path" type="Text" />
            <field name="filename" type="Text" />            
        </fields>        
    </zend-document>
 */ 

error_reporting(E_ALL);
/** require IFileFactory */
require_once '../IFileFactory.php';

// Create connection at MySql
// IFile works only with MySqli InterfaceĆ¹
// First to test, you must create "example_ifile_index" DB in the your MySql
$connection = @new mysqli('localhost''root''''example_ifile_index'3306);                    
if (
mysqli_connect_error()) {die(" - ".mysqli_connect_error()." - ");}

// try/catch
try {
    
// instance IFileFactory
    
$IFileFactory IFileFactory::getInstance();
    
// define mysqli interface
    
$ifile $IFileFactory->getIFileIndexing('mysqli'$connection);
    
    
// TEST Query Boolean
    // For Boolean Search IFile use IFileQueryRegistry
    
    // Case 1. Search term present in first group AND present or not present in second group
    // First group
    
$ifileQueryRegistry_01 = new IFileQueryRegistry();
    
$ifileQueryRegistry_01->setQuery('prova''body'IFileQuery::MATCH_REQUIRED);
    
$ifileQueryRegistry_01->setQuery('txt''body'IFileQuery::MATCH_REQUIRED);
    
// Second group
    
$ifileQueryRegistry_02 = new IFileQueryRegistry();
    
$ifileQueryRegistry_02->setQuery('doc''body'IFileQuery::MATCH_REQUIRED);
    
// Setting query
    
$ifileQueryRegistry =  new IFileQueryRegistry();
    
$ifileQueryRegistry->setQuery($ifileQueryRegistry_01nullIFileQuery::MATCH_REQUIRED);
    
$ifileQueryRegistry->setQuery($ifileQueryRegistry_02nullIFileQuery::MATCH_OPTIONAL);
    
// define order
    
$ifile->setSort('key'SORT_STRINGSORT_DESC);        
    
// call Boolean method 
    
$result $ifile->queryBoolean($ifileQueryRegistry);
    
// print result
    
printResult("Case 1: "$result);
    
    
// Case 2. Search term present or in first group OR in second group
    // First group
    
$ifileQueryRegistry_01 = new IFileQueryRegistry();
    
$ifileQueryRegistry_01->setQuery('prova''body'IFileQuery::MATCH_REQUIRED);
    
$ifileQueryRegistry_01->setQuery('txt''body'IFileQuery::MATCH_REQUIRED);
    
// Second gruppo
    
$ifileQueryRegistry_02 = new IFileQueryRegistry();
    
$ifileQueryRegistry_02->setQuery('doc''body'IFileQuery::MATCH_REQUIRED);
    
// Setting query
    
$ifileQueryRegistry =  new IFileQueryRegistry();
    
$ifileQueryRegistry->setQuery($ifileQueryRegistry_01nullIFileQuery::MATCH_OPTIONAL);
    
$ifileQueryRegistry->setQuery($ifileQueryRegistry_02nullIFileQuery::MATCH_OPTIONAL);
    
// define order
    
$ifile->setSort('key'SORT_STRINGSORT_DESC);        
    
// call Boolean method 
    
$result $ifile->queryBoolean($ifileQueryRegistry);
    
// print result
    
printResult("Case 2: "$result);
    
    
    
// Case 3. Search all term present in first group AND NOT present in second group
    // First group
    
$ifileQueryRegistry_01 = new IFileQueryRegistry();
    
$ifileQueryRegistry_01->setQuery('prova''body'IFileQuery::MATCH_REQUIRED);
    
$ifileQueryRegistry_01->setQuery('txt''body'IFileQuery::MATCH_REQUIRED);
    
// Second group
    
$ifileQueryRegistry_02 = new IFileQueryRegistry();
    
$ifileQueryRegistry_02->setQuery('doc''body'IFileQuery::MATCH_REQUIRED);
    
// Setting query
    
$ifileQueryRegistry =  new IFileQueryRegistry();
    
$ifileQueryRegistry->setQuery($ifileQueryRegistry_01nullIFileQuery::MATCH_REQUIRED);
    
$ifileQueryRegistry->setQuery($ifileQueryRegistry_02nullIFileQuery::MATCH_PROHIBITEN);
    
// define order
    
$ifile->setSort('key'SORT_STRINGSORT_DESC);        
    
// call Boolean method 
    
$result $ifile->queryBoolean($ifileQueryRegistry);
    
// print result
    
printResult("Case 3: "$result);
    
    
// Case 4. Search term (optional) present in first group AND NOT present in second group    
    // Second group
    
$ifileQueryRegistry_01 = new IFileQueryRegistry();
    
$ifileQueryRegistry_01->setQuery('prova''body'IFileQuery::MATCH_REQUIRED);
    
$ifileQueryRegistry_01->setQuery('txt''body'IFileQuery::MATCH_REQUIRED);
    
// Second group
    
$ifileQueryRegistry_02 = new IFileQueryRegistry();
    
$ifileQueryRegistry_02->setQuery('doc''body'IFileQuery::MATCH_REQUIRED);
    
// Setting query
    
$ifileQueryRegistry =  new IFileQueryRegistry();
    
$ifileQueryRegistry->setQuery($ifileQueryRegistry_01nullIFileQuery::MATCH_OPTIONAL);
    
$ifileQueryRegistry->setQuery($ifileQueryRegistry_02nullIFileQuery::MATCH_PROHIBITEN);
    
// define order
    
$ifile->setSort('key'SORT_STRINGSORT_DESC);        
    
// call Boolean method 
    
$result $ifile->queryBoolean($ifileQueryRegistry);
    
// print result
    
printResult("Case 4: "$result);
    
} catch (
Exception $e) {
    echo 
"Error: ".$e->getMessage();
}

/**
 * Function of utility. Used only for this example.
 * Print result of search
 * 
 * @param strint $type
 * @param array $result_T
 * @return 
 */
function printResult($type$result) {
 
    echo 
"Type of search: ".$type
    if(!empty(
$result) && is_array($result)) {
        echo 
"<br>Result Search:<br>";
        foreach (
$result as $hit) {
            
$doc $hit->getDocument();
            echo 
"File: ".$doc->name." - Chiave: ".$doc->key." - Score: ".$hit->score."<br>";
        }
    }  else {
        echo 
"<br>Not result returned<br>";
    }
    
    echo 
"End print for: ($type)<br><br>";
}
?>