• 技术文章 >后端开发 >php教程

    php中如何使用sphinx搜索引擎

    小云云小云云2018-03-21 14:12:10原创1238
    sphinx是一个高效的搜索引擎,分词搜索的速度比较快,索引建立存储在硬盘文件,不会干扰数据库,有自己内置的一套数据库.希望能帮助到大家。

    php中使用sphinx搜索引擎

    sphinx是一个高效的搜索引擎,分词搜索的速度比较快,索引建立存储在硬盘文件,不会干扰数据库,有自己内置的一套数据库

    1.ubuntu安装sphinx

    如果没有安装aptitude ,需要先安装 aptitude 因为因为用apt get install 安装下面命令会出现问题.
    sudo apt-get install aptitude
    sudo aptitude install sphinx3 sphinx3-doc sphinxsearch sphinx-common -y

    2.配置


    1

    2


    cd /etc/sphinxsearch/

    cp sphinx.conf.sample sphinx.conf

    修改配置文件如下


    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


    source src1

    {

    type = mysql

    sql_host = localhost

    sql_user = root

    sql_pass = magicmoma

    sql_db = coupon_20160901

    sql_port = 3306 # optional, default is 3306

    sql_query = SELECT couponid,title,description FROM app_coupon_api

    ##### 需要包含主键,分词索引字段 ########

    }

    index test1

    {

    source = src1

    path = /var/lib/sphinxsearch/data/test1 #索引存放目录

    docinfo = extern

    mlock = 0

    morphology = none

    min_word_len = 1

    charset_type = utf-8

    min_prefix_len = 0

    min_infix_len = 0

    ngram_len = 1

    html_strip = 0

    }

    indexer

    {

    mem_limit = 2048M

    }


    searchd

    {

    listen = 9312

    listen = 9306:mysql41

    log = /var/log/sphinxsearch/searchd.log

    query_log = /var/log/sphinxsearch/query.log

    read_timeout = 5

    client_timeout = 300

    max_children = 30

    pid_file = /var/run/sphinxsearch/searchd.pid

    max_matches = 1000

    seamless_rotate = 1

    preopen_indexes = 1

    unlink_old = 1

    mva_updates_pool = 1M

    max_packet_size = 8M

    max_filters = 256

    max_filter_values = 4096

    max_batch_queries = 32

    workers = threads # for RT to work

    }


    1. 执行命令分词,会在/var/lib/sphinxsearch/data/test1 目录下生成一堆索引文件
      sudo indexer -c /etc/sphinxsearch/sphinx.conf test1

      test1为上述配置文件的index名字

    4.命令行测试搜索

    sudo search -c /etc/sphinxsearch/sphinx.conf google

    二.在php中使用

    1.安装php sphinx的依赖库

    1.安装 aptitude

    apt-get install aptitude
    sudo aptitude install libsphinxclient-dev libsphinxclient-0.0.1 -y

    2.安装php sphinx的扩展

    安装 pecl
    sudo apt-get install php-pear php5-dev
    在安装sphinx
    sudo pecl install sphinx

    3.在配置文件php.ini中添加sphinx的扩展,

    我的php.ini文件为
    sudo vim /etc/php5/fpm/php.ini
    获取自己的php.ini文件位置使用
    php5-fpm -i|grep ini

    添加:
    extension=sphinx.so
    4.重启php5-fpm,查看php是否加载sphinx模块
    sudo /etc/init.d/php5-fpm restart
    5.将search程序运行在后台
    sudo searchd -c /etc/sphinxsearch/sphinx.conf
    默认监听配置文件中的端口:9312

    6.在thinkphp中调用搜索


    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13


    public function testSphinx()

    {

    $s = new \SphinxClient;

    $s->setServer("localhost", 9312);

    $s->SetArrayResult (true );

    $s->setMatchMode(SPH_MATCH_ANY);

    $s->setMaxQueryTime(3);

    $result = $s->query("test");

    $result = $result['matches'];

    $result = array_column($result,'id');

    $list = M('CouponApi')->field('couponid,title,description')->where(array('couponid'=>array('in',$result)))->select();

    dump($list);

    }

    搜索完毕,返回结果(默认返回20条,修改返回条数用添加 $s->SetLimits(0, 1000, 1000);),搜索速度相当快,索引80w条数据的title和description两个字段耗时不到10s,该搜索引擎支持增量索引,多种模式的搜索,网上的资料也非常多

    以上就是php中如何使用sphinx搜索引擎的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:sphinx 搜索引擎 php
    上一篇:php中curl抓取网页响应数据 下一篇:三种PHP字符串界定符的区别详解
    20期PHP线上班

    相关文章推荐

    精选22门好课,价值3725元,开通VIP免费学习!• 细思极恐?关于in_array的第3个参数• 关于HTML登陆界面的有关问题。• 今天又犯2了-小弟我不是在犯2,就是在犯2的路下• 关于PHP传值与传引用的奇怪有关问题• 建站之三:PHP网页兑现
    1/1

    PHP中文网