sphinx在windows环境下的配置及其java版API的使用

下载

官网下载任意解压包

解压

解压拷贝至d:\sphinx,并在d:\sphinx新建data与log文件夹

拷贝配置文件

将d:\sphinx\sphinx.conf.in拷贝至d:\sphinx\bin\sphinx.conf.in,并重命名为sphinx.conf

修改配置文件

1 打开d:\sphinx\bin\sphinx.conf

2 将@CONFDIR@替换成实际目录

3 修改source src1

1
2
3
4
5
6
7
type = mysql   
sql_host = localhost
sql_user = root
sql_pass =
sql_db = test
sql_port = 3306
sql_query_pre = SET NAMES utf-8

4 修改index test1

1
2
3
path = D:/sphinx/data/   
ngram_len = 1
ngram_chars = U+3000..U+2FA1F

建测试数据库

建索引

注意使用管理员权限

1
indexer --all --rotate

安装服务

searchd.exe –install -c d:\sphinx\bin\sphinx.conf –servicename SphinxSearch
config路径务必使用全称
启动服务:net start SphinxSearch
删除服务:sc delete SphinxSearch
打开服务面板:win+R services.msc

打jar包

在java API目录下make,打好jar包

Java API的使用

以我做的一个demo为例,目的是检索出指定关键词的微博,并按情感排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
@Override
public List<Status> queryPositiveTopK(String keyword, int K) {
//参数3指定了排序规则,用到的字段均已在bin/sphinx.conf中的source中配置
return query(keyword, K, "(positive_num - negative_num) * (comment_count+exist_repost_count+1) - (1-is_original)*100000");
}
@Override
public List<Status> queryNegativeTopK(String keyword, int K) {
return query(keyword, K, "(negative_num - positive_num) * (comment_count+exist_repost_count+1) - (1-is_original)*100000");
}
private List<Status> query(String keyword, int K, String sortRule){
List<Status> statuses = new ArrayList<>();
try{
SphinxClient client = new SphinxClient();
client.SetServer("localhost", 9312);
client.SetWeights(new int[] { 100, 1 });
client.SetMatchMode(SphinxClient.SPH_MATCH_PHRASE); //关键词匹配模式
//SetLimits() 参数1:偏移量,参数2:最多查找到多少条记录停止查找,参数3:从查找到的记录取出排序结果的前多少条
client.SetLimits(0, 12000000, K);
client.SetSortMode(SphinxClient.SPH_SORT_EXPR, sortRule); //设置排序方法,这里采用表达式
SphinxResult res = client.Query(keyword, "test1"); //指定关键词以及在sphinx中建立的索引
for (int i = 0; i < res.matches.length; i++) { //res.matches包含了所有查询到的记录
Status status = new Status();
status.setId(res.matches[i].docId); //docId是sphinx中配置的主键
for (int k = 0; k < res.attrNames.length; k++) { //循环中获取查询到的该条记录的所有键值对
String key = res.attrNames[k];
Object value = res.matches[i].attrValues.get(k);
if(key.equals("text")) {
status.setText((String)value);
}else if(key.equals("flag")){
status.setFlag((int)(long)value);
}else if(key.equals("mid")){
status.setMid((Long)value);
}else if(key.equals("uid")){
status.setUid((Long)value);
}else if(key.equals("comment_count")){
status.setComment_count((int)(long)value);
}else if(key.equals("exist_repost_count")){
status.setExist_repost_count((int)(long)value);
}else if(key.equals("positive_num")){
status.setPositive_num((int)(long)value);
}else if(key.equals("negative_num")){
status.setNegative_num((int)(long)value);
}else if(key.equals("repost_path")){
status.setRepost_path((String)value);
}else if(key.equals("@expr")){
System.out.println(key + " = " + value);
}else if(key.equals("is_original")){
status.setIs_original((int)(long)value);
}
}
if(status.getIs_original() == 1){
statuses.add(status);
}
}
}catch(Exception e){
}
return statuses;
}

附上bin/sphinx.conf中的source中配置好的字段
注意:
1 主键字段不需配置(查询后可以获取)
2 其他查询时需要获取的字段需要配置
3 在此配置的字段不参与关键词检索(被忽略)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
sql_attr_bigint     = mid
sql_attr_bigint = uid
sql_attr_bigint = ori_mid
sql_attr_bigint = parent_mid

sql_attr_string = text
sql_attr_string = repost_path

sql_attr_uint = repost_count
sql_attr_uint = comment_count
sql_attr_uint = flag
sql_attr_uint = positive_num
sql_attr_uint = negative_num
sql_attr_uint = exist_repost_count
sql_attr_uint = is_original

sql_attr_float = positive_score
sql_attr_float = negative_score

最后附上demo截图,这个demo是由java swing写成
微博舆情分析系统