gpt4 book ai didi

java - solrj 在特定字段上搜索

转载 作者:行者123 更新时间:2023-11-30 03:57:53 28 4
gpt4 key购买 nike

我正在使用 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

我怎样才能实现这个目标?

谢谢

最佳答案

错误已上线

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, value...) 方法,如下所示:

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

关于java - solrj 在特定字段上搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22657096/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com