如何在sphinx的sql查询源中选择列

时间:2017-03-21 13:04:56

标签: sphinx

我正在使用sphinx索引器根据我的mysql数据库中的文档创建一个字典,但我无法将源的sql查询限制为所选列。

这是我使用的命令

indexer --buildstops dict.txt 1000 --verbose --print-queries --dump-rows listing_rows --buildfreqs listing_core -c config/development.sphinx.conf

使用development.sphinx.conf中的以下源代码,找不到文档且dict.txt为空

source listing_source {
  type = mysql
  sql_host = mysql
  sql_user = sharetribe
  sql_pass = secret
  sql_db = sharetribe_development
  sql_query = SELECT title AS title, description AS description FROM listings;
}

输出

Sphinx 2.2.10-id64-release (2c212e0)
Copyright (c) 2001-2015, Andrew Aksyonoff
Copyright (c) 2008-2015, Sphinx Technologies Inc (http://sphinxsearch.com)

using config file 'config/development.sphinx.conf'...
WARNING: key 'max_matches' was permanently removed from Sphinx configuration. Refer to documentation for details.
WARNING: key 'charset_type' was permanently removed from Sphinx configuration. Refer to documentation for details.
WARNING: key 'enable_star' was permanently removed from Sphinx configuration. Refer to documentation for details.
WARNING: key 'charset_type' was permanently removed from Sphinx configuration. Refer to documentation for details.
WARNING: key 'enable_star' was permanently removed from Sphinx configuration. Refer to documentation for details.
indexing index 'listing_core'...
building stopwords list...
SQL-CONNECT: ok
SQL-QUERY: SELECT title AS title, description AS description FROM listings;: ok
total 0 docs, 0 bytes
total 0.008 sec, 0 bytes/sec, 0.00 docs/sec
total 0 reads, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg
total 0 writes, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg

当我更改sql_query以返回所有列时,索引器会找到预期的文档数(2)并将它们添加到字典中。

source listing_source {
  type = mysql
  sql_host = mysql
  sql_user = sharetribe
  sql_pass = secret
  sql_db = sharetribe_development
  sql_query = SELECT * FROM listings;
}

输出:

Sphinx 2.2.10-id64-release (2c212e0)
Copyright (c) 2001-2015, Andrew Aksyonoff
Copyright (c) 2008-2015, Sphinx Technologies Inc (http://sphinxsearch.com)

using config file 'config/development.sphinx.conf'...
WARNING: key 'max_matches' was permanently removed from Sphinx configuration. Refer to documentation for details.
WARNING: key 'charset_type' was permanently removed from Sphinx configuration. Refer to documentation for details.
WARNING: key 'enable_star' was permanently removed from Sphinx configuration. Refer to documentation for details.
WARNING: key 'charset_type' was permanently removed from Sphinx configuration. Refer to documentation for details.
WARNING: key 'enable_star' was permanently removed from Sphinx configuration. Refer to documentation for details.
indexing index 'listing_core'...
building stopwords list...
SQL-CONNECT: ok
SQL-QUERY: SELECT * FROM listings;: ok
total 2 docs, 485 bytes
total 0.008 sec, 56303 bytes/sec, 232.18 docs/sec
total 0 reads, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg
total 0 writes, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg

如何限制查询仅返回所选列?

1 个答案:

答案 0 :(得分:1)

  

sql_query = SELECT title AS title,description AS description FROM listings;

不起作用,因为它没有document_id。添加一个id列(作为第一个!),它应该工作。你也不需要' AS'如果使用相同的名称。 (*可能有效,因为像列一样的id恰好是第一个:)

因此,请确保包含ID,然后将您想要的列命名为字段 ...

sql_query = SELECT id, title, description FROM listings