Sphinx全文索引引擎

1、什麼是sphinxphp

原理:sphinx將數據庫中的表創建索引,php操做sphinx時,將要查詢的關鍵字進行匹配,返回一個id,php經過id到數據庫中查詢數據。html

2、下載mysql

連接:https://pan.baidu.com/s/1ic3JTra4NKbEgxV0bAjXsw
提取碼:b436linux

重要文件說明:sql

3、使用sphinx數據庫

 一、複製csft_mysql.conf文件到sphinx根目錄,並修更名稱爲sphinx.confwindows

二、配置sphinx.conf服務器

#MySQL數據源配置,詳情請查看:http://www.coreseek.cn/products-install/mysql/
#請先將var/test/documents.sql導入數據庫,並配置好如下的MySQL用戶密碼數據庫

#源定義
source exhibit
{
    type                    = mysql

    sql_host                = localhost
    sql_user                = root
    sql_pass                = root
    sql_db                    = test
    sql_port                = 3306
    sql_query_pre            = SET NAMES utf8
    
    #要求:第一個字段必須是ID,若是不叫ID能夠起個別名叫ID(類型必須是非零、惟1、不重複的整數)
    #sql_query第一列id需爲整數
    #title、content做爲字符串/文本字段,被全文索引
    sql_query                = SELECT itemid as id, title, keyword, address FROM destoon_exhibit 
    
    sql_attr_uint            = itemid           #從SQL讀取到的值必須爲整數
    #sql_attr_timestamp        = date_added     #從SQL讀取到的值必須爲整數,做爲時間屬性
    
    #命令行查詢時,設置正確的字符集
    sql_query_info_pre      = SET NAMES utf8                                        
    #命令行查詢時,從數據庫讀取原始數據信息
    #sql_query_info            = SELECT * FROM documents WHERE id=$id 
}

#index定義
index exhibit
{
    source            = exhibit             #對應的source名稱
    path            = G:/phpstudy/Sphinx/var/data/exhibit #請修改成實際使用的絕對路徑,例如:/usr/local/coreseek/var/...
    docinfo            = extern
    mlock            = 0
    morphology        = none
    min_word_len        = 1
    html_strip                = 0

    #中文分詞配置,詳情請查看:http://www.coreseek.cn/products-install/coreseek_mmseg/
    #charset_dictpath = /usr/local/mmseg3/etc/ #BSD、Linux環境下設置,/符號結尾
    charset_dictpath = G:/phpstudy/Sphinx/etc/                             #Windows環境下設置,/符號結尾,最好給出絕對路徑,例如:C:/usr/local/coreseek/etc/...
    charset_type        = zh_cn.utf-8
}

#全局index定義
indexer
{
    mem_limit            = 128M
}

#searchd服務定義
searchd
{
    listen                  =   9312
    read_timeout        = 5
    max_children        = 30
    
    # 最大返回的記錄數(即便查詢出的記錄數量多也只返回這些數據)
    max_matches            = 1000
    seamless_rotate        = 0
    preopen_indexes        = 0
    unlink_old            = 1
    pid_file = G:/phpstudy/Sphinx/var/log/searchd_mysql.pid  #請修改成實際使用的絕對路徑,例如:/usr/local/coreseek/var/...
    log = G:/phpstudy/Sphinx/var/log/searchd_mysql.log        #請修改成實際使用的絕對路徑,例如:/usr/local/coreseek/var/...
    query_log = G:/phpstudy/Sphinx/var/log/query_mysql.log #請修改成實際使用的絕對路徑,例如:/usr/local/coreseek/var/...
}

三、安裝sphinx服務器less

四、建立索引ui

五、開啓sphinx服務

六、安裝php擴展

下載地址:

連接:https://pan.baidu.com/s/1kVInF6Whk2y0Vwsv9rwhvA
提取碼:vd97
複製這段內容後打開百度網盤手機App,操做更方便哦

將php_sphinx.dll文件複製到如圖地址:

重啓服務器:

4、PHP操做Sphinx

<?php
/**
 * Created by PhpStorm.
 * User: Yang
 * Date: 2019/8/14
 * Time: 16:16
 */

$sphinx = new SphinxClient();
//設置searchd的主機名和TCP端口
$sphinx->SetServer("localhost", 9312);
//設置鏈接超時
$sphinx->SetConnectTimeout(3);
//控制搜索結果集的返回格式
$sphinx->SetArrayResult(true);
//設置全文查詢的匹配模式
/*
SPH_MATCH_ALL    匹配全部查詢詞(默認模式).
SPH_MATCH_ANY    匹配查詢詞中的任意一個.
SPH_MATCH_PHRASE    將整個查詢看做一個詞組,要求按順序完整匹配.
SPH_MATCH_BOOLEAN    將查詢看做一個布爾表達式.
SPH_MATCH_EXTENDED    將查詢看做一個Sphinx內部查詢語言的表達式.
SPH_MATCH_FULLSCAN    使用徹底掃描,忽略查詢詞彙.
SPH_MATCH_EXTENDED2    相似 SPH_MATCH_EXTENDED ,並支持評分和權重.
*/
$sphinx->SetMatchMode(SPH_MATCH_ANY);
$q = "2017";
$result = $sphinx->Query($q);
var_dump($result['matches']);
$id_array = array_column($result['matches'], "id");
$ids = implode(",", $id_array);
echo $ids;
array(20) {
  [0]=>
  array(3) {
    ["id"]=>
    string(4) "9388"
    ["weight"]=>
    int(2574)
    ["attrs"]=>
    array(1) {
      ["addtime"]=>
      string(10) "1488729600"
    }
  }
  [1]=>
  array(3) {
    ["id"]=>
    string(5) "24571"
    ["weight"]=>
    int(2574)
    ["attrs"]=>
    array(1) {
      ["addtime"]=>
      string(10) "1488729600"
    }
  }
  [2]=>
  array(3) {
    ["id"]=>
    string(2) "68"
    ["weight"]=>
    int(2569)
    ["attrs"]=>
    array(1) {
      ["addtime"]=>
      string(10) "1504195200"
    }
  }
  [3]=>
  array(3) {
    ["id"]=>
    string(2) "81"
    ["weight"]=>
    int(2569)
    ["attrs"]=>
    array(1) {
      ["addtime"]=>
      string(10) "1504195200"
    }
  }
...
} 9388,24571,68,81,3186,3213,3278,3444,3470,3645,3785,3843,3890,3907,4120,4164,4182,4212,4235,4568

注意:要經過定時器定時創建索引。

5、Linux下安裝Sphinx

連接:https://pan.baidu.com/s/1tUF8Y5imp-ryHxoDDHH2MQ
提取碼:4pav 

tar -zxvf sphinx-3.0.3-facc3fb-linux-amd64.tar.gz

 

 解壓出來不用安裝,像windows下同樣配置便可。

相關文章
相關標籤/搜索