如果客户端和服务器版本不一致可能会出现如下错误DEPRECATED: Do not call this method or, even better, use SphinxQL instead of an API
Error: searchd error: client version is higher than daemon version (client is v.1.25, daemon is v.1.22)
找了半天 sphinx java api 原来在官方源代码包里就有
1.首先下载官方源码包 https://github.com/sphinxsearch/sphinx (如果服务器已经安装了,请到服务器的对应目录编译 jar 包,否则会出现上面的错误)
2.然后到./api/java目录下执行make即可得到sphinxapi.jar文件(注意需要在 unix 系统环境,查看 sphinx 官方说明文档)
3.使用 Intellij 的插件来导入本地 jar 到 maven 库 http://mengkang.net/437.html/Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home/bin/java "-Dmaven.home=/Applications/IntelliJ IDEA 14.app/Contents/plugins/maven/lib/maven3" "-Dclassworlds.conf=/Applications/IntelliJ IDEA 14.app/Contents/plugins/maven/lib/maven3/bin/m2.conf" -Didea.launcher.port=7535 "-Didea.launcher.bin.path=/Applications/IntelliJ IDEA 14.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Applications/IntelliJ IDEA 14.app/Contents/plugins/maven/lib/maven3/boot/plexus-classworlds-2.4.jar:/Applications/IntelliJ IDEA 14.app/Contents/lib/idea_rt.jar" com.intellij.rt.execution.application.AppMain org.codehaus.classworlds.Launcher -Didea.version=14.1 -DskipTests=true org.apache.maven.plugins:maven-install-plugin:2.3.1:install-file \
-Dfile=/Users/zhoumengkang/Downloads/sphinx-master/api/java/sphinxapi.jar \
-DgroupId=com.sphinxsearch \
-DartifactId=sphinxsearch \
-Dversion=1.22 \
-Dpackaging=jar \
-DgeneratePom=true
4.到自己的pom.xml里面添加刚刚配置的参数
com.sphinxsearch
sphinxsearch
1.22
至此就导入完毕就可以使用了sphinx了。
根据官方的test.java写了一个简单的搜索,还是很方便的呢。public static IdPages sphinxSearch(int type, String query, int offset, int limit) throws SphinxException {
int totalNum = 0;
List ids = new ArrayList<>();
//========================= sphinx 配置 ========================
String host = TopitConfig.getString("sphinx.host");
int port;
if (type == SearchUser) {
port = TopitConfig.getint("sphinx.user.port");
}else if(type == SearchFeed){
port = TopitConfig.getint("sphinx.feed.port");
}else{
return null;
}
int mode = SphinxClient.SPH_MATCH_ALL;
String index = "*";
int sortMode = SphinxClient.SPH_SORT_RELEVANCE;
String sortClause = "";
SphinxClient cl = new SphinxClient();
cl.SetServer(host, port);
cl.SetWeights(new int[]{100, 1 });
cl.SetMatchMode(mode);
cl.SetLimits(offset, limit);
cl.SetSortMode(sortMode, sortClause);
//========================= sphinx 配置 ========================
StringBuffer q = new StringBuffer();
q.append(query);
SphinxResult res = cl.Query(q.toString(), index);
if ( res == null ) {
logger.error("Error: " + cl.GetLastError());
return null;
}
if ( cl.GetLastWarning() != null && cl.GetLastWarning().length() > 0 ) {
logger.error("WARNING: " + cl.GetLastWarning() + "\n");
}
totalNum = res.totalFound;
for ( int i=0; i
{
SphinxMatch info = res.matches[i];
ids.add((int) info.docId);
}
return new IdPages(totalNum,ids);
}