incubator-blur-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From amccu...@apache.org
Subject [8/9] git commit: Added the aggregate feature to get a single best response from the server. Still needs a sort implementation.
Date Tue, 19 Feb 2013 01:59:11 GMT
Added the aggregate feature to get a single best response from the server.  Still needs a sort
implementation.


Project: http://git-wip-us.apache.org/repos/asf/incubator-blur/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-blur/commit/a5a38ff4
Tree: http://git-wip-us.apache.org/repos/asf/incubator-blur/tree/a5a38ff4
Diff: http://git-wip-us.apache.org/repos/asf/incubator-blur/diff/a5a38ff4

Branch: refs/heads/0.2-dev
Commit: a5a38ff4e8a92da8bd9def93ffc7c890b4b239a3
Parents: 7dded84
Author: Aaron McCurry <amccurry@gmail.com>
Authored: Mon Feb 18 20:56:26 2013 -0500
Committer: Aaron McCurry <amccurry@gmail.com>
Committed: Mon Feb 18 20:56:26 2013 -0500

----------------------------------------------------------------------
 .../java/org/apache/blur/server/BlurServer.java    |   16 ++++-
 .../src/main/java/org/apache/blur/utils/Merge.java |   65 +++++++++++++++
 2 files changed, 80 insertions(+), 1 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/a5a38ff4/src/blur-core/src/main/java/org/apache/blur/server/BlurServer.java
----------------------------------------------------------------------
diff --git a/src/blur-core/src/main/java/org/apache/blur/server/BlurServer.java b/src/blur-core/src/main/java/org/apache/blur/server/BlurServer.java
index d727f76..d14e25f 100644
--- a/src/blur-core/src/main/java/org/apache/blur/server/BlurServer.java
+++ b/src/blur-core/src/main/java/org/apache/blur/server/BlurServer.java
@@ -69,6 +69,7 @@ import org.apache.blur.thrift.generated.UpdatePackage;
 import org.apache.blur.utils.BlurConstants;
 import org.apache.blur.utils.BlurUtil;
 import org.apache.blur.utils.BlurValidations;
+import org.apache.blur.utils.Merge;
 import org.apache.blur.utils.ThriftLuceneConversion;
 import org.apache.lucene.index.IndexReader;
 import org.apache.lucene.search.IndexSearcher;
@@ -237,7 +238,11 @@ public class BlurServer extends TableAdmin implements Iface {
       for (Future<TopFieldDocs> future : futures) {
         result.add(future.get());
       }
-      return result;
+      if (queryArgs.isAggregateResults()) {
+        return aggregateResults(result,numberToFetch);
+      } else {
+        return result;  
+      }
     } catch (Throwable t) {
       LOG.error("Unknown error", t);
       throw new BException(t.getMessage(), t);
@@ -246,6 +251,15 @@ public class BlurServer extends TableAdmin implements Iface {
     }
   }
 
+  private List<TopFieldDocs> aggregateResults(List<TopFieldDocs> topFieldDocs,
int numberToFetch) throws IOException {
+    if (topFieldDocs == null || topFieldDocs.size() <= 1) {
+      return topFieldDocs;
+    }
+    List<TopFieldDocs> result = new ArrayList<TopFieldDocs>();
+    result.add(Merge.merge(topFieldDocs, null, numberToFetch));
+    return result;
+  }
+
   private Collection<SearchAction> getSearchActions(TableDescriptor tableDescriptor,
List<Integer> shardIndexes, Map<Integer, IndexSearcher> searchers) throws BlurException
{
     String name = tableDescriptor.getName();
     int shardCount = tableDescriptor.getShardCount();

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/a5a38ff4/src/blur-core/src/main/java/org/apache/blur/utils/Merge.java
----------------------------------------------------------------------
diff --git a/src/blur-core/src/main/java/org/apache/blur/utils/Merge.java b/src/blur-core/src/main/java/org/apache/blur/utils/Merge.java
new file mode 100644
index 0000000..94e26d8
--- /dev/null
+++ b/src/blur-core/src/main/java/org/apache/blur/utils/Merge.java
@@ -0,0 +1,65 @@
+package org.apache.blur.utils;
+
+/**
+ * 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.
+ */
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
+import org.apache.blur.thrift.generated.ScoreDoc;
+import org.apache.blur.thrift.generated.Sort;
+import org.apache.blur.thrift.generated.TopFieldDocs;
+
+public class Merge {
+
+  /**
+   * Pass sort=null to merge sort by score descending.
+   */
+  public static TopFieldDocs merge(List<TopFieldDocs> shardHits, Sort sort, int topN)
throws IOException {
+    if (sort == null) {
+      TopFieldDocs topFieldDocs = new TopFieldDocs();
+      topFieldDocs.setShardIndex(-1);
+      long totalHits = 0;
+      ArrayList<ScoreDoc> scoreDocs = new ArrayList<ScoreDoc>();
+      for (int i = 0; i < shardHits.size(); i++) {
+        TopFieldDocs tfd = shardHits.get(i);
+        totalHits += tfd.getTotalHits();
+        scoreDocs.addAll(tfd.getScoreDocs());
+      }
+      topFieldDocs.setTotalHits(totalHits);
+      Collections.sort(scoreDocs, new Comparator<ScoreDoc>() {
+        @Override
+        public int compare(ScoreDoc left, ScoreDoc right) {
+          int compare = Double.compare(right.getScore(), left.getScore());
+          if (compare == 0) {
+            long ldl = left.getDocLocation();
+            long rdl = right.getDocLocation();
+            return (int) (ldl - rdl);
+          }
+          return compare;
+        }
+      });
+      int length = Math.min(topN, scoreDocs.size());
+      topFieldDocs.setScoreDocs(scoreDocs.subList(0, length));
+      return topFieldDocs;
+    } else {
+      throw new RuntimeException("not supported");
+    }
+  }
+}


Mime
View raw message