使用 solr 中的函数进行筛选和排序

2022-09-03 03:00:07

我有这样的文档:

{attr_a: 1}, {attr_a: 2, changes_user1_a: 3}

第二个文档说 user1 所做的更改尚未“提交”,对其他用户不可见。

这些字段是动态的:

<dynamicField name="changes_*" type="string" indexed="true" stored="true"/>
<dynamicField name="attr_*" type="string" indexed="true" stored="true"/>

问题是,user1需要进行过滤,分页和排序,同时考虑到他的新值,而其他用户需要在旧值上执行此操作。

我正在考虑通过以下方式解决此问题:如果user1想要按attr_a进行过滤和排序,我会发出如下查询:

fq=def(changes_user1_a, attr_a): 'somefilter'&sort=def(changes_user1_a, attr_a) asc

但是,此方法会为 fq 生成以下错误:

"error":{
"metadata":[
  "error-class","org.apache.solr.common.SolrException",
  "root-error-class","org.apache.solr.parser.ParseException"],
"msg":"org.apache.solr.search.SyntaxError: Cannot parse 'def(changes_user1_a, attr_a): 3': Encountered \" \":\" \": \"\" at line 1, column 28.\r\nWas expecting one of:\r\n    <EOF> \r\n    <AND> ...\r\n    <OR> ...\r\n    <NOT> ...\r\n    \"+\" ...\r\n    \"-\" ...\r\n    <BAREOPER> ...\r\n    \"(\" ...\r\n    \"*\" ...\r\n    \"^\" ...\r\n    <QUOTED> ...\r\n    <TERM> ...\r\n    <PREFIXTERM> ...\r\n    <WILDTERM> ...\r\n    <REGEXPTERM> ...\r\n    \"[\" ...\r\n    \"{\" ...\r\n    <LPARAMS> ...\r\n    \"filter(\" ...\r\n    <NUMBER> ...\r\n    ",
"code":400}

当仅排序时,这个:

error":{
"trace":"java.lang.UnsupportedOperationException\r\n\tat org.apache.lucene.queries.function.FunctionValues.doubleVal(FunctionValues.java:47)\r\n\tat org.apache.lucene.queries.function.ValueSource$ValueSourceComparator.copy(ValueSource.java:153)\r\n\tat org.apache.lucene.search.TopFieldCollector$SimpleFieldCollector$1.collect(TopFieldCollector.java:206)\r\n\tat org.apache.lucene.search.MatchAllDocsQuery$1$1.score(MatchAllDocsQuery.java:56)\r\n\tat org.apache.lucene.search.BulkScorer.score(BulkScorer.java:39)\r\n\tat org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:669)\r\n\tat org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:473)\r\n\tat org.apache.solr.search.SolrIndexSearcher.buildAndRunCollectorChain(SolrIndexSearcher.java:260)\r\n\tat org.apache.solr.search.SolrIndexSearcher.getDocListNC(SolrIndexSearcher.java:1810)\r\n\tat org.apache.solr.search.SolrIndexSearcher.getDocListC(SolrIndexSearcher.java:1627)\r\n\tat org.apache.solr.search.SolrIndexSearcher.search(SolrIndexSearcher.java:643)\r\n\tat org.apache.solr.handler.component.QueryComponent.process(QueryComponent.java:529)\r\n\tat org.apache.solr.handler.component.SearchHandler.handleRequestBody(SearchHandler.java:293)\r\n\tat org.apache.solr.handler.RequestHandlerBase.handleRequest(RequestHandlerBase.java:156)\r\n\tat org.apache.solr.core.SolrCore.execute(SolrCore.java:2036)\r\n\tat org.apache.solr.servlet.HttpSolrCall.execute(HttpSolrCall.java:657)\r\n\tat org.apache.solr.servlet.HttpSolrCall.call(HttpSolrCall.java:464)\r\n\tat org.apache.solr.servlet.SolrDispatchFilter.doFilter(SolrDispatchFilter.java:257)\r\n\tat org.apache.solr.servlet.SolrDispatchFilter.doFilter(SolrDispatchFilter.java:208)\r\n\tat org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1668)\r\n\tat org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:581)\r\n\tat org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)\r\n\tat org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548)\r\n\tat org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:226)\r\n\tat org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1160)\r\n\tat org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:511)\r\n\tat org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)\r\n\tat org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1092)\r\n\tat org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)\r\n\tat org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:213)\r\n\tat org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:119)\r\n\tat org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:134)\r\n\tat org.eclipse.jetty.server.Server.handle(Server.java:518)\r\n\tat org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:308)\r\n\tat org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:244)\r\n\tat org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:273)\r\n\tat org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95)\r\n\tat org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:93)\r\n\tat org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceAndRun(ExecuteProduceConsume.java:246)\r\n\tat org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:156)\r\n\tat org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:654)\r\n\tat org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:572)\r\n\tat java.lang.Thread.run(Thread.java:745)\r\n",
"code":500}

虽然像我预期的那样工作。fl=test:def(changes_user1_a, attr_a)

所以我的问题是:

  1. 我是否采取了正确的方法来解决这个问题?
  2. 如果是,为什么我不能使用 def 函数进行排序和筛选?

我使用的是 Solr 版本 6.1.0

谢谢


答案 1

对于具有函数的筛选器,您必须使用解析器。例如,请参阅:solr:使用函数创建过滤器查询frange

例如,以下查询将返回所有具有 的文档,或者如果字段不存在。changes_user1_a=3attr_a=3changes_user1_a

fq={!frange u=3 l=3}def(changes_user1_a, attr_a)

您的排序命令对我来说似乎没问题,但它显然不适用于字符串字段。我可以重现您的异常。当两个字段都是数字时,它就会炒作。也许是solr中的错误?

 null:java.lang.UnsupportedOperationException
    at org.apache.lucene.queries.function.FunctionValues.doubleVal(FunctionValues.java:47)
    at org.apache.lucene.queries.function.valuesource.DefFunction$1.doubleVal(DefFunction.java:88)
    at org.apache.lucene.queries.function.ValueSource$ValueSourceComparator.copy(ValueSource.java:264)
    at org.apache.lucene.search.TopFieldCollector$SimpleFieldCollector$1.collect(TopFieldCollector.java:141)
    at org.apache.lucene.search.Weight$DefaultBulkScorer.scoreAll(Weight.java:246)
    at org.apache.lucene.search.Weight$DefaultBulkScorer.score(Weight.java:197)
    at org.apache.lucene.search.BulkScorer.score(BulkScorer.java:39)
    at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:669)
    at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:473)
    at org.apache.solr.search.SolrIndexSearcher.buildAndRunCollectorChain(SolrIndexSearcher.java:225)
    at org.apache.solr.search.SolrIndexSearcher.getDocListNC(SolrIndexSearcher.java:2027)
    at org.apache.solr.search.SolrIndexSearcher.getDocListC(SolrIndexSearcher.java:1844)
    at org.apache.solr.search.SolrIndexSearcher.search(SolrIndexSearcher.java:609)
 ...

另请参阅:


答案 2

推荐