Reworked search results to group rows together better

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

Branch: refs/heads/apache-blur-0.2
Commit: a2538426a0a89f6f76ba2a1e375573b9e66cd28e
Parents: de753b6
Author: Chris Rohr <[email protected]>
Authored: Sun May 25 15:46:04 2014 -0400
Committer: Chris Rohr <[email protected]>
Committed: Sun May 25 15:46:04 2014 -0400

----------------------------------------------------------------------
 .../apache/blur/console/util/SearchUtil.java    | 77 ++++++++++++++++----
 .../src/main/webapp/js/blurconsole.fake.js      | 30 ++++++--
 .../src/main/webapp/js/blurconsole.model.js     |  9 ++-
 .../src/main/webapp/js/blurconsole.search.js    | 72 +++++++++++++-----
 .../main/webapp/sass/blurconsole.search.scss    |  9 +++
 5 files changed, 153 insertions(+), 44 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/a2538426/contrib/blur-console/src/main/java/org/apache/blur/console/util/SearchUtil.java
----------------------------------------------------------------------
diff --git 
a/contrib/blur-console/src/main/java/org/apache/blur/console/util/SearchUtil.java
 
b/contrib/blur-console/src/main/java/org/apache/blur/console/util/SearchUtil.java
index 23eecd3..78804f1 100644
--- 
a/contrib/blur-console/src/main/java/org/apache/blur/console/util/SearchUtil.java
+++ 
b/contrib/blur-console/src/main/java/org/apache/blur/console/util/SearchUtil.java
@@ -8,6 +8,7 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.TreeMap;
 
 import org.apache.blur.thirdparty.thrift_0_9_0.TException;
 import org.apache.blur.thrift.BlurClient;
@@ -44,6 +45,31 @@ import org.apache.blur.thrift.generated.Selector;
  */
 
 public class SearchUtil {
+       
+       /**
+        * Record only results:
+        * 
+        * {
+        *      famName: [
+        *              {
+        *                      colName: value
+        *              }
+        *      ]
+        * }
+        * 
+        * Row results:
+        * {
+        *      famName: {
+        *              rowid: [
+        *                      {
+        *                              colName: value
+        *                      }
+        *              ]
+        *      }
+        * }
+        */
+       
+       @SuppressWarnings("unchecked")
        public static Map<String, Object> search(Map<String, String[]> params) 
throws IOException, BlurException, TException {
                Iface client = 
BlurClient.getClient(Config.getConnectionString());
                
@@ -54,6 +80,8 @@ public class SearchUtil {
                String fetch = params.get("fetch")[0];
                String[] families = params.get("families[]");
                
+               boolean recordsOnly = "recordrecord".equalsIgnoreCase(rowQuery);
+               
                BlurQuery blurQuery = new BlurQuery();
                
                Query q = new Query(query, "rowrow".equalsIgnoreCase(rowQuery), 
ScoreType.SUPER, null, null);
@@ -62,7 +90,7 @@ public class SearchUtil {
                blurQuery.setFetch(Integer.parseInt(fetch));
                
                Selector s = new Selector();
-               s.setRecordOnly("recordrecord".equalsIgnoreCase(rowQuery));
+               s.setRecordOnly(recordsOnly);
                s.setColumnFamiliesToFetch(new 
HashSet<String>(Arrays.asList(families)));
                blurQuery.setSelector(s);
                
@@ -72,11 +100,11 @@ public class SearchUtil {
                results.put("total", blurResults.getTotalResults());
                
                Set<String> fams = new HashSet<String>();
-               Map<String, List<Map<String, Object>>> rows = new 
HashMap<String, List<Map<String, Object>>>();
+               Map<String, Object> rows = new HashMap<String, Object>();
                for (BlurResult result : blurResults.getResults()) {
                        FetchResult fetchResult = result.getFetchResult();
                        
-                       if ("recordrecord".equalsIgnoreCase(rowQuery)) {
+                       if (recordsOnly) {
                                // Record Result
                                FetchRecordResult recordResult = 
fetchResult.getRecordResult();
                                Record record = recordResult.getRecord();
@@ -84,7 +112,8 @@ public class SearchUtil {
                                String family = record.getFamily();
                                fams.add(family);
                                
-                               addRowToFam(family, 
columnsToMap(record.getColumns(), null, record.getRecordId()), rows);
+                               List<Map<String, String>> fam = 
(List<Map<String, String>>) getFam(family, rows, recordsOnly);
+                               fam.add(buildRow(record.getColumns(), 
record.getRecordId()));
                        } else {
                                // Row Result
                                FetchRowResult rowResult = 
fetchResult.getRowResult();
@@ -93,7 +122,9 @@ public class SearchUtil {
                                        String family = record.getFamily();
                                        fams.add(family);
                                        
-                                       addRowToFam(family, 
columnsToMap(record.getColumns(), row.getId(), record.getRecordId()), rows);
+                                       Map<String, List<Map<String, String>>> 
fam = (Map<String, List<Map<String, String>>>) getFam(family, rows, 
recordsOnly);
+                                       List<Map<String, String>> rowData = 
getRow(row.getId(), fam);
+                                       
rowData.add(buildRow(record.getColumns(), record.getRecordId()));
                                }
                        }
                }
@@ -104,26 +135,40 @@ public class SearchUtil {
                return results;
        }
        
-       private static void addRowToFam(String fam, Map<String, Object> row, 
Map<String, List<Map<String, Object>>> results) {
-               List<Map<String, Object>> famResults = results.get(fam);
+       private static Map<String, String> buildRow(List<Column> columns, 
String recordid) {
+               Map<String, String> map = new TreeMap<String, String>();
+               map.put("recordid", recordid);
+               
+               for (Column column : columns) {
+                       map.put(column.getName(), column.getValue());
+               }
+               
+               return map;
+       }
+       
+       private static Object getFam(String fam, Map<String, Object> results, 
boolean recordOnly) {
+               Object famResults = results.get(fam);
                
                if (famResults == null) {
-                       famResults = new ArrayList<Map<String,Object>>();
+                       if (recordOnly) {
+                               famResults = new ArrayList<Map<String, 
String>>();                              
+                       } else {
+                               famResults = new TreeMap<String, 
List<Map<String, String>>>();
+                       }
                        results.put(fam, famResults);
                }
                
-               famResults.add(row);
+               return famResults;
        }
        
-       private static Map<String, Object> columnsToMap(List<Column> columns, 
String rowid, String recordid) {
-               Map<String, Object> map = new HashMap<String, Object>();
-               map.put("rowid", rowid);
-               map.put("recordid", recordid);
+       private static List<Map<String, String>> getRow(String rowid, 
Map<String, List<Map<String, String>>> rows) {
+               List<Map<String, String>> row = rows.get(rowid);
                
-               for (Column column : columns) {
-                       map.put(column.getName(), column.getValue());
+               if (row == null) {
+                       row = new ArrayList<Map<String, String>>();
+                       rows.put(rowid, row);
                }
                
-               return map;
+               return row;
        }
 }

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/a2538426/contrib/blur-console/src/main/webapp/js/blurconsole.fake.js
----------------------------------------------------------------------
diff --git a/contrib/blur-console/src/main/webapp/js/blurconsole.fake.js 
b/contrib/blur-console/src/main/webapp/js/blurconsole.fake.js
index 1a817e2..ef5d064 100644
--- a/contrib/blur-console/src/main/webapp/js/blurconsole.fake.js
+++ b/contrib/blur-console/src/main/webapp/js/blurconsole.fake.js
@@ -179,13 +179,31 @@ blurconsole.fake = (function() {
                        if (total - args.start < toFetch) {
                                toFetch = total - args.start;
                        }
-                       results[fam] = [];
-                       for (var r = 0; r < randomNumber(toFetch); r++) {
-                               var row = {};
-                               for (var c=0; c < cols; c++) {
-                                       row['col'+c] = randomString();
+
+                       if (args.rowRecordOption === 'recordrecord') {
+                               results[fam] = [];
+                               for (var recordIndex = 0; recordIndex < 
randomNumber(toFetch); recordIndex++) {
+                                       var recordRow = {};
+                                       recordRow.recordid = 
randomNumber(1000000).toString();
+                                       for (var recordColIndex=0; 
recordColIndex < cols; recordColIndex++) {
+                                               recordRow['col'+recordColIndex] 
= randomString();
+                                       }
+                                       results[fam].push(recordRow);
+                               }
+                       } else {
+                               results[fam] = {};
+                               for (var rowIndex = 0; rowIndex < 
randomNumber(toFetch); rowIndex++) {
+                                       var rowid = 
randomNumber(10000000).toString();
+                                       results[fam][rowid] = [];
+                                       for (var rowRecordIndex = 0; 
rowRecordIndex < randomNumber(10); rowRecordIndex++) {
+                                               var row = {};
+                                               row.recordid = 
randomNumber(1000000).toString();
+                                               for (var rowRecordColIndex=0; 
rowRecordColIndex < cols; rowRecordColIndex++) {
+                                                       
row['col'+rowRecordColIndex] = randomString();
+                                               }
+                                               results[fam][rowid].push(row);
+                                       }
                                }
-                               results[fam].push(row);
                        }
                });
 

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/a2538426/contrib/blur-console/src/main/webapp/js/blurconsole.model.js
----------------------------------------------------------------------
diff --git a/contrib/blur-console/src/main/webapp/js/blurconsole.model.js 
b/contrib/blur-console/src/main/webapp/js/blurconsole.model.js
index 9e8fc41..c85d006 100644
--- a/contrib/blur-console/src/main/webapp/js/blurconsole.model.js
+++ b/contrib/blur-console/src/main/webapp/js/blurconsole.model.js
@@ -410,8 +410,13 @@ blurconsole.model = (function() {
 
                        if (typeof dataResults !== 'undefined' && dataResults 
!== null) {
                                $.each(dataResults, function(family, 
resultList){
-                                       var tmpList = results[family] || [];
-                                       results[family] = 
tmpList.concat(resultList);
+                                       if (currentArgs.rowRecordOption === 
'recordrecord') {
+                                               var recordList = 
results[family] || [];
+                                               results[family] = 
recordList.concat(resultList);
+                                       } else {
+                                               var rowList = results[family] 
|| {};
+                                               results[family] = 
$.extend(resultList, rowList);
+                                       }
                                });
                        }
                        $.gevent.publish('results-updated', [dataFamilies]);

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/a2538426/contrib/blur-console/src/main/webapp/js/blurconsole.search.js
----------------------------------------------------------------------
diff --git a/contrib/blur-console/src/main/webapp/js/blurconsole.search.js 
b/contrib/blur-console/src/main/webapp/js/blurconsole.search.js
index 3cfd20b..9654262 100644
--- a/contrib/blur-console/src/main/webapp/js/blurconsole.search.js
+++ b/contrib/blur-console/src/main/webapp/js/blurconsole.search.js
@@ -47,7 +47,7 @@ blurconsole.search = (function () {
        jqueryMap = {},
        setJqueryMap, initModule, unloadModule, drawResultHolders, drawResults, 
registerPageEvents, unregisterPageEvents,
        sendSearch, showOptions, reviewTables, loadTableList, getMoreData, 
fixPanelWidths, updateOptionPopover, updateOptionDisplay,
-       persistOptions;
+       persistOptions, getColList;
 
        setJqueryMap = function() {
                var $container = stateMap.$container;
@@ -211,31 +211,53 @@ blurconsole.search = (function () {
                $.each(families, function(i, fam) {
                        var famResults = results[fam],
                                famId = '#' + 
blurconsole.browserUtils.cleanId(fam),
-                               famHolder = $(famId + ' .panel-body'), table = 
'', cols;
+                               famHolder = $(famId + ' .panel-body'),
+                               table = '<table class="table table-condensed 
table-hover table-bordered"><thead><tr>',
+                               cols, famTotal;
 
-                       cols = 
blurconsole.utils.reject(blurconsole.utils.keys(famResults[0]), function(i) {
-                               return i === 'rowid' || i === 'recordid';
-                       });
-                       cols.sort();
+                       if ($.isArray(famResults)) {
+                               // Record results
+                               cols = getColList(famResults[0]);
 
-                       cols = ['rowid', 'recordid'].concat(cols);
+                               $.each(cols, function(i, col) {
+                                       table += '<th>' + col + '</th>';
+                               });
+                               table += '</tr></thead><tbody>';
+                               $.each(famResults, function(i, row) {
+                                       table += '<tr>';
+                                       $.each(cols, function(c, col) {
+                                               table += '<td>' + (row[col] || 
'') + '</td>';
+                                       });
+                                       table += '</tr>';
+                               });
+                               famTotal = famResults.length;
+                       } else {
+                               // Row results
+                               var rowids = blurconsole.utils.keys(famResults);
+                               var firstRow = famResults[rowids[0]];
+                               cols = getColList(firstRow[0]);
 
-                       table += '<table class="table table-condensed 
table-hover table-bordered"><thead><tr>';
-                       $.each(cols, function(i, col) {
-                               table += '<th>' + col + '</th>';
-                       });
-                       table += '</tr></thead><tbody>';
-                       $.each(famResults, function(i, row) {
-                               table += '<tr>';
-                               $.each(cols, function(c, col) {
-                                       table += '<td>' + (row[col] || '') + 
'</td>';
+                               $.each(cols, function(i, col) {
+                                       table += '<th>' + col + '</th>';
                                });
-                               table += '</tr>';
-                       });
+                               table += '</tr></thead><tbody>';
+                               $.each(famResults, function(rowid, records) {
+                                       table += '<tr class="row-separator"><td 
colspan="' + cols.length + '"><strong>rowid:</strong> ' + rowid + ' (<em>' + 
records.length + ' records</em>)</td></tr>';
+                                       $.each(records, function(i, rec) {
+                                               table += '<tr>';
+                                               $.each(cols, function(c, col) {
+                                                       table += '<td>' + 
(rec[col] || '') + '</td>';
+                                               });
+                                               table += '</tr>';
+                                       });
+                               });
+                               famTotal = rowids.length;
+                       }
+
                        table += '</tbody></table>';
 
-                       if (famResults.length < 
blurconsole.model.search.getTotal()) {
-                               table += '<div class="pull-right"><a href="' + 
famId + '" class="btn btn-primary nextPage">Load More...</a></div>';
+                       if (famTotal < blurconsole.model.search.getTotal()) {
+                               table += '<div class="pull-left"><a href="' + 
famId + '" class="btn btn-primary nextPage">Load More...</a></div>';
                        }
 
                        famHolder.html(table);
@@ -246,6 +268,16 @@ blurconsole.search = (function () {
                fixPanelWidths();
        };
 
+       getColList = function(row) {
+               var cols = 
blurconsole.utils.reject(blurconsole.utils.keys(row), function(i) {
+                       return i === 'recordid';
+               });
+               cols.sort();
+
+               cols = ['recordid'].concat(cols);
+               return cols;
+       };
+
        loadTableList = function() {
                var tableMap = blurconsole.model.tables.getAllEnabledTables();
 

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/a2538426/contrib/blur-console/src/main/webapp/sass/blurconsole.search.scss
----------------------------------------------------------------------
diff --git a/contrib/blur-console/src/main/webapp/sass/blurconsole.search.scss 
b/contrib/blur-console/src/main/webapp/sass/blurconsole.search.scss
index 2cffc29..da53b29 100644
--- a/contrib/blur-console/src/main/webapp/sass/blurconsole.search.scss
+++ b/contrib/blur-console/src/main/webapp/sass/blurconsole.search.scss
@@ -28,4 +28,13 @@ under the License.
 #resultCount {
        padding-left: 30px;
        font-style: italic;
+}
+
+tr.row-separator {
+       td {
+               background-color: #999;
+       }
+       &:hover td {
+               background-color: #999 !important;
+       }
 }
\ No newline at end of file

Reply via email to