2014-03-26 87 views
1

我正在使用solrj從solr安裝執行查詢搜索。solrj在特定字段上搜索

這是我有:

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

您可以使用SolrRequestParsers.parseQueryString(string),如下圖所示:

MultiMapSolrParams solrParams = SolrRequestParsers.parseQueryString("q=name:abc"); 
QueryResponse r = server.query(solrParams, SolrRequest.METHOD.POST); 

希望這有助於你。

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" 

集(名稱,值...)方法可以,如果你想以編程方式建立這樣的查詢使用:

q.set("name", "Jackson"); 
+0

我會去'q.set(「name」,「Jackson」);'。另外考慮使用'org.apache.solr.client.solrj.util.ClientUtils#escapeQueryChars'來轉義用戶可能給予查詢的特殊字符。 – cheffe