Python下Mysql数据库Coreseek搜索架构配置与应用

来源:互联网 发布:c语言中rand 编辑:程序博客网 时间:2024/06/10 08:59


之前做过一次coreseek的全文检索,今天配置新服务器 发现之前好多东西都忘记了,特此整理一下,日后用上时可以有备无患。

有需要的朋友可以参考参考下。


第一步:下载coreseek 3.2.14 for windows,并安装

1、下载后解压得到源程序目录 coreseek-3.2.14-win32,文件夹重命名一下,简单一些,命名为 coreseek

2、将 coreseek文件夹移动到 d:\下(根据你个人爱好,放哪都可以);

3开始”——>“运行”——>输入cmd,打开命令行窗口——>执行 "d: 回车"——>执行 "cd coreseek\",进入到 coreseek目录下;

4、执行 "set PATH=%D:\coreseek%\bin;%PATH%"。设置path,目的是为了能够调用bin目录下的 cat.exeiconv.exe等;

5、设置环境变量path 添加变量;

 

第二步:mysql数据源的配置与创建相关数据库索引文件

 

1、配置 mysql数据源及测试(在 coreseek\etc\csft_mysql.conf文件)

 

#源定义

source name           #索引名称例如circle

{

    type               = mysql       #数据库类型例如“mysql

    sql_host           = localhost    #host      例如“localhost

    sql_user           = root           #名称   

    sql_pass           = 123456     #密码      

    sql_db             = database        #数据库名  例如“circle

    sql_port           = 3306

    sql_query_pre      = SET NAMES utf8

 

    sql_query          = SELECT id, title, content FROM Circle        #sql_query第一列id需为整数,查询语句

                                                                   #titlecontent作为字符串/文本字段,被全文索引

    sql_attr_uint           = group_id                #SQL读取到的值必须为整数

    sql_attr_timestamp      = date_added            #SQL读取到的值必须为整数,作为时间属性

 

    sql_query_info_pre      = SET NAMES utf8        #命令行查询时,设置正确的字符集

    sql_query_info          = SELECT * FROM documents WHEREid=$id        #命令行查询时,从数据库读取原始数据信息

}

 

#index定义

index circle

{

    source            = circle             #对应的source名称

    path            = var/data/mysql    #请修改为实际使用的绝对路径,例如:/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/     #BSDLinux环境下设置,/符号结尾

    charset_dictpath    = d:/www/coreseek/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 = var/log/searchd_mysql.pid  #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...

    log = var/log/searchd_mysql.log        #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...

    query_log = var/log/query_mysql.log #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...

}

 

2、创建索引

在命令行窗口下执行 " bin\indexer-c d:\coreseek\etc\csft_mysql.conf circle"(此处一定为全路径),即:创建名为circle的数据源的索引。如果需要创建 csft_mysql.conf 配置文件中的所有索引,刚使用 --all替代 circle

 

CMD下:

D:\www\coreseek>bin\indexer-c d:\coreseek\etc\csft_mysql.conf circle回车

Coreseek Fulltext 3.2 [Sphinx 0.9.9-release (r2117)]

Copyright (c) 2007-2011,

Beijing Choice SoftwareTechnologies Inc (http://www.coreseek

 

 using config file 'etc\csft_mysql.conf'...

indexing index 'weibo'...

collected 8 docs, 0.0 MB

sorted 0.0 Mhits, 100.0%done

total 8 docs, 1381 bytes

total 0.188 sec, 7319bytes/sec, 42.40 docs/sec

total 2 reads, 0.000 sec,1.2 kb/call avg, 0.0 msec/call avg

total 7 writes, 0.000 sec,0.9 kb/call avg, 0.1 msec/call avg

 

此时,在 coreseek\var\data 下发现,生成6个文件。

 

第三步:数据测试(命令行下不需要开启 searchd服务)

1、测试搜索中文。命令“ echo搜索的词 | iconv -f gbk -t utf-8 | search -c etc\csft_mysql.conf--stdin | iconv -f utf-8 -t gbk ”

 

CMD下:


 CoreseekFulltext 3.2 [ Sphinx 0.9.9-release(r2117)]

Copyright (c) 2007-2011,

Beijing Choice SoftwareTechnologies Inc (http://www.coreseek.com)

 

 using config file 'etc\csft_mysql.conf'...

index 'weibo': query '生意

': returned 1 matches of 1total in 0.013 sec

 

displaying matches:

1. document=2, weight=1,hit=22, dates=Thu Jan 01 08:33:32 1970

        id=2

        title=队照排货照炒百度生意照做

        content=他还告诉记者,如果顾客在他们店购买iPhone5iPhone4S的回收价格可

以再往上浮动300元左右,记者随后咨询了另外几家手机店,发现回收价格基本在1500元至2

000元之间

        hit=22

        dates=2012-10-01

 

words:

1.    '生意': 1 documents, 1 hits

 

此时coreseek配置以及创建索引结束

 

第四步:安装/开启searchd服务

1.    安装searchd服务:cmd下命令, 
(更正下:此处为csft_mysql.conf)

当出现successly字样后说明服务安装成功。

2.    启动searchd服务:cmd下命令:services.msc
找到searchd服务启动
 
综上所有配置已经成功!
 
 

 

coreseekpython中的调用应用实例:

Coreseek文件中有sphinxapi

创建sphinxhelper.py,代码如下:

 

# encoding=utf8

__author__ = 'Administrator'

from extensions.sphinxapiimport *

 

 

class SphinxHelper(object):

    __MODE__ = SPH_MATCH_ALL

    __HOST__ = 'localhost'

    __PORT__ = 9312

    __INDEX__ = ""

    __WEIGHT__ = 100

 

    __FILTER_COL__ = None

    __FILTER_VAl__ = None

    __SORT_BY__ = None

    __GROUP_BY__ = None

    __GROUP_SORT__ = None

    __SPHINX_CLIENT__ = None

 

    def __init__(self,host="localhost", port=9312, mode=None, index="circle",

                 weight=100, filter_col=None,filter_val=None, sort_by=None,

                 group_by=None, group_sort=None):

        if mode and mode == "b":

            self.__MODE__ = SPH_MATCH_BOOLEAN

        elif mode and mode == "e":

            self.__MODE__ = SPH_MATCH_EXTENDED

 

        self.__HOST__ = host

        self.__PORT__ = port

        self.__INDEX__ = index

        self.__WEIGHT__ = weight

        self.__FILTER_COL__ = filter_col

        self.__FILTER_VAl__ = filter_val

        self.__SORT_BY__ = sort_by

        self.__GROUP_BY__ = group_by

        self.__GROUP_SORT__ = group_sort

 

        if not self.__SPHINX_CLIENT__:

            client = SphinxClient()

            client.SetServer(self.__HOST__,self.__PORT__)

            client.SetWeights([self.__WEIGHT__,1])

            client.SetMatchMode(self.__MODE__)

            self.__SPHINX_CLIENT__ = client

 

    def query(self, key_word, page=1,page_size=20):

        self.__SPHINX_CLIENT__.SetLimits((page- 1) * page_size, page_size, max(page_size, 1000))

 

        res =self.__SPHINX_CLIENT__.Query(key_word, self.__INDEX__)

        if not res:

            print 'query failed: %s' %self.__SPHINX_CLIENT__.GetLastError()

 

        ifself.__SPHINX_CLIENT__.GetLastWarning():

            print 'WARNING: %s\n' %self.__SPHINX_CLIENT__.GetLastWarning()

 

        #print 'Query \'%s\' retrieved %d of %dmatches in %s sec' % (

            #key_word, res['total'],res['total_found'], res['time'])

        total = res['total']

        if "words" in res.keys():

            # for info in res['words']:

            # print '%s found %d times in %ddocuments' % (info['word'], info['hits'], info['docs'])

 

            if "matches" inres.keys():

                searched_id_list =[str(item['id']) for item in res['matches']]

                id_list_string =",".join(searched_id_list)

 

                return id_list_string, total

 

        return None, 0

 

    def build_excerpts(self, target_docs=None,key_word=None):

        opts = {'before_match': '<fontcolor="red">', 'after_match': '</font>',

                'chunk_separator': ' ... ','limit': 0, 'around': 0}

 

        returnself.__SPHINX_CLIENT__.BuildExcerpts(target_docs, self.__INDEX__, key_word,opts)

 

Control中调用:

Sphinx_helper =SphinxHelper()

Key_word=[“要搜索的关键词”]

id_list_string, total_count= sphinx_helper.query(Key_word)

 

id_list_string, total_count即为查询数据的ID与符合查询条件的条数。

 

0 0
原创粉丝点击