incubator-blur-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From twilli...@apache.org
Subject [1/2] git commit: Support basic full text search; support record selectors
Date Mon, 03 Mar 2014 23:49:18 GMT
Repository: incubator-blur
Updated Branches:
  refs/heads/apache-blur-0.2 2b8f22c1a -> 885604e3b


Support basic full text search; support record selectors


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

Branch: refs/heads/apache-blur-0.2
Commit: 8bf80130ca4c926a84df28629e85a34251ba67c2
Parents: 36c4ade
Author: twilliams <twilliams@apache.org>
Authored: Mon Mar 3 18:48:07 2014 -0500
Committer: twilliams <twilliams@apache.org>
Committed: Mon Mar 3 18:48:07 2014 -0500

----------------------------------------------------------------------
 .../org/apache/blur/slur/BlurQueryHelper.java   |  59 ++++
 .../org/apache/blur/slur/BlurResultHelper.java  |  60 ++++
 .../org/apache/blur/slur/RowMutationHelper.java |  49 ++--
 .../apache/blur/slur/SolrLookingBlurServer.java |  22 +-
 .../apache/blur/slur/BlurQueryHelperTest.java   |  58 ++++
 .../apache/blur/slur/RowMutationHelperTest.java |  50 ++--
 .../blur/slur/SolrLookingBlurServerTest.java    | 271 +++++++++++++------
 7 files changed, 447 insertions(+), 122 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/8bf80130/whiteboard/slur/src/main/java/org/apache/blur/slur/BlurQueryHelper.java
----------------------------------------------------------------------
diff --git a/whiteboard/slur/src/main/java/org/apache/blur/slur/BlurQueryHelper.java b/whiteboard/slur/src/main/java/org/apache/blur/slur/BlurQueryHelper.java
new file mode 100644
index 0000000..bae9cde
--- /dev/null
+++ b/whiteboard/slur/src/main/java/org/apache/blur/slur/BlurQueryHelper.java
@@ -0,0 +1,59 @@
+package org.apache.blur.slur;
+
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.blur.thrift.generated.BlurQuery;
+import org.apache.blur.thrift.generated.Query;
+import org.apache.blur.thrift.generated.Selector;
+import org.apache.commons.collections.map.HashedMap;
+import org.apache.solr.common.params.CommonParams;
+import org.apache.solr.common.params.SolrParams;
+
+import com.google.common.collect.Maps;
+
+public class BlurQueryHelper {
+
+  public static BlurQuery from(SolrParams p) {
+    BlurQuery blurQuery = new BlurQuery();
+    
+    Query query = new Query();
+    query.setRowQuery(false);
+    
+    maybeAddSelector(blurQuery, p);
+    
+    query.setQuery(p.get(CommonParams.Q));
+    
+    blurQuery.setQuery(query);
+    return blurQuery;
+  }
+
+  private static void maybeAddSelector(BlurQuery blurQuery, SolrParams p) {
+    String fieldString = p.get(CommonParams.FL);
+    Selector selector = new Selector();
+    selector.setRecordOnly(true);
+    
+    if(fieldString != null) {
+      Map<String, Set<String>> famCols = Maps.newHashMap();
+      String[] fields = fieldString.split(",");
+      
+      for(String field: fields) {
+        String[] famCol = field.split("\\.");
+        
+        if(famCol.length != 2) {
+          throw new IllegalArgumentException("Fields must be in a family.column format["
+ field + "]");
+        }
+        if(!famCols.containsKey(famCol[0])) {
+          famCols.put(famCol[0], new HashSet<String>());
+        }
+        Set<String> cols = famCols.get(famCol[0]);
+        cols.add(famCol[1]);
+      }
+      selector.setColumnsToFetch(famCols);
+     
+    }
+    blurQuery.setSelector(selector);
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/8bf80130/whiteboard/slur/src/main/java/org/apache/blur/slur/BlurResultHelper.java
----------------------------------------------------------------------
diff --git a/whiteboard/slur/src/main/java/org/apache/blur/slur/BlurResultHelper.java b/whiteboard/slur/src/main/java/org/apache/blur/slur/BlurResultHelper.java
new file mode 100644
index 0000000..aab8482
--- /dev/null
+++ b/whiteboard/slur/src/main/java/org/apache/blur/slur/BlurResultHelper.java
@@ -0,0 +1,60 @@
+package org.apache.blur.slur;
+
+import java.util.List;
+
+import org.apache.blur.thrift.generated.BlurResult;
+import org.apache.blur.thrift.generated.BlurResults;
+import org.apache.blur.thrift.generated.Column;
+import org.apache.blur.thrift.generated.FetchRecordResult;
+import org.apache.blur.thrift.generated.FetchRowResult;
+import org.apache.blur.thrift.generated.Record;
+import org.apache.blur.thrift.generated.Row;
+import org.apache.blur.utils.BlurConstants;
+import org.apache.solr.common.SolrDocument;
+import org.apache.solr.common.SolrDocumentList;
+
+public class BlurResultHelper {
+
+  public static SolrDocumentList from(BlurResults results) {
+    SolrDocumentList docResults = new SolrDocumentList();
+    
+    convertMetadata(results, docResults);
+
+    convertRows(results.getResults(), docResults);
+    
+    return docResults;
+  }
+
+  private static void convertRows(List<BlurResult> results, SolrDocumentList docResults)
{
+    for(BlurResult result: results) {      
+      docResults.add(convertRecord(result.getFetchResult().getRecordResult()));
+    }
+    
+  }
+
+  private static SolrDocument convertRecord(FetchRecordResult recResult) {
+    SolrDocument doc = new SolrDocument();
+    Record record = recResult.getRecord();
+    
+    doc.addField(BlurConstants.RECORD_ID, record.getRecordId());
+    
+    for(Column col: record.getColumns()) {
+      System.out.println("Adding[" + joinColumnFamily(record.getFamily(), col.getName())
+ "] val[" + col.getValue() + "]");
+      doc.addField(joinColumnFamily(record.getFamily(), col.getName()), col.getValue());
+    }
+    
+    return doc;
+  }
+
+  private static String joinColumnFamily(String family, String name) {
+    if(family != null) {
+      return family + "." + name;
+    }
+    return name;
+  }
+
+  private static void convertMetadata(BlurResults results, SolrDocumentList docResults) {
+    docResults.setNumFound(results.getTotalResults());
+    docResults.setStart(results.getQuery().getStart());
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/8bf80130/whiteboard/slur/src/main/java/org/apache/blur/slur/RowMutationHelper.java
----------------------------------------------------------------------
diff --git a/whiteboard/slur/src/main/java/org/apache/blur/slur/RowMutationHelper.java b/whiteboard/slur/src/main/java/org/apache/blur/slur/RowMutationHelper.java
index 2d2cca4..850ea96 100644
--- a/whiteboard/slur/src/main/java/org/apache/blur/slur/RowMutationHelper.java
+++ b/whiteboard/slur/src/main/java/org/apache/blur/slur/RowMutationHelper.java
@@ -24,6 +24,7 @@ import org.apache.blur.thrift.generated.Record;
 import org.apache.blur.thrift.generated.RecordMutation;
 import org.apache.blur.thrift.generated.RecordMutationType;
 import org.apache.blur.thrift.generated.RowMutation;
+import org.apache.blur.utils.BlurConstants;
 import org.apache.solr.common.SolrInputDocument;
 import org.apache.solr.common.SolrInputField;
 
@@ -33,40 +34,38 @@ public class RowMutationHelper {
 
   public static List<RowMutation> from(Collection<SolrInputDocument> docs, String
table) {
     List<RowMutation> mutations = Lists.newArrayList();
-    for(SolrInputDocument d: docs) {
+    for (SolrInputDocument d : docs) {
       mutations.add(from(d, table));
     }
     return mutations;
   }
-  
+
   public static RowMutation from(SolrInputDocument doc, String table) {
-    validate(doc);
+    validateAsRow(doc);
 
     RowMutation mutate = new RowMutation();
-    String rowid = extractId(doc);
+    String rowid = extractRowId(doc);
     mutate.setRowId(rowid);
     mutate.setTable(table);
     List<RecordMutation> recordMutations = Lists.newArrayList();
+
     if (doc.hasChildDocuments()) {
-      for(SolrInputDocument child: doc.getChildDocuments()) {
-        recordMutations.add(createRecordMutation(child, extractId(child)));
+      for (SolrInputDocument child : doc.getChildDocuments()) {
+        validateAsRecord(child);
+        recordMutations.add(createRecordMutation(child, extractRecordId(child)));
       }
-    } else {
-      recordMutations.add(createRecordMutation(doc, rowid));
+      
     }
     mutate.setRecordMutations(recordMutations);
     return mutate;
   }
 
-  private static String extractId(SolrInputDocument doc) {
-    Object id = doc.getFieldValue("rowid");
-    if (id == null) {
-      id = doc.getFieldValue("id");
-    }
-    if (id == null) {
-      throw new IllegalArgumentException("Document must either have id or rowid field.");
-    }
-    return id.toString();
+  private static String extractRowId(SolrInputDocument doc) {
+    return doc.getFieldValue(BlurConstants.ROW_ID).toString();
+  }
+
+  private static String extractRecordId(SolrInputDocument doc) {
+    return doc.getFieldValue(BlurConstants.RECORD_ID).toString();
   }
 
   private static RecordMutation createRecordMutation(SolrInputDocument doc, String id) {
@@ -105,12 +104,24 @@ public class RowMutationHelper {
     throw new IllegalArgumentException("Unable to determine column family from document");
   }
 
-  private static void validate(SolrInputDocument doc) {
-    if ((doc.getFieldNames().size() > 1) && (doc.hasChildDocuments())) {
+  private static void validateAsRow(SolrInputDocument doc) {
+    Object rowid = doc.getFieldValue(BlurConstants.ROW_ID);
+
+    if (rowid == null)
+      throw new IllegalArgumentException("Document must have rowid field.");
 
+    for (String field : doc.getFieldNames()) {
+      if (!BlurConstants.ROW_ID.equals(field)) {
+        throw new IllegalArgumentException("Parent documents act as rows and cant have fields.");
+      }
     }
 
   }
 
+  private static void validateAsRecord(SolrInputDocument doc) {
+    Object rowid = doc.getFieldValue(BlurConstants.RECORD_ID);
 
+    if (rowid == null)
+      throw new IllegalArgumentException("Document must have recordid field.");
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/8bf80130/whiteboard/slur/src/main/java/org/apache/blur/slur/SolrLookingBlurServer.java
----------------------------------------------------------------------
diff --git a/whiteboard/slur/src/main/java/org/apache/blur/slur/SolrLookingBlurServer.java
b/whiteboard/slur/src/main/java/org/apache/blur/slur/SolrLookingBlurServer.java
index 3ec310b..193e390 100644
--- a/whiteboard/slur/src/main/java/org/apache/blur/slur/SolrLookingBlurServer.java
+++ b/whiteboard/slur/src/main/java/org/apache/blur/slur/SolrLookingBlurServer.java
@@ -25,6 +25,8 @@ import org.apache.blur.thrift.BlurClient;
 import org.apache.blur.thrift.BlurClientManager;
 import org.apache.blur.thrift.generated.Blur.Iface;
 import org.apache.blur.thrift.generated.BlurException;
+import org.apache.blur.thrift.generated.BlurQuery;
+import org.apache.blur.thrift.generated.BlurResults;
 import org.apache.blur.thrift.generated.RowMutation;
 import org.apache.blur.thrift.generated.RowMutationType;
 import org.apache.solr.client.solrj.SolrRequest;
@@ -38,6 +40,7 @@ import org.apache.solr.client.solrj.response.QueryResponse;
 import org.apache.solr.client.solrj.response.SolrPingResponse;
 import org.apache.solr.client.solrj.response.SolrResponseBase;
 import org.apache.solr.client.solrj.response.UpdateResponse;
+import org.apache.solr.common.SolrDocumentList;
 import org.apache.solr.common.SolrInputDocument;
 import org.apache.solr.common.params.SolrParams;
 import org.apache.solr.common.util.NamedList;
@@ -222,7 +225,24 @@ public class SolrLookingBlurServer extends SolrServer {
 
   @Override
   public QueryResponse query(SolrParams params) throws SolrServerException {
-    throw new RuntimeException("Not Implemented.");
+    QueryResponse response = new QueryResponse();
+    long start = System.currentTimeMillis();
+    
+    try {
+      BlurQuery blurQuery = BlurQueryHelper.from(params);
+      BlurResults results = client().query(tableName, blurQuery);
+      NamedList<Object> resp = new NamedList<Object>();
+        
+      resp.add("response", BlurResultHelper.from(results));
+      
+      response.setResponse(resp);
+    } catch (Exception e) {
+      throw new SolrServerException("Unable to complete query", e);
+    }
+    
+    response.setElapsedTime((System.currentTimeMillis()-start));
+    
+    return response;
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/8bf80130/whiteboard/slur/src/test/java/org/apache/blur/slur/BlurQueryHelperTest.java
----------------------------------------------------------------------
diff --git a/whiteboard/slur/src/test/java/org/apache/blur/slur/BlurQueryHelperTest.java b/whiteboard/slur/src/test/java/org/apache/blur/slur/BlurQueryHelperTest.java
new file mode 100644
index 0000000..8f72c1f
--- /dev/null
+++ b/whiteboard/slur/src/test/java/org/apache/blur/slur/BlurQueryHelperTest.java
@@ -0,0 +1,58 @@
+package org.apache.blur.slur;
+
+import static org.junit.Assert.*;
+
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.blur.thrift.generated.BlurQuery;
+import org.apache.solr.client.solrj.SolrQuery;
+import org.apache.solr.common.params.SolrParams;
+import org.junit.Test;
+
+public class BlurQueryHelperTest {
+
+  @Test
+  public void simpleQueryString() {
+    SolrParams p = new SolrQuery("foo");
+    
+    BlurQuery query = BlurQueryHelper.from(p);
+    
+    assertEquals("Should get our query string back.", "foo", query.getQuery().getQuery());
+  }
+  
+  @Test(expected=IllegalArgumentException.class)
+  public void fieldValuesMustFollowBlursFamilyColumnFormat() {
+    SolrQuery p = new SolrQuery();
+    
+    p.setFields("foo");
+    
+    BlurQuery query = BlurQueryHelper.from(p);
+  }
+  
+  @Test
+  public void fieldsShouldTranslateToSelector() {
+    SolrQuery p = new SolrQuery();
+    
+    p.setFields("fam1.col1","fam1.col2","fam2.col1");
+    
+    BlurQuery query = BlurQueryHelper.from(p);
+    
+    Map<String, Set<String>> columns = query.getSelector().getColumnsToFetch();
+    
+    assertTrue("Should have fam1 defined.", columns.containsKey("fam1"));
+    assertTrue("Should have fam2 defined.", columns.containsKey("fam2"));
+    
+    Set<String> fam1 = columns.get("fam1");
+    
+    assertEquals("Should get all columns back.", 2, fam1.size());
+    assertTrue("Should contain our column", fam1.contains("col1"));
+    assertTrue("Should contain our column", fam1.contains("col2"));
+    
+    Set<String> fam2 = columns.get("fam2");
+    assertEquals("Should get all columns back.", 1, fam2.size());
+    assertTrue("Should contain our column", fam2.contains("col1"));
+       
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/8bf80130/whiteboard/slur/src/test/java/org/apache/blur/slur/RowMutationHelperTest.java
----------------------------------------------------------------------
diff --git a/whiteboard/slur/src/test/java/org/apache/blur/slur/RowMutationHelperTest.java
b/whiteboard/slur/src/test/java/org/apache/blur/slur/RowMutationHelperTest.java
index 5bf9158..7b9a843 100644
--- a/whiteboard/slur/src/test/java/org/apache/blur/slur/RowMutationHelperTest.java
+++ b/whiteboard/slur/src/test/java/org/apache/blur/slur/RowMutationHelperTest.java
@@ -43,26 +43,32 @@ public class RowMutationHelperTest {
 
   @Test
   public void basicOneForOneConversion() {
-    SolrInputDocument doc = new SolrInputDocument();
-    doc.addField("fam.key", "value");
-    doc.addField("id", "123");
+    SolrInputDocument parent = new SolrInputDocument();
+    parent.addField("rowid", "123");
 
-    RowMutation mutate = RowMutationHelper.from(doc, "foo");
+    SolrInputDocument child = new SolrInputDocument();
+    child.addField("recordid", "1");
+    child.addField("fam.key", "value");
+
+    parent.addChildDocument(child);
+
+    RowMutation mutate = RowMutationHelper.from(parent, "foo");
 
     assertEquals("Should get our rowid back.", "123", mutate.getRowId());
     assertEquals("Should get a single record.", 1, mutate.getRecordMutationsSize());
-    assertEquals("Should get a simple value back.", "value", getRecordValue("key", mutate));
-    assertEquals("Should properly figure our family.", "fam", getFirstRecord(mutate).getFamily());
     assertEquals("Tablename should be set", "foo", mutate.getTable());
   }
 
   @Test
   public void multivalueFieldsShouldTranslate() {
     SolrInputDocument doc = new SolrInputDocument();
-    doc.addField("id", "123");
-    doc.addField("fam.key", "value1");
-    doc.addField("fam.key", "value2");
+    doc.addField("rowid", "123");
 
+    SolrInputDocument child = new SolrInputDocument();
+    child.addField("recordid", "1");
+    child.addField("fam.key", "value1");
+    child.addField("fam.key", "value2");
+    doc.addChildDocument(child);
     RowMutation mutate = RowMutationHelper.from(doc, "foo");
 
     List<Object> vals = getRecordValues("key", mutate);
@@ -75,13 +81,13 @@ public class RowMutationHelperTest {
   @Test
   public void documentWithChildDocumentsShouldBeRowWithRecords() {
     SolrInputDocument doc = new SolrInputDocument();
-    doc.addField("id", "1");
-    
+    doc.addField("rowid", "1");
+
     List<SolrInputDocument> children = Lists.newArrayList();
-    
-    for(int i = 0; i < 10; i++) {
+
+    for (int i = 0; i < 10; i++) {
       SolrInputDocument child = new SolrInputDocument();
-      child.addField("id", i);
+      child.addField("recordid", i);
       child.addField("fam.key", "value" + i);
       children.add(child);
     }
@@ -90,11 +96,14 @@ public class RowMutationHelperTest {
     RowMutation mutate = RowMutationHelper.from(doc, "foo");
 
     assertEquals("Children should turn into records.", 10, mutate.getRecordMutationsSize());
+    assertEquals("Should get a simple value back.", "value0", getRecordValue("key", mutate));
+    assertEquals("Should properly figure our family.", "fam", getFirstRecord(mutate).getFamily());
   }
-  
+
   @Test(expected = IllegalArgumentException.class)
   public void docWithChildrenCantItselfHaveFieldValues() {
     SolrInputDocument parent = new SolrInputDocument();
+    parent.addField("id", "1");
     parent.addField("fam.key1", "123");
     SolrInputDocument child = new SolrInputDocument();
     parent.addChildDocument(child);
@@ -103,6 +112,15 @@ public class RowMutationHelperTest {
   }
 
   @Test(expected = IllegalArgumentException.class)
+  public void rowsCantDirectlyHaveValues() {
+    SolrInputDocument parent = new SolrInputDocument();
+    parent.addField("id", "1");
+    parent.addField("fam.key1", "123");
+
+    RowMutationHelper.from(parent, "foo");
+  }
+
+  @Test(expected = IllegalArgumentException.class)
   public void docsMustUseTheNormalBlurFamilyColumnFormat() {
     SolrInputDocument parent = new SolrInputDocument();
     parent.addField("columnWithoutFamily", "123");
@@ -121,7 +139,7 @@ public class RowMutationHelperTest {
     Record rec = getFirstRecord(mutate);
 
     for (Column col : rec.getColumns()) {
-      
+
       if (col.getName().equals(field)) {
         vals.add(col.getValue());
       }

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/8bf80130/whiteboard/slur/src/test/java/org/apache/blur/slur/SolrLookingBlurServerTest.java
----------------------------------------------------------------------
diff --git a/whiteboard/slur/src/test/java/org/apache/blur/slur/SolrLookingBlurServerTest.java
b/whiteboard/slur/src/test/java/org/apache/blur/slur/SolrLookingBlurServerTest.java
index 05360c2..8e372d4 100644
--- a/whiteboard/slur/src/test/java/org/apache/blur/slur/SolrLookingBlurServerTest.java
+++ b/whiteboard/slur/src/test/java/org/apache/blur/slur/SolrLookingBlurServerTest.java
@@ -18,9 +18,11 @@ package org.apache.blur.slur;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertNull;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.Collection;
 import java.util.List;
 
 import org.apache.blur.MiniCluster;
@@ -34,15 +36,20 @@ import org.apache.blur.thrift.generated.BlurResults;
 import org.apache.blur.thrift.generated.Query;
 import org.apache.blur.thrift.generated.TableDescriptor;
 import org.apache.blur.thrift.generated.TableStats;
+import org.apache.blur.utils.BlurConstants;
 import org.apache.blur.utils.GCWatcher;
+import org.apache.commons.collections.CollectionUtils;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.LocalFileSystem;
 import org.apache.hadoop.fs.Path;
 import org.apache.hadoop.fs.permission.FsAction;
 import org.apache.hadoop.fs.permission.FsPermission;
+import org.apache.solr.client.solrj.SolrQuery;
 import org.apache.solr.client.solrj.SolrServer;
 import org.apache.solr.client.solrj.SolrServerException;
+import org.apache.solr.client.solrj.response.QueryResponse;
+import org.apache.solr.common.SolrDocument;
 import org.apache.solr.common.SolrInputDocument;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
@@ -87,56 +94,23 @@ public class SolrLookingBlurServerTest {
   }
 
   @Test
-  public void addedDocumentShouldShowUpInBlur() throws SolrServerException, IOException,
BlurException, TException {
-    String table = "addedDocumentShouldShowUpInBlur";
-    createTable(table);
-    SolrServer server = new SolrLookingBlurServer(miniCluster.getControllerConnectionStr(),
table);
-    SolrInputDocument doc = new SolrInputDocument();
-    doc.addField("id", "1");
-    doc.addField("fam.value", "123");
-
-    server.add(doc);
-
-    TableStats stats = client().tableStats(table);
-
-    assertEquals("We should have one record.", 1, stats.recordCount);
-    assertEquals("We should have one row.", 1, stats.rowCount);
-
-    assertTotalResults(table, "fam.value:123", 1l);
-
-    removeTable(table);
-  }
-
-  @Test
-  public void childDocsShouldBecomeRecordsOfRow() throws SolrServerException, IOException,
BlurException, TException {
+  public void childDocsShouldBecomeRecordsOfRow() throws Exception {
     String table = "childDocsShouldBecomeRecordsOfRow";
-    createTable(table);
-    SolrServer server = new SolrLookingBlurServer(miniCluster.getControllerConnectionStr(),
table);
-    SolrInputDocument doc = new SolrInputDocument();
-    doc.addField("id", "1");
-    
-    List<SolrInputDocument> children = Lists.newArrayList();
-    for(int i = 0; i < 100; i++) {
-      SolrInputDocument child = new SolrInputDocument();
-      child.addField("id", i);
-      child.addField("fam.key", "value" + i);
-      children.add(child);
-    }
-    doc.addChildDocuments(children);
     
-    server.add(doc);
+    TestTableCreator.newTable(table)
+    .withRowCount(1).withRecordsPerRow(100)
+    .withRecordColumns("fam.value").create();
 
     TableStats stats = client().tableStats(table);
 
     assertEquals("We should have one record.", 100, stats.recordCount);
     assertEquals("We should have one row.", 1, stats.rowCount);
 
-    assertTotalResults(table, "fam.key:value1", 1l);
+    assertTotalResults(table, "fam.value:value0-0", 1l);
     assertTotalRecordResults(table, "recordid:99", 1l);
 
     removeTable(table);
-  }  
-  
+  }
 
   @Test
   public void docShouldBeDiscoverableWithMultiValuedFields() throws SolrServerException,
IOException, BlurException,
@@ -145,9 +119,14 @@ public class SolrLookingBlurServerTest {
     createTable(table);
     SolrServer server = new SolrLookingBlurServer(miniCluster.getControllerConnectionStr(),
table);
     SolrInputDocument doc = new SolrInputDocument();
-    doc.addField("id", "1");
-    doc.addField("fam.value", "123");
-    doc.addField("fam.value", "124");
+    doc.addField("rowid", "1");
+
+    SolrInputDocument child = new SolrInputDocument();
+    child.addField("recordid", "1");
+    child.addField("fam.value", "123");
+    child.addField("fam.value", "124");
+
+    doc.addChildDocument(child);
 
     server.add(doc);
 
@@ -159,74 +138,49 @@ public class SolrLookingBlurServerTest {
   }
 
   @Test
-  public void documentsShouldBeAbleToBeIndexedInBatch() throws SolrServerException, IOException,
BlurException,
+  public void documentsShouldBeAbleToBeIndexedInBatch() throws Exception,
       TException {
     String table = "multipleDocumentsShouldBeIndexed";
-    createTable(table);
+    TestTableCreator.newTable(table)
+                          .withRowCount(100).withRecordsPerRow(1)
+                          .withRecordColumns("fam.value").create();
+ 
 
-    SolrServer server = new SolrLookingBlurServer(miniCluster.getControllerConnectionStr(),
table);
-
-    List<SolrInputDocument> docs = Lists.newArrayList();
-
-    for (int i = 0; i < 100; i++) {
-      SolrInputDocument doc = new SolrInputDocument();
-      doc.addField("id", i);
-      doc.addField("fam.value", "12" + i);
-      docs.add(doc);
-    }
-
-    server.add(docs);
-
-    assertTotalResults(table, "fam.value:123", 1l);
-    assertTotalResults(table, "fam.value:124", 1l);
+    assertTotalResults(table, "fam.value:value0-0", 1l);
+    assertTotalResults(table, "fam.value:value1-0", 1l);
     assertTotalResults(table, "rowid:1", 1l);
     assertTotalResults(table, "rowid:2", 1l);
-    assertTotalResults(table, "fam.value:1299", 1l);
+    assertTotalResults(table, "fam.value:value99-0", 1l);
     assertTotalResults(table, "fam.value:justincase", 0l);
 
     removeTable(table);
   }
 
   @Test
-  public void weShouldBeAbleToDeleteARowById() throws SolrServerException, IOException, BlurException,
+  public void weShouldBeAbleToDeleteARowById() throws Exception,
       TException {
     String table = "weShouldBeAbleToDeleteARowById";
-    createTable(table);
-    SolrServer server = new SolrLookingBlurServer(miniCluster.getControllerConnectionStr(),
table);
-    SolrInputDocument doc1 = new SolrInputDocument();
-    doc1.addField("id", "1");
-    doc1.addField("fam.value", "123");
-    SolrInputDocument doc2 = new SolrInputDocument();
-    doc2.addField("id", "2");
-    doc2.addField("fam.value", "124");
-    List<SolrInputDocument> docs = Lists.newArrayList(doc1, doc2);
-
-    server.add(docs);
+    
+    SolrServer server = TestTableCreator.newTable(table).withRowCount(2).withRecordsPerRow(1).withRecordColumns("fam.value").create();
 
+    assertTotalResults(table, "rowid:0", 1l);
     assertTotalResults(table, "rowid:1", 1l);
-    assertTotalResults(table, "rowid:2", 1l);
 
     server.deleteById("1");
 
+    assertTotalResults(table, "rowid:0", 1l);
     assertTotalResults(table, "rowid:1", 0l);
-    assertTotalResults(table, "rowid:2", 1l);
 
     removeTable(table);
   }
 
   @Test
-  public void weShouldBeAbleToDeleteARowByAListOfIds() throws SolrServerException, IOException,
BlurException,
+  public void weShouldBeAbleToDeleteARowByAListOfIds() throws Exception,
       TException {
     String table = "weShouldBeAbleToDeleteARowByAListOfIds";
-    createTable(table);
-    SolrServer server = new SolrLookingBlurServer(miniCluster.getControllerConnectionStr(),
table);
-    for (int i = 0; i < 20; i++) {
-      SolrInputDocument doc = new SolrInputDocument();
-      doc.addField("id", i);
-      doc.addField("fam.value", "value" + i);
-      server.add(doc);
-    }
-
+    
+    SolrServer server = TestTableCreator.newTable(table).withRowCount(20).withRecordsPerRow(1).withRecordColumns("fam.value").create();
+    
     assertTotalResults(table, "rowid:1", 1l);
     assertTotalResults(table, "rowid:2", 1l);
     List<String> ids = Lists.newArrayList("1", "2", "3", "4", "5");
@@ -239,6 +193,99 @@ public class SolrLookingBlurServerTest {
     removeTable(table);
   }
 
+  @Test
+  public void basicFullTextQuery() throws Exception {
+    String table = "basicFullTextQuery";
+    SolrServer server = TestTableCreator.newTable(table)
+        .withRowCount(1).withRecordsPerRow(2)
+        .withRecordColumns("fam.value", "fam.mvf","fam.mvf").create();
+
+    SolrQuery query = new SolrQuery("value0-0");
+
+    QueryResponse response = server.query(query);
+
+    assertEquals("We should get our doc back.", 1l, response.getResults().getNumFound());
+
+    SolrDocument docResult = response.getResults().get(0);
+
+    assertEquals("0", docResult.getFieldValue("recordid"));
+    assertEquals("value0-0", docResult.getFieldValue("fam.value"));
+
+    Collection<Object> mvfVals = docResult.getFieldValues("fam.mvf");
+
+    assertTrue("We should get all our values back[" + mvfVals + "]",
+        CollectionUtils.isEqualCollection(mvfVals, Lists.newArrayList("value0-0", "value0-0")));
+
+    removeTable(table);
+  }
+
+  @Test
+  public void fieldsRequestsShouldTurnIntoSelectors() throws Exception,
+      TException {
+
+    String table = "fieldsRequestsShouldTurnIntoSelectors";
+    SolrServer server = TestTableCreator.newTable(table)
+                          .withRowCount(1).withRecordsPerRow(2)
+                          .withRecordColumns("fam.value", "fam.mvf").create();
+
+    SolrQuery query = new SolrQuery("value0-0");
+    query.setFields("fam.value");
+    QueryResponse response = server.query(query);
+
+    assertEquals("We should get our doc back for a valid test.", 1l, response.getResults().getNumFound());
+
+    SolrDocument docResult = response.getResults().get(0);
+
+    assertEquals("value0-0", docResult.getFieldValue("fam.value"));
+    assertNull("We shouldn't get this one back since it wasnt in our fields.", docResult.getFieldValues("fam.mvf"));
+
+    removeTable(table);
+  }
+
+  @Test
+  public void weShouldBeAbleToPageResults() throws SolrServerException, IOException, BlurException,
TException {
+    String table = "weShouldBeAbleToPageResults";
+    SolrServer server = createServerAndTableWithSimpleTestDoc(table);
+
+    SolrQuery query = new SolrQuery("123");
+    query.setFields("fam.value");
+    QueryResponse response = server.query(query);
+
+    assertEquals("We should get our doc back for a valid test.", 1l, response.getResults().getNumFound());
+
+    SolrDocument docResult = response.getResults().get(0);
+
+    assertEquals("123", docResult.getFieldValue("fam.value"));
+    assertNull("We shouldn't get this one back since it wasnt in our fields.", docResult.getFieldValues("fam.mvf"));
+
+    removeTable(table);
+  }
+
+  private SolrServer createServerAndTableWithSimpleTestDoc(String table) throws BlurException,
TException, IOException,
+      SolrServerException {
+    createTable(table);
+    SolrServer server = new SolrLookingBlurServer(miniCluster.getControllerConnectionStr(),
table);
+    SolrInputDocument doc;
+
+    doc = createSimpleTestDoc();
+
+    server.add(doc);
+    return server;
+  }
+
+  private SolrInputDocument createSimpleTestDoc() {
+    SolrInputDocument doc;
+    doc = new SolrInputDocument();
+    doc.addField("rowid", "1");
+    SolrInputDocument child = new SolrInputDocument();
+    child.addField("recordid", "1");
+    child.addField("fam.value", "123");
+    child.addField("fam.mvf", "aaa");
+    child.addField("fam.mvf", "bbb");
+    doc.addChildDocument(child);
+    return doc;
+  }
+
   private void assertTotalResults(String table, String q, long expected) throws BlurException,
TException {
     BlurQuery bquery = new BlurQuery();
     Query query = new Query();
@@ -248,17 +295,18 @@ public class SolrLookingBlurServerTest {
 
     assertEquals("Should find our row.", expected, results.getTotalResults());
   }
+
   private void assertTotalRecordResults(String table, String q, long expected) throws BlurException,
TException {
     BlurQuery bquery = new BlurQuery();
     Query query = new Query();
     query.setQuery(q);
     query.setRowQuery(false);
     bquery.setQuery(query);
-   
+
     BlurResults results = client().query(table, bquery);
 
     assertEquals("Should find our record.", expected, results.getTotalResults());
-    
+
   }
 
   private void removeTable(String table) {
@@ -289,4 +337,55 @@ public class SolrLookingBlurServerTest {
     return BlurClient.getClient(connectionStr);
   }
 
+  private static class TestTableCreator {
+    private int rows = 1;
+    private int recordsPerRow = 10;
+    private String tableName;
+    private String[] columns = { "fam.value" };
+
+    private TestTableCreator(String table) {
+      this.tableName = table;
+    }
+
+    public static TestTableCreator newTable(String table) {
+      return new TestTableCreator(table);
+    }
+
+    public TestTableCreator withRowCount(int rows) {
+      this.rows = rows;
+      return this;
+    }
+
+    public TestTableCreator withRecordsPerRow(int count) {
+      this.recordsPerRow = count;
+      return this;
+    }
+
+    public TestTableCreator withRecordColumns(String... cols) {
+      this.columns = cols;
+      return this;
+    }
+
+    public SolrServer create() throws Exception {
+      createTable(tableName);
+      SolrServer server = new SolrLookingBlurServer(miniCluster.getControllerConnectionStr(),
tableName);
+
+      for (int i = 0; i < rows; i++) {
+        SolrInputDocument parent = new SolrInputDocument();
+        parent.addField(BlurConstants.ROW_ID, i);
+        for (int j = 0; j < recordsPerRow; j++) {
+          SolrInputDocument child = new SolrInputDocument();
+          child.addField(BlurConstants.RECORD_ID, j);
+          
+          for (String colName : columns) {
+            child.addField(colName, "value" + i + "-" + j);
+          }
+          parent.addChildDocument(child);
+        }
+        server.add(parent);
+      }
+      return server;
+    }
+  }
+
 }


Mime
View raw message