我正在使用solrj从solr安装执行查询搜索。
这就是我所拥有的:
public List<Details> search(String query, int first, int rows) {
List<Details> cds = new ArrayList<Details>();
try {
SolrQuery q = new SolrQuery();
q.set("q", "name:" + query); //this throws an error on the solr side!
q.setFields("id");
q.setRows(rows);
q.setStart(first);
QueryResponse r = server.query(q);
final SolrDocumentList docList = r.getResults();
for (SolrDocument doc : docList) {
String id_ = doc.getFieldValue("id").toString();
Long id = Long.parseLong(id_);
Details cd = detailsFacade.find(id);
cds.add(cd);
}
} catch (SolrServerException ex) {
Logger.getLogger(SolrIndexer.class.getName()).log(Level.SEVERE, null, ex);
}
return cds;
}
public int count(String query) {
try {
SolrQuery q = new SolrQuery();
q.set("q", "name:" + query); //this throws an error on the solr side
q.setRows(0); // don't actually request any data
return (int) server.query(q).getResults().getNumFound();
} catch (SolrServerException ex) {
Logger.getLogger(SolrIndexer.class.getName()).log(Level.SEVERE, null, ex);
}
return 0;
}
我只想对名称字段进行查询。 (我有几个字段包含索引文档的数据)。
solr端的错误:
1126326 [qtp1645986986-17] ERROR org.apache.solr.core.SolrCore – org.apache.solr.common.SolrException: org.apache.solr.search.SyntaxError: Cannot parse 'name:': Encountered "<EOF>" at line 1, column 5.
Was expecting one of:
<BAREOPER> ...
"(" ...
"*" ...
<QUOTED> ...
<TERM> ...
<PREFIXTERM> ...
<WILDTERM> ...
<REGEXPTERM> ...
"[" ...
"{" ...
<LPARAMS> ...
<NUMBER> ...
at org.apache.solr.handler.component.QueryComponent.prepare(QueryComponent.java:171)
at org.apache.solr.handler.component.SearchHandler.handleRequestBody(SearchHandler.java:187)
at org.apache.solr.handler.RequestHandlerBase.handleRequest(RequestHandlerBase.java:135)
at org.apache.solr.core.SolrCore.execute(SolrCore.java:1859)
at org.apache.solr.servlet.SolrDispatchFilter.execute(SolrDispatchFilter.java:721)
at org.apache.solr.servlet.SolrDispatchFilter.doFilter(SolrDispatchFilter.java:417)
at org.apache.solr.servlet.SolrDispatchFilter.doFilter(SolrDispatchFilter.java:201)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1419)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:455)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:557)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1075)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:384)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1009)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:255)
at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:154)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
at org.eclipse.jetty.server.Server.handle(Server.java:368)
at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:489)
at org.eclipse.jetty.server.BlockingHttpConnection.handleRequest(BlockingHttpConnection.java:53)
at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:942)
at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:1004)
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:640)
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)
at org.eclipse.jetty.server.BlockingHttpConnection.handle(BlockingHttpConnection.java:72)
at org.eclipse.jetty.server.bio.SocketConnector$ConnectorEndPoint.run(SocketConnector.java:264)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
at java.lang.Thread.run(Thread.java:724)
Caused by: org.apache.solr.search.SyntaxError: Cannot parse 'name:': Encountered "<EOF>" at line 1, column 5.
Was expecting one of:
<BAREOPER> ...
"(" ...
"*" ...
<QUOTED> ...
<TERM> ...
<PREFIXTERM> ...
<WILDTERM> ...
<REGEXPTERM> ...
"[" ...
"{" ...
<LPARAMS> ...
<NUMBER> ...
at org.apache.solr.parser.SolrQueryParserBase.parse(SolrQueryParserBase.java:157)
at org.apache.solr.search.LuceneQParser.parse(LuceneQParser.java:50)
at org.apache.solr.search.QParser.getQuery(QParser.java:142)
at org.apache.solr.handler.component.QueryComponent.prepare(QueryComponent.java:142)
... 31 more
Caused by: org.apache.solr.parser.ParseException: Encountered "<EOF>" at line 1, column 5.
Was expecting one of:
<BAREOPER> ...
"(" ...
"*" ...
<QUOTED> ...
<TERM> ...
<PREFIXTERM> ...
<WILDTERM> ...
<REGEXPTERM> ...
"[" ...
"{" ...
<LPARAMS> ...
<NUMBER> ...
at org.apache.solr.parser.QueryParser.generateParseException(QueryParser.java:651)
at org.apache.solr.parser.QueryParser.jj_consume_token(QueryParser.java:533)
at org.apache.solr.parser.QueryParser.Clause(QueryParser.java:217)
at org.apache.solr.parser.QueryParser.Query(QueryParser.java:108)
at org.apache.solr.parser.QueryParser.TopLevelQuery(QueryParser.java:97)
at org.apache.solr.parser.SolrQueryParserBase.parse(SolrQueryParserBase.java:153)
... 34 more
我怎样才能实现这个目标?
由于
答案 0 :(得分:2)
错误在行
q.set("q", "name:" + query); //this throws an error on the solr side
您必须删除此行,然后直接调用
q.setQuery(query);
这假设指定的查询格式正确,如描述的on the solr query syntax wiki page。例如,在您的情况下,查询必须类似于:
name:"Jackson"
如果要以编程方式构建查询,可以使用set(name,values ...)方法:
q.set("name", "Jackson");
答案 1 :(得分:0)
您可以使用SolrRequestParsers.parseQueryString(string)
,如下所示:
MultiMapSolrParams solrParams = SolrRequestParsers.parseQueryString("q=name:abc");
QueryResponse r = server.query(solrParams, SolrRequest.METHOD.POST);
希望这会对你有所帮助。