solrj 查询SolrServer的一般流程
2012-08-17 15:15
176 查看
程序举例:
测试结果:
参考:http://lucidworks.lucidimagination.com/display/solr/Common+Query+Parameters
package cn.wzb;
/**
* @author wang.zb
*/
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
public class TestQuery {
public static void main(String[] args) {
//实例化SolrServer,以获取与solrServer的通信
SolrServer solrServer = new HttpSolrServer("http://localhost:8983/solr");
//创建查询参数以及设定的查询参数
SolrQuery params = new SolrQuery();
//the common parameters for all search
params.set("q", "*:*");
params.set("fq", "age:[20 TO 30]", "grade:[70 TO *]"); // filter query
params.set("fl", "*,score"); // field list
params.set("sort", "grade desc" ); //default score desc.
params.set("start", "0");
params.set("rows", "10");
params.set("timeAllowed", "30000"); //miliseconds
//params.set("wt", "xml"); // the response writer type
params.set("omitHeader", "true"); //default false
params.set("cache", "false"); //default true
//查询并获取相应的结果!
QueryResponse response = null;
try {
response = solrServer.query(params);
} catch (SolrServerException e) {
System.err.println(e.getMessage());
e.printStackTrace();
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
} finally {
solrServer.shutdown();
}
//获取相关的查询结果
if(response != null ){
System.out.println("查询耗时(ms):" + response.getQTime());
System.out.println(response.toString());
}
//关闭SolrServer连接实例,释放资源
solrServer.shutdown();
}
}
package cn.wzb; /** * @author wang.zb */ import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrServer; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.impl.HttpSolrServer; import org.apache.solr.client.solrj.response.QueryResponse; public class TestQuery { public static void main(String[] args) { //实例化SolrServer,以获取与solrServer的通信 SolrServer solrServer = new HttpSolrServer("http://localhost:8983/solr"); //创建查询参数以及设定的查询参数 SolrQuery params = new SolrQuery(); params.set("q", "*:*"); //查询并获取相应的结果! QueryResponse response = null; try { response = solrServer.query(params); } catch (SolrServerException e) { System.err.println(e.getMessage()); e.printStackTrace(); } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); } finally { solrServer.shutdown(); } //获取相关的查询结果 if(response != null ){ System.out.println("查询耗时(ms):" + response.getQTime()); System.out.println(response.toString()); } //关闭SolrServer连接实例,释放资源 solrServer.shutdown(); } }
测试结果:
2012-8-17 15:20:26 org.apache.solr.client.solrj.impl.HttpClientUtil createClient 信息: Creating new http client, config:maxConnections=128&maxConnectionsPerHost=32&followRedirects=false 查询耗时(ms):0 {responseHeader={status=0,QTime=0,params={wt=javabin,q=*:*,version=2}},response={numFound=5,start=0,docs=[SolrDocument{id=no1, number=1, name=张三, grade=64.8, age=20, introduction=fooaaa, 他是一个好学生,学习优秀, 是一个中国人, text_auto=fooaaa, 他是一个好学生,学习优秀, 是一个中国人, text=fooaaa, 他是一个好学生,学习优秀, 是一个中国人, title=fooaaa, 他是一个好学生,学习优秀, 是一个中国人, title_smart=fooaaa, 他是一个好学生,学习优秀, 是一个中国人, entranceTime=Tue Jan 08 00:08:00 CST 2008, deposit=7000.0009}, SolrDocument{id=no2, number=2, name=李四, grade=54.8, age=25, introduction=baraaa,他不是一个好学生,不爱学习,是一个美国人, text_auto=baraaa,他不是一个好学生,不爱学习,是一个美国人, text=baraaa,他不是一个好学生,不爱学习,是一个美国人, title=baraaa,他不是一个好学生,不爱学习,是一个美国人, title_smart=baraaa,他不是一个好学生,不爱学习,是一个美国人, entranceTime=Sun Jan 07 00:07:00 CST 2007, deposit=9000.0009}, SolrDocument{id=no3, number=3, name=王五, grade=94.8, age=25, introduction=bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人, text_auto=bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人, text=bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人, title=bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人, title_smart=bazaaa,bbbfoo 他是一个好学生,学习优秀,是一个法国人, entranceTime=Wed Jan 05 00:05:00 CST 2005, deposit=3000.0009}, SolrDocument{id=no4, number=4, name=沈六, grade=80.8, age=30, introduction=bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabar, text_auto=bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabar, text=bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabar, title=bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabar, title_smart=bbbfoo 他已经毕业了,现在开始工作了, 德国人,aaabar, entranceTime=Tue Jan 10 00:09:00 CST 2006, deposit=90000.000788}, SolrDocument{id=no5, number=4, name=江八, grade=70.8, age=25, introduction=bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo, text_auto=bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo, text=bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo, title=bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo, title_smart=bbbbar 他毕业了,现在开始工作了,但是仍在认真学习,日本人 ,aaafoo, entranceTime=Wed Jan 10 00:09:00 CST 2007, deposit=2343900.000788}]}}
参考:http://lucidworks.lucidimagination.com/display/solr/Common+Query+Parameters
package cn.wzb;
/**
* @author wang.zb
*/
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
public class TestQuery {
public static void main(String[] args) {
//实例化SolrServer,以获取与solrServer的通信
SolrServer solrServer = new HttpSolrServer("http://localhost:8983/solr");
//创建查询参数以及设定的查询参数
SolrQuery params = new SolrQuery();
//the common parameters for all search
params.set("q", "*:*");
params.set("fq", "age:[20 TO 30]", "grade:[70 TO *]"); // filter query
params.set("fl", "*,score"); // field list
params.set("sort", "grade desc" ); //default score desc.
params.set("start", "0");
params.set("rows", "10");
params.set("timeAllowed", "30000"); //miliseconds
//params.set("wt", "xml"); // the response writer type
params.set("omitHeader", "true"); //default false
params.set("cache", "false"); //default true
//查询并获取相应的结果!
QueryResponse response = null;
try {
response = solrServer.query(params);
} catch (SolrServerException e) {
System.err.println(e.getMessage());
e.printStackTrace();
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
} finally {
solrServer.shutdown();
}
//获取相关的查询结果
if(response != null ){
System.out.println("查询耗时(ms):" + response.getQTime());
System.out.println(response.toString());
}
//关闭SolrServer连接实例,释放资源
solrServer.shutdown();
}
}
相关文章推荐
- solrj操纵CloudSolrServer的一般步骤
- Solrj对SolrServer进行Term查询:(利用prefix 也可以实现auto-complete)
- SolrJ 操作HttpSolrServer, ConcurrentUpdateSolrServer和CloudSolrServer
- SolrCloud中一台server宕机情况下,依然能够正常查询
- solr与solrj6.0查询总结
- Apache Solrj EmbeddedSolrServer使用
- org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException: Document contains multiple val
- Org.apache.solr.client.solrj.impl.CloudSolrServer$RouteException:DEF_CONTENT_CHARST 错误解决
- solr异常:org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException: Expected mime type appl
- Microsoft Dynamics CRM server 2013 一般销售流程之 订单完成 转财务应收款 发票功能
- Apache Solrj EmbeddedSolrServer使用
- solr facet查询及solrj 读取facet数据(相当有用)
- Solr Server对外提供Web Service的沟通方式 使用SolrJ跟Solr Server通信
- solr配置与solrj EmbeddedSolrServer使用篇
- solr facet查询及solrj 读取facet数据(相当有用)
- Noqualifying bean of type [org.apache.solr.client.solrj.SolrServer] is defined:expected single match
- Solr客户端 SolrJ单机 添加查询操作
- Solr笔记四之Solrj创建索引和搜索的一般步骤
- solr实例代码 import org.apache.solr.client.solrj.SolrServer
- solr facet查询及solrj 读取facet数据