Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Facet Pivot, histogram widget, and pyramid widget #47

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
ajax-solr.min.js
*~
83 changes: 75 additions & 8 deletions core/AbstractFacetWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
}
}(function () {


/**
* Baseclass for all facet widgets.
*
Expand Down Expand Up @@ -82,6 +83,16 @@ AjaxSolr.AbstractFacetWidget = AjaxSolr.AbstractWidget.extend(
'facet.range.include'
]);
}
/***************************
* Added new parameter for facet.pivot: according to the documentation
* there is only facet.pivot.mincount
* ************************/
else if (this['facet.pivot'] !== undefined) {
this.manager.store.addByValue('facet.pivot', this.field);
parameters = parameters.concat([
'facet.pivot.mincount',
]);
}

for (var i = 0, l = parameters.length; i < l; i++) {
if (this[parameters[i]] !== undefined) {
Expand Down Expand Up @@ -121,6 +132,17 @@ AjaxSolr.AbstractFacetWidget = AjaxSolr.AbstractWidget.extend(
return this.manager.store.addByValue('fq', this.fq(value));
});
},

/**
* Adds a filter query independent of the widget facet field.
*
* @returns {Boolean} Whether a filter query was added.
*/
addByField: function (field, value) {
return this.changeSelection(function () {
return this.manager.store.addByValue('fq', (field+':'+value));
});
},

/**
* Removes a filter query.
Expand Down Expand Up @@ -183,19 +205,42 @@ AjaxSolr.AbstractFacetWidget = AjaxSolr.AbstractWidget.extend(
else if (this['facet.range'] !== undefined) {
property = 'facet_ranges';
}
else if (this['facet.pivot'] !== undefined) {
property = 'facet_pivot';
}
if (property !== undefined) {
switch (this.manager.store.get('json.nl').val()) {
case 'map':
return this.getFacetCountsMap(property);
case 'arrarr':
return this.getFacetCountsArrarr(property);
default:
return this.getFacetCountsFlat(property);
}
/* -------------------------------------------------------------------------------------------------
New If added to control the case of the facet_pivots: we do not know how the
* json.nl value would affect pivots, therefore this special case.
-----------------------------------------*/
if (property==='facet_pivot'){
return this.getPivotCountsMap(property);
}else{
//---------------------------------------------------------------------------------------------------
switch (this.manager.store.get('json.nl').val()) {
case 'map':
return this.getFacetCountsMap(property);
case 'arrarr':
return this.getFacetCountsArrarr(property);
default:
return this.getFacetCountsFlat(property);
}
}
}
throw 'Cannot get facet counts unless one of the following properties is set to "true" on widget "' + this.id + '": "facet.field", "facet.date", or "facet.range".';
},

/** ----------------------------------------------------------------------------
* Used "if" it is facet.pivot data.
*
* * @param {JSON} as the solr output for facet.pivots.
* @returns {Map} A map associating to each key an array of two elements (total count, and another map),
* or the count.
*/
getPivotCountsMap: function (property) {
return parse_pivots(this.manager.response.facet_counts[property][this.field]);
},

/**
* Used if the facet counts are represented as a JSON object.
*
Expand Down Expand Up @@ -290,4 +335,26 @@ AjaxSolr.AbstractFacetWidget = AjaxSolr.AbstractWidget.extend(
}
});

/*********************************
* Method for parsing pivots output: it works recursively such that at each level
* which has a new pivot the ouput will indicate the counts and then the map of other results.
* It follows the same structure than the solr ourput.
*
* @param {JSON} as the solr output for facet.pivots.
* @returns {Map} A map associating to each key an array of two elements (total count, and another map),
* or the count.
*
* *********************************/
function parse_pivots(data_input){
var output = {}
for (var i = 0, l = data_input.length; i < l; i++) {
if ("pivot" in data_input[i]){
output[data_input[i]["value"]] = [data_input[i]["count"],parse_pivots(data_input[i]["pivot"])];
} else {
output[data_input[i]["value"]] = data_input[i]["count"];
}
}
return output
}
/********************************************************/
}));
44 changes: 44 additions & 0 deletions examples/reuters/css/reuters.css
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ a:hover {
}
#calendar {
width: 160px;
margin-top: 10px;
}
#search_help {
font-size: 80%;
Expand Down Expand Up @@ -158,3 +159,46 @@ font-size: 170%;
a.tagcloud_size_10 {
font-size: 180%;
}

/*histogram*/

div.bar {
display: inline-block;
width: 20px;
height: 75px;
background-color: teal;
margin-right: 2px;
}

bar2 {
background-color: blue;
}

.shared, .bar, .label {
font-size: 8pt;
font-weight: bold;
font-family: Arial, sans-serif;
}
.femalebar, .malebar {
fill: #a7b6c1;
}
.highlight rect.malebar, .highlight rect.femalebar {
fill: #a14538;
}
text.malebar, text.femalebar {
display: none;
}
.highlight text {
display: block;
fill: #000;
}

rect.highlight2{
fill: #a14538;
}

#histogram
{
margin-top: 10px;
}

54 changes: 40 additions & 14 deletions examples/reuters/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,37 @@
<script src="widgets/AutocompleteWidget.js"></script>
<script src="widgets/CountryCodeWidget.js"></script>
<script src="widgets/CalendarWidget.js"></script>
<script src="widgets/HistogramWidget.js"></script>
<script src="widgets/PyramidAges.js"></script> <!--*********************************************************************** -->
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script>

$('CalendarWidget').datepicker({
changeMonth: true,
changeYear: true,
maxDate:0,
onChangeMonthYear: function(year, month, inst){
// set date to 1st on year or month change

// this seems bit janky, but works
//$('#' + inst.id).datepicker( "setDate", month + '/1/' + year );
alert("hohoho");
// Can't I use the instatnce to set the date?

// $(inst).datepicker( "setDate", month + '/1/' + year ); // fails
// inst.datepicker( "setDate", month + '/1/' + year ); // fails
// inst.selectedDay = 1; // fails
// inst.currentDay = 1; // fails
}
});

</script>
</head>
<body>
<div id="wrap">
<div id="header">
<h1>AJAX Solr Demonstration</h1>
<h2>Browse Reuters business news from 1987</h2>
<h1>Meneame database</h1>
<h2>Comments</h2>
</div>

<div class="right">
Expand All @@ -51,21 +76,22 @@ <h2>Search</h2>
<input type="text" id="query" name="query" autocomplete="off">
</ul>

<h2>Top Topics</h2>
<div class="tagcloud" id="topics"></div>

<h2>Top Organisations</h2>
<div class="tagcloud" id="organisations"></div>

<h2>Top Exchanges</h2>
<div class="tagcloud" id="exchanges"></div>

<h2>By Country</h2>
<div id="countries"></div>
<div id="preview"></div>
<h2>Most commun Words</h2>
<div class="tagcloud" id="comment_content"></div>

<h2>Most commun Users</h2>
<div class="tagcloud" id="comment_user_id"></div>

<h2>By Date</h2>
<div id="calendar"></div>

<h2>Histogram</h2>
<div id="histogram"></div>

<!-- ********************************************************************************* -->
<h2>Pyramid age-gender</h2>
<div id="pyramidAges"></div>
<!-- *********************************************************************************-->

<div class="clear"></div>
</div>
Expand Down
46 changes: 33 additions & 13 deletions examples/reuters/js/reuters.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
//var ip = location.hostname;
var ip = "localhost";
var Manager;

(function ($) {

$(function () {
Manager = new AjaxSolr.Manager({
solrUrl: 'http://evolvingweb.ca/solr/reuters/'
solrUrl: 'http://' + ip + ':8983/solr/collection1/'
});
Manager.addWidget(new AjaxSolr.ResultWidget({
id: 'result',
target: '#docs'
}));


Manager.addWidget(new AjaxSolr.PagerWidget({
id: 'pager',
target: '#pager',
Expand All @@ -20,7 +24,8 @@ var Manager;
$('#pager-header').html($('<span></span>').text('displaying ' + Math.min(total, offset + 1) + ' to ' + Math.min(total, offset + perPage) + ' of ' + total));
}
}));
var fields = [ 'topics', 'organisations', 'exchanges' ];
/*var fields = ['comment_id', 'comment_order', 'comment_karma', 'comment_randkey', 'comment_date', 'comment_user_id', 'comment_link_id', 'comment_content', 'comment_votes', 'comment_parent' ];*/
var fields = ['comment_content', 'comment_user_id' ];
for (var i = 0, l = fields.length; i < l; i++) {
Manager.addWidget(new AjaxSolr.TagcloudWidget({
id: fields[i],
Expand All @@ -32,34 +37,49 @@ var Manager;
id: 'currentsearch',
target: '#selection'
}));

Manager.addWidget(new AjaxSolr.AutocompleteWidget({
id: 'text',
target: '#search',
fields: [ 'topics', 'organisations', 'exchanges' ]
fields: [ 'comment_id', 'comment_content']
}));
Manager.addWidget(new AjaxSolr.CountryCodeWidget({
id: 'countries',
target: '#countries',
field: 'countryCodes'

Manager.addWidget(new AjaxSolr.HistogramWidget({
id: 'histogram',
target: '#histogram',
fields: ['comment_date', 'comment_hour']
}));

//************** new widget pyramid pivots ********************

Manager.addWidget(new AjaxSolr.PyramidAges({
id: 'pyramidAges',
target: '#pyramidAges',
field: 'user_sex,user_age',
'facet.pivot': true
}));
// **********************************************************

Manager.addWidget(new AjaxSolr.CalendarWidget({
id: 'calendar',
target: '#calendar',
field: 'date'
field: 'comment_date'
}));

Manager.init();
Manager.store.addByValue('q', '*:*');
var params = {
facet: true,
'facet.field': [ 'topics', 'organisations', 'exchanges', 'countryCodes' ],
'facet.field': ['comment_id', 'comment_order', 'comment_karma', 'comment_randkey', 'comment_date', 'comment_user_id', 'comment_link_id', 'comment_content', 'comment_votes', 'comment_parent', 'comment_hour' ],
'facet.limit': 20,
'facet.mincount': 1,
'f.topics.facet.limit': 50,
'f.countryCodes.facet.limit': -1,
'facet.date': 'date',
'facet.date.start': '1987-02-26T00:00:00.000Z/DAY',
'facet.date.end': '1987-10-20T00:00:00.000Z/DAY+1DAY',
'facet.date': 'comment_date',
'facet.date.start': '2005-12-01T00:00:00.000Z/DAY',
'facet.date.end': '2006-12-31T00:00:00.000Z/DAY+1DAY',
'facet.date.gap': '+1DAY',
//************** new line for pyramid pivot *******************************
'facet.pivot':'user_sex,user_age',
'json.nl': 'map'
};
for (var name in params) {
Expand Down
Loading