使用Sphinx搜索多个全文字段

时间:2017-11-16 20:48:57

标签: php mysql sphinx

我正在尝试使用sphinxsearch搜索多个字段,实质上是为了解决搜索过滤属性中使用的数字ID的限制(数据库使用大量字母数字uniqID作为ID)。

这是Sphinx配置中使用的主要搜索:

sql_query               = \
        SELECT text_page.id, text_page.document_id, documents.startdate, documents.enddate, documents.long_title, documents.volume,text_page.images_page_id, text_page.text, \
        series.name, series.id AS series_id, series.white_label_id AS white_label_id, \
        documents.date_created\
        FROM text_page \
        INNER JOIN documents ON text_page.document_id = documents.id \
        INNER JOIN series ON documents.series_id = series.id

text_page.text是主要的全文字段。

我已将此行添加到配置中,以尝试将此行全文编入索引:

sql_field_string = white_label_id

然后,我尝试通过PHP Sphinx类运行以下查询来创建由white_label_id缩小的查询。

"@text (search words) @white_label_id (some-uniq-id)"

据我了解from here,这应该意味着@text和@white_label_id必须在数据库行上产生命中才能返回结果。

但是查询不会产生任何结果,也没有错误或警告。

关于这里出了什么问题的任何建议?是因为white_label_idtext字段位于不同的表中吗?是否存在避免重构数据库以使用数字ID的解决方案?

编辑:

根据要求,这是一个完整的配置文件。 请注意,目前代码仍在使用PHP Sphinx类,而不是通过mysqli使用SphinxQL。

source src2
{

    sql_host                = localhost
    sql_user                = username
    sql_pass                = password
    sql_db                  = databasename
    sql_port                = 3306  # optional, default is 3306 
    sql_query_pre           = SET NAMES utf8

    sql_query               = \
        SELECT text_page.id, text_page.document_id, documents.startdate, documents.enddate, documents.long_title, documents.volume,text_page.images_page_id, text_page.text, \
        series.name, series.id AS series_id, series.white_label_id AS white_label_id, \
        documents.date_created\
        FROM text_page \
        INNER JOIN documents ON text_page.document_id = documents.id \
        INNER JOIN series ON documents.series_id = series.id


    sql_attr_uint                   = startdate
    sql_attr_uint                   = enddate
    sql_attr_uint                   = volume

    sql_attr_timestamp      = date_created


    sql_attr_string     = long_title
    sql_attr_string     = name
    #sql_attr_string        = white_label_id #NB - does not work with nonnumeric ids
    sql_attr_string     = document_id
    sql_attr_string     = series_id

    sql_field_string = white_label_id  #currently appears to do nothing


    sql_ranged_throttle = 0 
}

source src2throttled : src2
{
    sql_ranged_throttle         = 100
}

index myindex11
{

    source          = src2
    path            = /var/data/mydata1
    docinfo         = extern
    mlock           = 0
    morphology      = none
    min_word_len        = 1
    charset_type        = utf-8
    html_strip              = 0

}

index myindex1stemmed : myindex1
{
    path            = /var/data/mydata1stemmed
    morphology      = stem_en
    index_exact_words   = 1
}

1 个答案:

答案 0 :(得分:0)

最终事实证明,解决Sphinx列id上的'仅数字'规则是一个更好的解决方案。

答案是创建基于文本的uniq_id列的数字哈希,然后可以将其用作sql_attr_uint来缩小搜索范围。

例如,原始帖子中的SQL查询变为:

sql_query               = \
        SELECT text_page.id, text_page.document_id, documents.startdate, documents.enddate, documents.long_title, documents.volume,text_page.images_page_id, text_page.text, \
        series.name, series.id AS series_id, CRC32(series.white_label_id) AS white_label_id, \
        documents.date_created\
        FROM text_page \
        INNER JOIN documents ON (text_page.document_id = documents.id AND documents.is_active = 1) \
        INNER JOIN series ON documents.series_id = series.id