From d5d9ec0c9f70c28a6014a3bcda3771323bdefa7c Mon Sep 17 00:00:00 2001 From: Roman Chyla Date: Tue, 28 Dec 2021 18:44:39 -0500 Subject: [PATCH] test: make topn() behave like a top level query incl custom score computation --- .../builders/AqpAdsabsSubQueryProvider.java | 7 +++- .../builders/AqpScoringQueryNodeBuilder.java | 38 ++++++++++--------- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/contrib/adsabs/src/java/org/apache/lucene/queryparser/flexible/aqp/builders/AqpAdsabsSubQueryProvider.java b/contrib/adsabs/src/java/org/apache/lucene/queryparser/flexible/aqp/builders/AqpAdsabsSubQueryProvider.java index dd97515fd..f558bae18 100644 --- a/contrib/adsabs/src/java/org/apache/lucene/queryparser/flexible/aqp/builders/AqpAdsabsSubQueryProvider.java +++ b/contrib/adsabs/src/java/org/apache/lucene/queryparser/flexible/aqp/builders/AqpAdsabsSubQueryProvider.java @@ -601,8 +601,9 @@ public Query parse(FunctionQParser fp) throws SyntaxError { SortSpec sortSpec = SortSpecParsing.parseSortSpec(sortOrRank, fp.getReq()); + Query q; if (sortSpec.getSort() == null) { - return new SecondOrderQuery(innerQuery, + q = new SecondOrderQuery(innerQuery, new SecondOrderCollectorTopN(topN)); } else { @@ -616,9 +617,11 @@ public Query parse(FunctionQParser fp) throws SyntaxError { throw new SyntaxError("I am sorry, you can't use " + sortOrRank + " for topn() sorting. Reason: " + e.getMessage()); } - return new SecondOrderQuery(innerQuery, + q = new SecondOrderQuery(innerQuery, new SecondOrderCollectorTopN(sortOrRank.toString(), topN, sortOrder)); } + + return AqpScoringQueryNodeBuilder.wrapQuery(q, "cite_read_boost", 0.5f); } }); diff --git a/contrib/adsabs/src/java/org/apache/lucene/queryparser/flexible/aqp/builders/AqpScoringQueryNodeBuilder.java b/contrib/adsabs/src/java/org/apache/lucene/queryparser/flexible/aqp/builders/AqpScoringQueryNodeBuilder.java index 8bdd1c8c6..a1e37f7a5 100644 --- a/contrib/adsabs/src/java/org/apache/lucene/queryparser/flexible/aqp/builders/AqpScoringQueryNodeBuilder.java +++ b/contrib/adsabs/src/java/org/apache/lucene/queryparser/flexible/aqp/builders/AqpScoringQueryNodeBuilder.java @@ -23,24 +23,28 @@ * score = lucene_score * ( classic_factor + modifier ) */ public class AqpScoringQueryNodeBuilder implements StandardQueryBuilder { - + public Query build(QueryNode queryNode) throws QueryNodeException { - - AqpAdsabsScoringQueryNode q = (AqpAdsabsScoringQueryNode) queryNode; - - Query query = (Query) queryNode.getChildren().get(0).getTag( - QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID); - - ValueSource vs = new SumFloatFunction(new ValueSource[] { - new FloatFieldSource(q.getSource()), // classic score - new ConstValueSource(q.getModifier()) // modifier - }); - - - FunctionQuery functionQuery = new FunctionQuery(vs); - return new CustomScoreQuery(query, functionQuery); - - } + + AqpAdsabsScoringQueryNode q = (AqpAdsabsScoringQueryNode) queryNode; + + Query query = (Query) queryNode.getChildren().get(0).getTag( + QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID); + + return wrapQuery(query, q.getSource(), q.getModifier()); + + } + + public static Query wrapQuery(Query q, String source, float modifier) { + ValueSource vs = new SumFloatFunction(new ValueSource[] { + new FloatFieldSource(source), // classic score + new ConstValueSource(modifier) // modifier of how much lucene score to use + }); + + + FunctionQuery functionQuery = new FunctionQuery(vs); + return new CustomScoreQuery(q, functionQuery); + } }