You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by GitBox <gi...@apache.org> on 2022/12/16 18:28:31 UTC

[GitHub] [lucene-solr] kwatters commented on a diff in pull request #976: SOLR-13749: Implement support for joining across collections with multiple shards

kwatters commented on code in PR #976:
URL: https://github.com/apache/lucene-solr/pull/976#discussion_r1051039032


##########
solr/core/src/java/org/apache/solr/search/join/XCJFQuery.java:
##########
@@ -0,0 +1,380 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.solr.search.join;
+
+import java.io.IOException;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.PostingsEnum;
+import org.apache.lucene.index.Terms;
+import org.apache.lucene.index.TermsEnum;
+import org.apache.lucene.search.ConstantScoreScorer;
+import org.apache.lucene.search.ConstantScoreWeight;
+import org.apache.lucene.search.DocIdSet;
+import org.apache.lucene.search.DocIdSetIterator;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.QueryVisitor;
+import org.apache.lucene.search.ScoreMode;
+import org.apache.lucene.search.Scorer;
+import org.apache.lucene.search.Weight;
+import org.apache.lucene.util.BytesRefBuilder;
+import org.apache.lucene.util.FixedBitSet;
+import org.apache.solr.client.solrj.io.SolrClientCache;
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.eq.FieldEqualitor;
+import org.apache.solr.client.solrj.io.stream.CloudSolrStream;
+import org.apache.solr.client.solrj.io.stream.SolrStream;
+import org.apache.solr.client.solrj.io.stream.StreamContext;
+import org.apache.solr.client.solrj.io.stream.TupleStream;
+import org.apache.solr.client.solrj.io.stream.UniqueStream;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionNamedParameter;
+import org.apache.solr.cloud.CloudDescriptor;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.cloud.ClusterState;
+import org.apache.solr.common.cloud.DocRouter;
+import org.apache.solr.common.cloud.Slice;
+import org.apache.solr.common.params.CommonParams;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.common.params.SolrParams;
+import org.apache.solr.schema.FieldType;
+import org.apache.solr.search.BitDocSet;
+import org.apache.solr.search.DocSet;
+import org.apache.solr.search.DocSetUtil;
+import org.apache.solr.search.Filter;
+import org.apache.solr.search.SolrIndexSearcher;
+
+public class XCJFQuery extends Query {
+
+  protected final String query;
+  protected final String zkHost;
+  protected final String solrUrl;
+  protected final String collection;
+  protected final String fromField;
+  protected final String toField;
+  protected final boolean routedByJoinKey;
+
+  protected final long timestamp;
+  protected final int ttl;
+
+  protected SolrParams otherParams;
+  protected String otherParamsString;
+
+  public XCJFQuery(String query, String zkHost, String solrUrl, String collection, String fromField, String toField,
+                   boolean routedByJoinKey, int ttl, SolrParams otherParams) {
+
+    this.query = query;
+    this.zkHost = zkHost;
+    this.solrUrl = solrUrl;
+    this.collection = collection;
+    this.fromField = fromField;
+    this.toField = toField;
+    this.routedByJoinKey = routedByJoinKey;
+
+    this.timestamp = System.nanoTime();
+    this.ttl = ttl;
+
+    this.otherParams = otherParams;
+    // SolrParams doesn't implement equals(), so use this string to compare them
+    if (otherParams != null) {
+      this.otherParamsString = otherParams.toString();
+    }
+  }
+
+  private interface JoinKeyCollector {
+    void collect(Object value) throws IOException;
+    DocSet getDocSet() throws IOException;
+  }
+
+  private class TermsJoinKeyCollector implements JoinKeyCollector {
+
+    FieldType fieldType;
+    SolrIndexSearcher searcher;
+
+    TermsEnum termsEnum;
+    BytesRefBuilder bytes;
+    PostingsEnum postingsEnum;
+
+    FixedBitSet bitSet;
+
+    public TermsJoinKeyCollector(FieldType fieldType, Terms terms, SolrIndexSearcher searcher) throws IOException {
+      this.fieldType = fieldType;
+      this.searcher = searcher;
+
+      termsEnum = terms.iterator();
+      bytes = new BytesRefBuilder();
+
+      bitSet = new FixedBitSet(searcher.maxDoc());
+    }
+
+    @Override
+    public void collect(Object value) throws IOException {
+      fieldType.readableToIndexed((String) value, bytes);
+      if (termsEnum.seekExact(bytes.get())) {
+        postingsEnum = termsEnum.postings(postingsEnum, PostingsEnum.NONE);
+        bitSet.or(postingsEnum);
+      }
+    }
+
+    @Override
+    public DocSet getDocSet() throws IOException {
+      if (searcher.getIndexReader().hasDeletions()) {
+        bitSet.and(searcher.getLiveDocSet().getBits());
+      }
+      return new BitDocSet(bitSet);
+    }
+  }
+
+  private class PointJoinKeyCollector extends GraphPointsCollector implements JoinKeyCollector {
+
+    SolrIndexSearcher searcher;
+
+    public PointJoinKeyCollector(SolrIndexSearcher searcher) {
+      super(searcher.getSchema().getField(toField), null, null);
+      this.searcher = searcher;
+    }
+
+    @Override
+    public void collect(Object value) throws IOException {
+      if (value instanceof Long || value instanceof Integer) {
+        set.add(((Number) value).longValue());
+      } else {
+        throw new UnsupportedOperationException("Unsupported field type for XCJFQuery");
+      }
+    }
+
+    @Override
+    public DocSet getDocSet() throws IOException {
+      Query query = getResultQuery(searcher.getSchema().getField(toField), false);
+      if (query == null) {
+        return DocSet.EMPTY;
+      }
+      return DocSetUtil.createDocSet(searcher, query, null);
+    }
+  }
+
+  private class XCJFQueryWeight extends ConstantScoreWeight {
+
+    private SolrIndexSearcher searcher;
+    private ScoreMode scoreMode;
+    private Filter filter;
+
+    public XCJFQueryWeight(SolrIndexSearcher searcher, ScoreMode scoreMode, float score) {
+      super(XCJFQuery.this, score);
+      this.scoreMode = scoreMode;
+      this.searcher = searcher;
+    }
+
+    private String createHashRangeFq() {
+      if (routedByJoinKey) {
+        ClusterState clusterState = searcher.getCore().getCoreContainer().getZkController().getClusterState();
+        CloudDescriptor desc = searcher.getCore().getCoreDescriptor().getCloudDescriptor();
+        Slice slice = clusterState.getCollection(desc.getCollectionName()).getSlicesMap().get(desc.getShardId());
+        DocRouter.Range range = slice.getRange();
+
+        // In CompositeIdRouter, the routing prefix only affects the top 16 bits
+        int min = range.min & 0xffff0000;
+        int max = range.max | 0x0000ffff;
+
+        return String.format(Locale.ROOT, "{!hash_range f=%s l=%d u=%d}", fromField, min, max);

Review Comment:
   The route parameter would route the query to one specific shard.  In this scenario, we want the query to execute on every shard, however, only return the joinkeys from those shards within the given hash range.  



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org