文章目錄

在Sphinx中,如果你调用的是C的api,并使用更新属性的功能,而此时,你想将更新后的索引冲刷到硬盘,你就会发现C的api中没有提供这个功能。而在Java,PHP,Python中,都提供了FlushAttributes这个接口来完成这个功能,于是你不得不另外在写一个程序来调用这个接口。

仔细想想,Sphinx都是用C++写的,而C的API中竟然没有提供这个接口,反倒是其它语言有提供,真是匪夷所思。所幸,代码都是开源的,想要自己有这个接口,自己动手写一个就好了,也许这就是开源的好处。

代码如下:

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
int sphinx_flush_attributes(sphinx_client * client) {
char *buf, *req, *p;
int req_len = 0;
if (!client) {
printf("not valid client\n");
return -1;
}
buf = malloc ( 12 + req_len ); // request body length plus 12 header bytes
if ( !buf ) {
set_error ( client, "malloc() failed (bytes=%d)", req_len );
return -1;
}

req = buf;

send_word ( &req, SEARCHD_COMMAND_FLUSHATTRS );
send_word ( &req, VER_COMMAND_FLUSHATTRS );
send_int ( &req, req_len );

// send query, get response
if ( !net_simple_query ( client, buf, req_len ) )
return -1;

// parse response
if ( client->response_len < 4 ) {
set_error ( client, "incomplete reply" );
return -1;
}

p = client->response_start;
return unpack_int ( &p );
}

之后还要添加
SEARCHD_COMMAND_FLUSHATTRS = 7,
VER_COMMAND_FLUSHATTRS = 0x100,
以及在头文件中添加int sphinx_flush_attributes(sphinx_client * client);即可。

打赏作者

文章目錄