Skip to content

Commit f785ff4

Browse files
committed
dedup code by calling wrapWithParentPathConstraint
1 parent 568bf4c commit f785ff4

File tree

2 files changed

+14
-25
lines changed

2 files changed

+14
-25
lines changed

solr/core/src/java/org/apache/solr/search/join/BlockJoinChildQParser.java

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,16 @@
1616
*/
1717
package org.apache.solr.search.join;
1818

19-
import org.apache.lucene.index.Term;
2019
import org.apache.lucene.search.BooleanClause.Occur;
2120
import org.apache.lucene.search.BooleanQuery;
22-
import org.apache.lucene.search.FieldExistsQuery;
2321
import org.apache.lucene.search.MatchAllDocsQuery;
2422
import org.apache.lucene.search.Query;
25-
import org.apache.lucene.search.TermQuery;
2623
import org.apache.lucene.search.join.ToChildBlockJoinQuery;
2724
import org.apache.solr.common.params.SolrParams;
2825
import org.apache.solr.request.SolrQueryRequest;
29-
import org.apache.solr.schema.IndexSchema;
3026
import org.apache.solr.search.SyntaxError;
3127

28+
/** Matches child documents based on parent doc criteria. */
3229
public class BlockJoinChildQParser extends BlockJoinParentQParser {
3330

3431
public BlockJoinChildQParser(
@@ -65,8 +62,8 @@ protected Query noClausesQuery() throws SyntaxError {
6562
* <pre>NEW: q={!child parentPath="/a/b/c"}p_title:dad
6663
*
6764
* OLD: q={!child of=$ff v=$vv}
68-
* ff=(*:* -{prefix f="_nest_path_" v="/a/b/c/"})
69-
* vv=(+p_title:dad +{field f="_nest_path_" v="/a/b/c"})</pre>
65+
* ff=(*:* -{!prefix f="_nest_path_" v="/a/b/c/"})
66+
* vv=(+p_title:dad +{!field f="_nest_path_" v="/a/b/c"})</pre>
7067
*
7168
* <p>For {@code parentPath="/"}:
7269
*
@@ -81,7 +78,7 @@ protected Query noClausesQuery() throws SyntaxError {
8178
*/
8279
@Override
8380
protected Query parseUsingParentPath(String parentPath) throws SyntaxError {
84-
// allParents filter: (*:* -{prefix f="_nest_path_" v="<parentPath>/"})
81+
// allParents filter: (*:* -{!prefix f="_nest_path_" v="<parentPath>/"})
8582
// For root: (*:* -_nest_path_:*)
8683
final Query allParentsFilter = buildAllParentsFilterFromPath(parentPath);
8784

@@ -98,18 +95,9 @@ protected Query parseUsingParentPath(String parentPath) throws SyntaxError {
9895
}
9996

10097
// constrain the parent query to only match docs at exactly parentPath
101-
// (+<original_parent> +{field f="_nest_path_" v="<parentPath>"})
98+
// (+<original_parent> +{!field f="_nest_path_" v="<parentPath>"})
10299
// For root: (+<original_parent> -_nest_path_:*)
103-
final BooleanQuery.Builder constrainedBuilder =
104-
new BooleanQuery.Builder().add(parsedParentQuery, Occur.MUST);
105-
if (parentPath.equals("/")) {
106-
constrainedBuilder.add(
107-
new FieldExistsQuery(IndexSchema.NEST_PATH_FIELD_NAME), Occur.MUST_NOT);
108-
} else {
109-
constrainedBuilder.add(
110-
new TermQuery(new Term(IndexSchema.NEST_PATH_FIELD_NAME, parentPath)), Occur.FILTER);
111-
}
112-
final Query constrainedParentQuery = constrainedBuilder.build();
100+
Query constrainedParentQuery = wrapWithParentPathConstraint(parentPath, parsedParentQuery);
113101

114102
return createQuery(allParentsFilter, constrainedParentQuery, null);
115103
}

solr/core/src/java/org/apache/solr/search/join/BlockJoinParentQParser.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.apache.solr.search.SyntaxError;
5252
import org.apache.solr.util.SolrDefaultScorerSupplier;
5353

54+
/** Matches parent documents based on child doc criteria. */
5455
public class BlockJoinParentQParser extends FiltersQParser {
5556
/** implementation detail subject to change */
5657
public static final String CACHE_NAME = "perSegFilter";
@@ -114,8 +115,8 @@ public Query parse() throws SyntaxError {
114115
* <pre>NEW: q={!parent parentPath="/a/b/c"}c_title:son
115116
*
116117
* OLD: q=(+{!field f="_nest_path_" v="/a/b/c"} +{!parent which=$ff v=$vv})
117-
* ff=(*:* -{prefix f="_nest_path_" v="/a/b/c/"})
118-
* vv=(+c_title:son +{prefix f="_nest_path_" v="/a/b/c/"})</pre>
118+
* ff=(*:* -{!prefix f="_nest_path_" v="/a/b/c/"})
119+
* vv=(+c_title:son +{!prefix f="_nest_path_" v="/a/b/c/"})</pre>
119120
*
120121
* <p>For {@code parentPath="/"}:
121122
*
@@ -129,7 +130,7 @@ public Query parse() throws SyntaxError {
129130
* root "/")
130131
*/
131132
protected Query parseUsingParentPath(String parentPath) throws SyntaxError {
132-
// allParents filter: (*:* -{prefix f="_nest_path_" v="<parentPath>/"})
133+
// allParents filter: (*:* -{!prefix f="_nest_path_" v="<parentPath>/"})
133134
// For root: (*:* -_nest_path_:*)
134135
final Query allParentsFilter = buildAllParentsFilterFromPath(parentPath);
135136

@@ -140,15 +141,15 @@ protected Query parseUsingParentPath(String parentPath) throws SyntaxError {
140141
return wrapWithParentPathConstraint(parentPath, allParentsFilter);
141142
}
142143

143-
// constrain child query: (+<original_child> +{prefix f="_nest_path_" v="<parentPath>/"})
144+
// constrain child query: (+<original_child> +{!prefix f="_nest_path_" v="<parentPath>/"})
144145
// For root: (+<original_child> +_nest_path_:*)
145146
final Query constrainedChildQuery =
146147
buildChildQueryWithPathConstraint(parentPath, parsedChildQuery);
147148

148149
final String scoreMode = localParams.get("score", ScoreMode.None.name());
149150
final Query parentJoinQuery = createQuery(allParentsFilter, constrainedChildQuery, scoreMode);
150151

151-
// wrap result: (+<parent_join> +{field f="_nest_path_" v="<parentPath>"})
152+
// wrap result: (+<parent_join> +{!field f="_nest_path_" v="<parentPath>"})
152153
// For root: (+<parent_join> -_nest_path_:*)
153154
return wrapWithParentPathConstraint(parentPath, parentJoinQuery);
154155
}
@@ -165,7 +166,7 @@ protected Query parseUsingParentPath(String parentPath) throws SyntaxError {
165166
* /a/b/c})
166167
* </ul>
167168
*
168-
* <p>Equivalent to: {@code (*:* -{prefix f="_nest_path_" v="<parentPath>/"})} For root ({@code
169+
* <p>Equivalent to: {@code (*:* -{!prefix f="_nest_path_" v="<parentPath>/"})} For root ({@code
169170
* /}): {@code (*:* -_nest_path_:*)}
170171
*/
171172
protected static Query buildAllParentsFilterFromPath(String parentPath) {
@@ -186,7 +187,7 @@ protected static Query buildAllParentsFilterFromPath(String parentPath) {
186187
* level are matched. For root, this excludes docs that have a {@code _nest_path_} value. For
187188
* non-root, this requires an exact match on {@code _nest_path_}.
188189
*/
189-
private static Query wrapWithParentPathConstraint(String parentPath, Query query) {
190+
protected static Query wrapWithParentPathConstraint(String parentPath, Query query) {
190191
final BooleanQuery.Builder builder = new BooleanQuery.Builder().add(query, Occur.MUST);
191192
if (parentPath.equals("/")) {
192193
builder.add(new FieldExistsQuery(IndexSchema.NEST_PATH_FIELD_NAME), Occur.MUST_NOT);

0 commit comments

Comments
 (0)