sphinx 在windows 环境下 安装 .

sphinx 在windows 环境下 安装 .

一般而言,Sphinx是一个独立的搜索引擎,意图为其他应用提供高速、低空间占用、高结果相关度的全文搜索功能。Sphinx可以非常容易的与SQL数据库和脚本语言集成。

当前系统内置MySQL和PostgreSQL 数据库数据源的支持,也支持从标准输入读取特定格式的XML数据。通过修改源代码,用户可以自行增加新的数据源(例如:其他类型的DBMS的原生支持)。

搜索API支持PHP、Python、Perl、Rudy和Java,并且也可以用作MySQL存储引擎。搜索API非常简单,可以在若干个小时之内移植到新的语言上。

Sphinx特性:

  • 高速的建立索引(在当代CPU上,峰值性能可达到10MB/秒);
  • 高性能的搜索(在2–4GB的文本数据上,平均每次检索响应时间小于0.1秒);
  • 可处理海量数据(目前已知可以处理超过100GB的文本数据,在单一CPU的系统上可处理100M文档);
  • 提供了优秀的相关度算法,基于短语相似度和统计(BM25)的复合Ranking方法;
  • 支持分布式搜索;
  • 提供文件的摘录生成;
  • 可作为MySQL的存储引擎提供搜索服务;
  • 支持布尔、短语、词语相似度等多种检索模式;
  • 文档支持多个全文检索字段(最大不超过32个);
  • 文档支持多个额外的属性信息(例如:分组信息,时间戳等);
  • 停止词查询;
  • 支持单一字节编码和UTF-8编码;
  • 原生的MySQL支持(同时支持MyISAM和InnoDB);
  • 原生的PostgreSQL支持.

中文手册可以在获得,感谢的辛勤工作。


二、Sphinx在windows上的安装

1.直接在找到最新的windows(sphinx-2.0.6-release-win32.zip)版本,下载后解压在D:\sphinx目录下;

2.在D:\sphinx\下新建一个data目录用来存放索引文件,一个log目录方日志文件,复制D:\sphinx\sphinx.conf.in到D:\sphinx\bin\sphinx.conf(注意修改文件名);

3.修改D:\sphinx\bin\sphinx.conf,我这里列出需要修改的几个:

type        = mysql # 数据源,我这里是mysql sql_host    = localhost # 数据库服务器 sql_user    = root # 数据库用户名 sql_pass    = '' # 数据库密码 sql_db      = sphinx # 数据库 sql_port    = 3306 # 数据库端口sql_query_pre   = SET NAMES utf8 # 去掉此行前面的注释,如果你的数据库是uft8编码的index test1 { # 放索引的目录 path   = D:/sphinx/data/ # 编码 charset_type  = utf-8 #  指定utf-8的编码表 charset_table  = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F # 简单分词,只支持0和1,如果要搜索中文,请指定为1 ngram_len    = 1 # 需要分词的字符,如果要搜索中文,去掉前面的注释 ngram_chars   = U+3000..U+2FA1F }# index test1stemmed : test1 # { # path   = @CONFDIR@/data/test1stemmed # morphology  = stem_en # } # 如果没有分布式索引,注释掉下面的内容 # index dist1 # { # 'distributed' index type MUST be specified # type    = distributed # local index to be searched # there can be many local indexes configured # local    = test1 # local    = test1stemmed # remote agent # multiple remote agents may be specified # syntax is 'hostname:port:index1,[index2[,...]] # agent    = localhost:3313:remote1 # agent    = localhost:3314:remote2,remote3 # remote agent connection timeout, milliseconds # optional, default is 1000 ms, ie. 1 sec # agent_connect_timeout = 1000 # remote agent query timeout, milliseconds # optional, default is 3000 ms, ie. 3 sec # agent_query_timeout  = 3000 # }# 搜索服务需要修改的部分 searchd { # 日志 log     = D:/sphinx/log/searchd.log # PID file, searchd process ID file name pid_file   = D:/sphinx/log/searchd.pid # windows下启动searchd服务一定要注释掉这个 # seamless_rotate  = 1 }

4.导入测试数据

C:\Program Files\MySQL\MySQL Server 5.0\bin>mysql -uroot test<d:/sphinx/example.sql

5.建立索引

D:\sphinx\bin>indexer.exe –all
Sphinx 0.9.8-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoff
using config file ‘./sphinx.conf’…
indexing index ‘test1′…
collected 4 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 4 docs, 193 bytes
total 0.101 sec, 1916.30 bytes/sec, 39.72 docs/sec
D:\sphinx\bin>

6.搜索’test’试试

D:\sphinx\bin>search.exe test
Sphinx 0.9.8-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoff
using config file ‘./sphinx.conf’…
index ‘test1′: query ‘test ‘: returned 3 matches of 3 total in 0.000 sec
displaying matches:
1. document=1, weight=2, group_id=1, date_added=Wed Nov 26 14:58:59 2008
        id=1
        group_id=1
        group_id2=5
        date_added=2008-11-26 14:58:59
        title=test one
        content=this is my test document number one. also checking search within
 phrases.
2. document=2, weight=2, group_id=1, date_added=Wed Nov 26 14:58:59 2008
        id=2
        group_id=1
        group_id2=6
        date_added=2008-11-26 14:58:59
        title=test two
        content=this is my test document number two
3. document=4, weight=1, group_id=2, date_added=Wed Nov 26 14:58:59 2008
        id=4
        group_id=2
        group_id2=8
        date_added=2008-11-26 14:58:59
        title=doc number four
        content=this is to test groups
words:
1. ‘test’: 3 documents, 5 hits
D:\sphinx\bin>

都所出来了吧。

6.测试中文搜索

修改test数据库中documents数据表,

UPDATE `test`.`documents` SET `title` = ‘测试中文’, `content` = ‘this is my test document number two,应该搜的到吧’ WHERE `documents`.`id` = 2;

重建索引:

D:\sphinx\bin>indexer.exe –all

搜索’中文’试试:

D:\sphinx\bin>search.exe 中文
Sphinx 0.9.8-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoff
using config file ‘./sphinx.conf’…
index ‘test1′: query ‘中文 ‘: returned 0 matches of 0 total in 0.000 sec
words:
D:\sphinx\bin>

貌似没有搜到,这是因为windows命令行中的编码是gbk,当然搜不出来。我们可以用程序试试,在D:\sphinx\api下新建一个foo.php的文件,注意utf-8编码

<?php
require ‘sphinxapi.php’;
$s = new SphinxClient();
$s->SetServer(‘localhost’,9312);
$result = $s->Query(‘中文’);
var_dump($result);
?>

启动Sphinx searchd服务

D:\sphinx\bin>searchd.exe
Sphinx 0.9.8-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoff
WARNING: forcing –console mode on Windows
using config file ‘./sphinx.conf’…
creating server socket on 0.0.0.0:3312
accepting connections

执行PHP查询:

php d:/sphinx/api/foo.php

或者在浏览器中调用均可
//searchd.exe  开启sphinx 服务端进程
//search.exe  调用sphinx搜索测试

结果是不是出来?剩下的工作就是去看手册,慢慢摸索高阶的配置。


附:数据库

CREATE TABLE documents (
id int(11) NOT NULL auto_increment,
group_id int(11) NOT NULL,
group_id2 int(11) NOT NULL,
date_added datetime NOT NULL,
title varchar(255) NOT NULL,
content text NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=5;
INSERT INTO documents VALUES ('1', '1', '5', '2008-09-13 21:37:47', 'test one', 'this is my test document number one. also checking search within phrases.');
INSERT INTO documents VALUES ('2', '1', '6', '2008-09-13 21:37:47', 'test two', 'this is my test document number two');
INSERT INTO documents VALUES ('3', '2', '7', '2008-09-13 21:37:47', 'another doc', 'this is another group');
INSERT INTO documents VALUES ('4', '2', '8', '2008-09-13 21:37:47', 'doc number four', 'this is to test groups');
UPDATE documents SET title = ‘测试中文’, content = ‘this is my test document number two,应该搜的到吧’ WHERE id = 2;

Read more

前端防范 XSS(跨站脚本攻击)

目录 一、防范措施 1.layui util  核心转义的特殊字符 示例 2.js-xss.js库 安装 1. Node.js 环境(npm/yarn) 2. 浏览器环境 核心 API 基础使用 1. 基础过滤(默认规则) 2. 自定义过滤规则 (1)允许特定标签 (2)允许特定属性 (3)自定义标签处理 (4)自定义属性处理 (5)转义特定字符 常见场景示例 1. 过滤用户输入的评论内容 2. 允许特定富文本标签(如富文本编辑器内容) 注意事项 更多配置 XSS(跨站脚本攻击)是一种常见的网络攻击手段,它允许攻击者将恶意脚本注入到其他用户的浏览器中。

详细教程:如何从前端查看调用接口、传参及返回结果(附带图片案例)

详细教程:如何从前端查看调用接口、传参及返回结果(附带图片案例)

目录 1. 打开浏览器开发者工具 2. 使用 Network 面板 3. 查看具体的API请求 a. Headers b. Payload c. Response d. Preview e. Timing 4. 实际操作步骤 5. 常见问题及解决方法 a. 无法看到API请求 b. 请求失败 c. 跨域问题(CORS) 作为一名后端工程师,理解前端如何调用接口、传递参数以及接收返回值是非常重要的。下面将详细介绍如何通过浏览器开发者工具(F12)查看和分析这些信息,并附带图片案例帮助你更好地理解。 1. 打开浏览器开发者工具 按下 F12 或右键点击页面选择“检查”可以打开浏览器的开发者工具。常用的浏览器如Chrome、Firefox等都内置了开发者工具。下面是我选择我的一篇文章,打开开发者工具进行演示。 2. 使用

Cursor+Codex隐藏技巧:用截图秒修前端Bug的保姆级教程(React/Chakra UI案例)

Cursor+Codex隐藏技巧:用截图秒修前端Bug的保姆级教程(React/Chakra UI案例) 前端开发中最令人头疼的莫过于那些难以定位的UI问题——元素错位、样式冲突、响应式失效...传统调试方式往往需要反复修改代码、刷新页面、检查元素。现在,通过Cursor编辑器集成的Codex功能,你可以直接用截图交互快速定位和修复这些问题。本文将带你从零开始,掌握这套革命性的调试工作流。 1. 环境准备与基础配置 在开始之前,确保你已经具备以下环境: * Cursor编辑器最新版(v2.5+) * Node.js 18.x及以上版本 * React 18项目(本文以Chakra UI 2.x为例) 首先在Cursor中安装Codex插件: 1. 点击左侧扩展图标 2. 搜索"Codex"并安装 3. 登录你的OpenAI账户(需要ChatGPT Plus订阅) 关键配置项: // 在项目根目录创建.