我浏览了几十篇文章和论坛帖子,翻阅了官方文档,却找不到答案。这个文章听起来很有希望,因为它说的是The data to be indexed can generally come from very different sources: SQL databases, plain text files, HTML files,但不幸的是,与其他所有文章和论坛线程一样,它专门讨论MySQL。
听到Sphinx这么酷,它可以做到这一点,而且它几乎可以用任何你喜欢的数据源做任何你想做的事情,这是相当奇怪的。但是,除了MySQL之外,那些具有数据源的示例都在哪里呢?当您想要扫描世界上最简单的数据源-纯文本文件时,只需一步一步地浏览Sphinx配置的一个极小且琐碎的示例。比方说,我已经安装了Sphinx,并希望(递归地)扫描我的主目录,以查找所有包含"Hello“的纯文本文件。我应该做些什么来实现这一点呢?
先决条件
Ubuntusudo apt-get install sphinxsearch发布于 2015-06-13 04:45:35
在继续没有SQL的狮身人面像!之前,请看一看这个。
理想情况下我会这么做。
我们将使用狮身人面像的字段来索引带有文件路径的表。下面是PHP脚本,用于为特定目录创建一个具有文件路径的表(Scandir)。
<?php
$con = mysqli_connect("localhost","root","password","database");
mysqli_query($con,"CREATE TABLE fileindex ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,text VARCHAR(100) NOT NULL);");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$dir = scandir('/absolute/path/to/your/dir/');
foreach ($dir as $entry) {
if (!is_dir($entry)) {
$path= "/absolute/path/to/your/dir/$entry";
mysqli_query($con,"INSERT INTO fileindex ( text ) VALUES ( '$path' )");
}
}
mysqli_close($con);
?>下面的代码是sphinx.conf文件,用于用文件路径对表进行索引。注意,sql_file_field将索引文本(Filepath)列中指定的文件
source src1
{
type = mysql
sql_host = localhost
sql_user = root
sql_pass = password
sql_db = filetest
sql_port = 3306 # optional, default is 3306
sql_query_pre = SET CHARACTER_SET_RESULTS=utf8
sql_query_pre = SET NAMES utf8
sql_query = SELECT id,text from fileindex
sql_file_field = text
}
index filename
{
source = src1
path = /var/lib/sphinxsearch/data/files
docinfo = extern
}
indexer
{
mem_limit = 128M
}
searchd
{
log = /var/log/sphinxsearch/searchd.log
pid_file = /var/log/sphinxsearch/searchd.pid
}创建表后,将sphinx.conf保存在/etc/sphinxsearch/sphinx.conf中,只需运行sudo文件名-旋转,您的索引就准备好了!键入“搜索”,然后键入关键字以获得结果。
https://stackoverflow.com/questions/30813530
复制相似问题