查询solr

时间:2011-09-09 07:28:46

标签: solr facet solrj

我从网址

获取以下查询
public static String query="pen&mq=pen&f=owners%5B%22abc%22%5D&f=application_type%5B%22cde%22%5D";

我想使用此查询查询solr

 CommonsHttpSolrServer server = new CommonsHttpSolrServer( "http://localhost:8080/solr/");          
server.setParser(new XMLResponseParser());          
SolrQuery solrQuery = new SolrQuery();
solrQuery.setQuery(query); 
 QueryResponse qr =server.query(solrQuery);
 SolrDocumentList sdl = qr.getResults();

System.out.println("Found: " + sdl.getNumFound());

我在tomcat日志文件中检查了我的查询,它被翻译为

path=/select params={version=2&wt=javabin&q=pen%26mq%3Dpen%26f%3Downers%255B%2522abc%2522%255D%26f%3Dapplication_type%255B%2522cde%2522%255D`

尽管有500个文档,但它给了我0个结果。 我尝试使用

解码我的查询
URLDecoder.decode(query,"UTF-8");

然后它开始抱怨“[”迹象。

org.apache.lucene.queryParser.ParseException: Encountered " "]" "] 

我应该如何使用此查询查询solr服务器? 是否有必要解析查询并获取每个过滤器的值,然后使用

 solrQuery.setFilterQueries()

方法。有人可以帮助我吗

1 个答案:

答案 0 :(得分:3)

如果根据tomcat日志比较从手动调用中的url到Solr的内容以及作为参数传递给Solrj的solr调用的内容,它们不匹配。

看起来您在调用中传递了已编码的url参数:solrQuery.setQuery(query)然后Solrj正在对已编码的参数应用编码。您可以看到pen&mq=pen被转为q=pen%26mq%3Dpen

的地方

基于Solrj Wiki Page我建议使用query.setParam选项设置所有查询参数。所以你需要做以下事情:

SolrQuery solrQuery = new SolrQuery();
solrQuery.setQuery("pen");
solrQuery.setParam("mq","pen");
solrQuery.setParam("f","owners[\"abc\"]");
solrQuery.setParam("f","application_type[\"cde"\]");
QueryResponse qr = server.query(solrQuery);
SolrDocumentList sdl = qr.getResults();

System.out.println("Found: " + sdl.getNumFound());

更新:添加了对setParams的完整转换。