• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • 位置: php 中文手册 -> PECL扩展库

    mnoGoSearch(搜索引擎)

    mnoGoSearch是一个功能完整的Web搜索引擎,你可以用它来在HTTP,HTTPS,FTP,NTTP服务器,本地文件和数据库文本字段之上构建搜索引擎。它内置支持XML,HTML,eml和文本文件。外部转换器,可用于支持其它类型的文档。支持自动 70多种语言/字符集猜测。还支持群集,从而能够实现通过多台计算机分发数据,能够索引高达几百亿文件巨大集合。Mnogosearch是由dpsearch改良而来的,与php整合比较好用。

    此扩展已被移至 PECL 资源库且不再与 PHP 捆绑。下载 http://www.mnogosearch.org/

    PHP包含内置的MySQL访问库,可以用来访问MySQL。众所周知,mnoGoSearch与此内置库不兼容,只能与通用MySQL库一起使用。因此,如果将mnoGoSearch与MySQL一起使用,则在PHP配置期间,必须指明在mnoGoSearch配置期间使用的MySQL安装目录,例如:--with-mnogosearch --with-mysql=/usr
    Well since the function udm_get_res_param used with UDM_PARAM_NUM_ROWS does infact not get the number of rows on the current page, it can actualy find more rows than specified by UDM_PARAM_PAGE_SIZE, ??? well i had to rewrite the some stuff I also added highlightning since the parameter UDM_PARAM_HLBEG nor UDM_PARAM_HLEND doesnt seem to work, alot of things here are fairly undocumented, I also found out after reading the Search_Mnogosearch pear class that replacing "\2" and "\3" in the text with HLBEG and HLEND works. well here is the class rewritten (without fetch, just find): 
    <?php
    class MnogoSearch
    {
      public  $matches = NULL;
      public  $total  = NULL;
      public  $hilites = array('title', 'text');
      public  $hlbeg  = '<span class="hilite">';
      public  $hlend  = '</span>';
      protected $agent  = NULL;
      protected $res   = NULL;
      public function __construct() {
        $this->agent = udm_alloc_agent('mysql://user:pass@localhost/mnogosearch/');
        $this->set(UDM_PARAM_SEARCH_MODE, UDM_MODE_ALL);
        $this->set(UDM_PARAM_CHARSET, 'iso-8859-1');
        $this->set(UDM_PARAM_BROWSER_CHARSET, 'iso-8859-1');
        $this->set(UDM_PARAM_WORD_MATCH, UDM_MATCH_SUBSTR);
      }
      private function hiLite($t) {
        if ($t == '')
          return '';
        $t = str_replace("\2", $this->hlbeg, $t);
        $t = str_replace("\3", $this->hlend, $t);
        return $t;
      }
      public function set($k, $v) {
        udm_set_agent_param($this->agent, $k, $v);
      }
      public function find($q, $page=0, $rows=10) {
        if ($page < 0 || $rows <= 0) {
          $page = 0;
          $rows = 10;
        }
        $this->set(UDM_PARAM_PAGE_SIZE, $rows);
        $this->set(UDM_PARAM_PAGE_NUM, $page);
        $this->res = udm_find($this->agent, $q);
        $this->total = udm_get_res_param($this->res, UDM_PARAM_FOUND);
        $found = udm_get_res_param($this->res, UDM_PARAM_NUM_ROWS);
        if($found) {
          $b = udm_get_res_param($this->res, UDM_PARAM_FIRST_DOC);
          $e = udm_get_res_param($this->res, UDM_PARAM_LAST_DOC);
          $rows = $e - $b + 1;
          $fields = array('urlid', 'url', 'content', 'title', 'keywords', 'desc',
                  'text', 'size', 'rating', 'modified', 'order', 'crc', 'category',
                  'lang', 'charset', 'siteid', 'pop_rank', 'originid');
          for($i=0; $i<$rows; $i++){
            for($j=0; $j<count($fields); $j++) {
              $this->matches[$i][$fields[$j]] = udm_get_res_field($this->res, $i, $j+1);
            }
            foreach($this->hilites as $hilite) {
              $this->matches[$i][$hilite] = $this->hiLite($this->matches[$i][$hilite]);
            }
          }
          return $rows;
        } else {
          return FALSE;
        }
      }
      public function __destruct() {
        udm_free_agent($this->agent);
        if (isset($this->res)) {
          udm_free_res($this->res);
        }
      }
    }
    ?>
    Im thinking, maybe swish-e is better ? :)
    for win32 users, mnogosearch since version 3.2.x
    support COM interface
    for Reflection API of COM use Visual Studio .NET
    -> Tools ..
    -> OLE/COM object viewer
    - > C:\Program Files\mnoGoSearch\searchcom.dll
    simple code
    <pre>
    <?
    define('MNOGOSEARCH_WIN32',
      (strtoupper(substr(PHP_OS,0,3))=='WIN') && extension_loaded('COM'));
    if (!MNOGOSEARCH_WIN32)
     die('MnoGoSearch COM not loaded');
    /** Create COM object */
    $agent = new COM('MnoGoSearch.GoSearch')
              or die('Can\'t create COM object MnoGoSearch.GoSearch');
    com_load_typelib('MnoGoSearch.GoSearch');
    //mysql://user:passwd@host:port/database/?mode=multi
    $agent->DBAddr = $params['DBAddr'];
    //buggy, not work
    //$agent->DBMode = $params['DBMode'];
    $agent->SetCharset($params['Charset']);
    $agent->SetCacheMode(true);
    $q = 'q='. $query .'&np='. $_REQUEST['page'] .'&ps='. $_REQUEST['pg_size'] .'&m='. $_REQUEST['match'];
    $q .= '&wm='. $_REQUEST['word_match'] .'&ul='. $_REQUEST['url_match']. '&wf='. $_REQUEST['where_find'];
    $q .= '&typ='. $_REQUEST['content_type'] .'&s='. $_REQUEST['sortby'].'';
    //!!! EXECUTE SEARCH QUERY
    $result = $agent->Find($q);
    if ($agent->ErrorCode>0 || !$result->Valid)
      die('MnogoSearch win32 COM Error #'.$agent->ErrorCode.' - '.$agent->ErrorDescription);
    $last=$result->LastDoc;
    $first=$result->FirstDoc;
    $hl_begin = '<span class="mnogosearch_hilight">';
    $hl_end= '</span>';
    $fetched_result=array();
    $i=0;
    for($row=$first;$row<=$last;$row++) {
      $i=$row-$first;
      $line = $result->Line($row);
      $fetched_result[$i] = array(
        'order'=>$line->Section('Order', $hl_begin, $hl_end),
        'url'=> $line->Section('URL', $hl_begin, $hl_end),
        'relevance'=>$line->Section('Score', $hl_begin, $hl_end),
        'pagerank'=> $line->Section('Pop_Rank', $hl_begin, $hl_end),
        'content_type'=> $line->Section('Content-Type', $hl_begin, $hl_end),
        'content_length'=> $line->Section('Content-Length', $hl_begin, $hl_end),
        'last_time'=> $line->Section('Last-Modified', $hl_begin, $hl_end),
        'title'=> $line->Section('title', $hl_begin, $hl_end),
        'body'=> $line->Section('body', $hl_begin, $hl_end),
        'metadesc'=> $line->Section('meta.description', $hl_begin, $hl_end),
        'metakeywords'=> $line->Section('meta.keywords', $hl_begin, $hl_end)
      );
    }
    var_dump($fetched_result);
    echo '<br/>search time : '.$result->SearchSeconds;
    echo '<br/>words stats :'.$result->WordInfo;
    echo '<br/>pg_count: '.$result->NPages;
    echo '<br/>total_rows: '. $result->Count;
    echo '<br/>first_doc: '.$result->FirstDoc;
    echo '<br/>last_doc: '.$result->LastDoc;
    echo '<br/>rows_in_page: '.$result->RowsOnPage;
    ?>
    </pre>
    I have found that if you compile php with "--with-ftp and --with-mnogosearch=/dir" you must compile and build mnogosearch with "--disable-ftp" first. There are some naming conflicts between php's ftp libs and mno's ftp libs.
    Hope this helps