Skip to content

Commit

Permalink
Issue #78 improvement of finding the terms for a document
Browse files Browse the repository at this point in the history
  • Loading branch information
jettro committed Jul 28, 2015
1 parent b91afea commit de82f00
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 92 deletions.
69 changes: 39 additions & 30 deletions assets/js/elasticsearch-gui.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! elasticsearch-gui - v1.2.1 - 2015-07-25
/*! elasticsearch-gui - v1.2.1 - 2015-07-28
* https://github.com/jettro/elasticsearch-gui
* Copyright (c) 2015 ; Licensed */
(function(window, document, undefined) {'use strict';
Expand Down Expand Up @@ -93447,6 +93447,11 @@ function ($scope, elastic, configuration, aggregateBuilder, $modal, queryStorage
};

$scope.init = function () {
// elastic.indexes(function (data) {
// // just to initialize the indices in the service
// // TODO would be better to move this to the service itself
// });

elastic.fields([], [], function (data) {
$scope.fields = data;
if (!$scope.configure.title) {
Expand Down Expand Up @@ -93480,7 +93485,7 @@ function ($scope, elastic, configuration, aggregateBuilder, $modal, queryStorage
var query = {};
query.index = "";
query.body = {};
query.fields = $scope.configure.title + "," + $scope.configure.description;
// query.fields = $scope.configure.title + "," + $scope.configure.description;

query.size = $scope.pageSize;
query.from = ($scope.currentPage - 1) * $scope.pageSize;
Expand Down Expand Up @@ -93636,8 +93641,9 @@ function ($scope, elastic, configuration, aggregateBuilder, $modal, queryStorage
};

$scope.showAnalysis = function (index, type, id) {
elastic.documentTerms(index, type, id, $scope.fields, function (result) {
$scope.tokensPerField = result;
$scope.tokensPerField = {"id": index+type+id};
elastic.documentTerms(index, type, id, function (result) {
$scope.tokensPerField.tokens = result;
});
};

Expand Down Expand Up @@ -94183,6 +94189,7 @@ serviceModule.factory('elastic', ['esFactory', 'configuration', '$q', '$rootScop
this.changeServerAddress = function (serverAddress) {
serverUrl = serverAddress;
es = createEsFactory();
this.indexes();
};

this.obtainServerAddress = function () {
Expand Down Expand Up @@ -94233,7 +94240,9 @@ serviceModule.factory('elastic', ['esFactory', 'configuration', '$q', '$rootScop
}
}
activeIndexes = indices;
callback(indices);
if (callback) {
callback(indices);
}
});
};

Expand Down Expand Up @@ -94303,34 +94312,31 @@ serviceModule.factory('elastic', ['esFactory', 'configuration', '$q', '$rootScop
});
};

this.documentTerms = function (index, type, id, fields, callback) {
es.get({"index": index, "type": type, "id": id}).then(function (result) {
var actions = [];
var analyzedFields = [];
for (var field in fields) {
if (fields[field].type === "string") {
var sourceField = field;
if (field.indexOf(".") > -1) {
sourceField = field.substr(0, field.indexOf("."));
}
var text = result._source[sourceField];
if (text) {
analyzedFields.push({"field": field, "value": text});
actions.push(es.indices.analyze({"field": field, "text": text, "index": index, "format": "text"}));
}
this.documentTerms = function (index, type, id, callback) {
es.termvector(
{
"index": index,
"type": type,
"id": id,
"body":{
"fields":["*"],
"field_statistics": false,
"term_statistics": true
}
}

$q.all(actions).then(function (results) {
var i = 0;
while (i < analyzedFields.length) {
analyzedFields[i].tokens = results[i].tokens;
i++;
})
.then(function(result) {
var fieldTerms = {};
if (result.term_vectors) {
angular.forEach(result.term_vectors, function(value,key) {
var terms = [];
angular.forEach(value.terms, function(term, termKey) {
terms.push(termKey);
});
fieldTerms[key] = terms;
});
callback(fieldTerms);
}
callback(analyzedFields);
}, logErrors, function (notify) {
});
});
};

this.fields = function (selectedIndex, selectedType, callback) {
Expand Down Expand Up @@ -94546,6 +94552,9 @@ serviceModule.factory('elastic', ['esFactory', 'configuration', '$q', '$rootScop
var broadcastError = function(error) {
$rootScope.$broadcast('msg:notification', 'error', error.message);
};

// just to initialize the indices
this.indexes();
}

return new ElasticService(esFactory, configuration, $q, $rootScope);
Expand Down
6 changes: 3 additions & 3 deletions assets/js/elasticsearch-gui.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/js/elasticsearch-gui.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->

<script src="assets/js/elasticsearch-gui.min.js"></script>
<script src="assets/js/elasticsearch-gui.js"></script>

</body>
</html>
12 changes: 9 additions & 3 deletions javascript/controllers/SearchCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ function ($scope, elastic, configuration, aggregateBuilder, $modal, queryStorage
};

$scope.init = function () {
// elastic.indexes(function (data) {
// // just to initialize the indices in the service
// // TODO would be better to move this to the service itself
// });

elastic.fields([], [], function (data) {
$scope.fields = data;
if (!$scope.configure.title) {
Expand Down Expand Up @@ -60,7 +65,7 @@ function ($scope, elastic, configuration, aggregateBuilder, $modal, queryStorage
var query = {};
query.index = "";
query.body = {};
query.fields = $scope.configure.title + "," + $scope.configure.description;
// query.fields = $scope.configure.title + "," + $scope.configure.description;

query.size = $scope.pageSize;
query.from = ($scope.currentPage - 1) * $scope.pageSize;
Expand Down Expand Up @@ -216,8 +221,9 @@ function ($scope, elastic, configuration, aggregateBuilder, $modal, queryStorage
};

$scope.showAnalysis = function (index, type, id) {
elastic.documentTerms(index, type, id, $scope.fields, function (result) {
$scope.tokensPerField = result;
$scope.tokensPerField = {"id": index+type+id};
elastic.documentTerms(index, type, id, function (result) {
$scope.tokensPerField.tokens = result;
});
};

Expand Down
55 changes: 29 additions & 26 deletions javascript/services/ElasticService.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ serviceModule.factory('elastic', ['esFactory', 'configuration', '$q', '$rootScop
this.changeServerAddress = function (serverAddress) {
serverUrl = serverAddress;
es = createEsFactory();
this.indexes();
};

this.obtainServerAddress = function () {
Expand Down Expand Up @@ -58,7 +59,9 @@ serviceModule.factory('elastic', ['esFactory', 'configuration', '$q', '$rootScop
}
}
activeIndexes = indices;
callback(indices);
if (callback) {
callback(indices);
}
});
};

Expand Down Expand Up @@ -128,34 +131,31 @@ serviceModule.factory('elastic', ['esFactory', 'configuration', '$q', '$rootScop
});
};

this.documentTerms = function (index, type, id, fields, callback) {
es.get({"index": index, "type": type, "id": id}).then(function (result) {
var actions = [];
var analyzedFields = [];
for (var field in fields) {
if (fields[field].type === "string") {
var sourceField = field;
if (field.indexOf(".") > -1) {
sourceField = field.substr(0, field.indexOf("."));
}
var text = result._source[sourceField];
if (text) {
analyzedFields.push({"field": field, "value": text});
actions.push(es.indices.analyze({"field": field, "text": text, "index": index, "format": "text"}));
}
this.documentTerms = function (index, type, id, callback) {
es.termvector(
{
"index": index,
"type": type,
"id": id,
"body":{
"fields":["*"],
"field_statistics": false,
"term_statistics": true
}
}

$q.all(actions).then(function (results) {
var i = 0;
while (i < analyzedFields.length) {
analyzedFields[i].tokens = results[i].tokens;
i++;
})
.then(function(result) {
var fieldTerms = {};
if (result.term_vectors) {
angular.forEach(result.term_vectors, function(value,key) {
var terms = [];
angular.forEach(value.terms, function(term, termKey) {
terms.push(termKey);
});
fieldTerms[key] = terms;
});
callback(fieldTerms);
}
callback(analyzedFields);
}, logErrors, function (notify) {
});
});
};

this.fields = function (selectedIndex, selectedType, callback) {
Expand Down Expand Up @@ -371,6 +371,9 @@ serviceModule.factory('elastic', ['esFactory', 'configuration', '$q', '$rootScop
var broadcastError = function(error) {
$rootScope.$broadcast('msg:notification', 'error', error.message);
};

// just to initialize the indices
this.indexes();
}

return new ElasticService(esFactory, configuration, $q, $rootScope);
Expand Down
55 changes: 27 additions & 28 deletions partials/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -143,39 +143,38 @@ <h3>Results</h3>
max-size="maxSize" ng-change="changePage()" previous-text="&lsaquo;" next-text="&rsaquo;"
first-text="&laquo;" last-text="&raquo;"></pagination>

<div class="well" ng-show="tokensPerField">
<table>
<thead>
<tr>
<th>Field</th>
<th>Value</th>
<th>Terms</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="field in tokensPerField">
<td>{{field.field}}</td>
<td>{{field.value}}</td>
<td><span ng-repeat="token in field.tokens">&quot;{{token.token}}&quot;&nbsp;</span></td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="3">
<button class="btn btn-default" ng-click="tokensPerField=undefined">Close</button>
</td>
</tr>
</tfoot>
</table>
</div>

<div ng-repeat="result in results.hits">
<div><a ng-click="showAnalysis(result._index,result._type,result._id)">Show terms</a>&nbsp;<span
class="text-info">{{result.fields[configure.title]}}</span><span class="text-warning"
class="text-info">{{result._source[configure.title]}}</span><span class="text-warning"
ng-show="search.details"> index: {{result._index}} - score: {{result._score}} - type: {{result._type}}</span>
</div>
<div class="well-small">
{{result.fields[configure.description]}}
{{result._source[configure.description]}}
</div>
<div class="well" ng-if="result._index+result._type+result._id == tokensPerField.id">
<table class="table table-condensed">
<thead>
<tr>
<th>Field</th>
<th>Value</th>
<th>Terms</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="(key,value) in tokensPerField.tokens">
<td>{{key}}</td>
<td>{{result._source[key]}}</td>
<td><div style="float:left" ng-repeat="token in value">&quot;{{token}}&quot;&nbsp;</div></td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="3">
<button class="btn btn-default" ng-click="tokensPerField.id=undefined">Close</button>
</td>
</tr>
</tfoot>
</table>
</div>
</div>
<br/>
Expand Down

0 comments on commit de82f00

Please sign in to comment.