Skip to content

Commit

Permalink
Issue 73: Added the option to select or, and, phrase per term in the …
Browse files Browse the repository at this point in the history
…multi match
  • Loading branch information
jettro committed Dec 23, 2014
1 parent 6d65dcb commit c05e97e
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 54 deletions.
17 changes: 12 additions & 5 deletions assets/js/elasticsearch-gui.js
Original file line number Diff line number Diff line change
Expand Up @@ -82527,6 +82527,7 @@ function QueryCtrl($scope, $modal, elastic, aggregateBuilder, queryStorage) {
$scope.query.types = {};
$scope.query.advanced = {};
$scope.query.advanced.searchFields = [];
$scope.query.advanced.newType = 'or';
$scope.query.multiSearch=false;


Expand Down Expand Up @@ -82639,6 +82640,7 @@ function QueryCtrl($scope, $modal, elastic, aggregateBuilder, queryStorage) {
var searchField = {};
searchField.field = $scope.query.advanced.newField;
searchField.text = $scope.query.advanced.newText;
searchField.type = $scope.query.advanced.newType;
$scope.query.advanced.searchFields.push(searchField);
};

Expand Down Expand Up @@ -82687,6 +82689,10 @@ function QueryCtrl($scope, $modal, elastic, aggregateBuilder, queryStorage) {
$scope.query.chosenFields = [];
$scope.query.advanced = {};
$scope.query.advanced.searchFields = [];
$scope.query.advanced.newType = 'or';
$scope.query.advanced.newText = null;
$scope.query.advanced.newField = null;

$scope.query.multiSearch=false;

$scope.changeQuery();
Expand Down Expand Up @@ -82757,12 +82763,11 @@ function QueryCtrl($scope, $modal, elastic, aggregateBuilder, queryStorage) {
query.fields = $scope.query.chosenFields.toString();
}
if ($scope.query.multiSearch && $scope.query.advanced.searchFields.length > 0) {
// Check query type, if and use must else use should
var tree = {};
for (var i = 0; i < $scope.query.advanced.searchFields.length; i++) {
var searchField = $scope.query.advanced.searchFields[i];
var fieldForSearch = $scope.fields[searchField.field];
recurseTree(tree, searchField.field, searchField.text);
recurseTree(tree, searchField.field, searchField.text, searchField.type);
if (fieldForSearch.nestedPath) {
defineNestedPathInTree(tree, fieldForSearch.nestedPath, fieldForSearch.nestedPath);
}
Expand Down Expand Up @@ -82816,7 +82821,8 @@ function QueryCtrl($scope, $modal, elastic, aggregateBuilder, queryStorage) {
if ($scope.query.type === 'phrase') {
matchQuery[fieldName].type = "phrase";
} else {
matchQuery[fieldName].operator = $scope.query.type;
console.log(tree[prop] + '-' + tree['_type_'+prop]);
matchQuery[fieldName].operator = tree['_type_'+prop];
}
boolQuery.bool.must.push({"match": matchQuery});
}
Expand Down Expand Up @@ -82846,17 +82852,18 @@ function QueryCtrl($scope, $modal, elastic, aggregateBuilder, queryStorage) {

}

function recurseTree(tree, newKey, value) {
function recurseTree(tree, newKey, value, type) {
var newKeys = newKey.split(".");

if (newKeys.length > 1) {
if (!tree.hasOwnProperty(newKeys[0])) {
tree[newKeys[0]] = {};
}
recurseTree(tree[newKeys[0]], newKeys.splice(1).join("."), value);
recurseTree(tree[newKeys[0]], newKeys.splice(1).join("."), value, type);
} else {
if (!tree.hasOwnProperty(newKey)) {
tree[newKey] = value;
tree['_type_' + newKey] = type;
}
}
}
Expand Down
22 changes: 11 additions & 11 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.

17 changes: 12 additions & 5 deletions js/controllers/QueryCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function QueryCtrl($scope, $modal, elastic, aggregateBuilder, queryStorage) {
$scope.query.types = {};
$scope.query.advanced = {};
$scope.query.advanced.searchFields = [];
$scope.query.advanced.newType = 'or';
$scope.query.multiSearch=false;


Expand Down Expand Up @@ -126,6 +127,7 @@ function QueryCtrl($scope, $modal, elastic, aggregateBuilder, queryStorage) {
var searchField = {};
searchField.field = $scope.query.advanced.newField;
searchField.text = $scope.query.advanced.newText;
searchField.type = $scope.query.advanced.newType;
$scope.query.advanced.searchFields.push(searchField);
};

Expand Down Expand Up @@ -174,6 +176,10 @@ function QueryCtrl($scope, $modal, elastic, aggregateBuilder, queryStorage) {
$scope.query.chosenFields = [];
$scope.query.advanced = {};
$scope.query.advanced.searchFields = [];
$scope.query.advanced.newType = 'or';
$scope.query.advanced.newText = null;
$scope.query.advanced.newField = null;

$scope.query.multiSearch=false;

$scope.changeQuery();
Expand Down Expand Up @@ -244,12 +250,11 @@ function QueryCtrl($scope, $modal, elastic, aggregateBuilder, queryStorage) {
query.fields = $scope.query.chosenFields.toString();
}
if ($scope.query.multiSearch && $scope.query.advanced.searchFields.length > 0) {
// Check query type, if and use must else use should
var tree = {};
for (var i = 0; i < $scope.query.advanced.searchFields.length; i++) {
var searchField = $scope.query.advanced.searchFields[i];
var fieldForSearch = $scope.fields[searchField.field];
recurseTree(tree, searchField.field, searchField.text);
recurseTree(tree, searchField.field, searchField.text, searchField.type);
if (fieldForSearch.nestedPath) {
defineNestedPathInTree(tree, fieldForSearch.nestedPath, fieldForSearch.nestedPath);
}
Expand Down Expand Up @@ -303,7 +308,8 @@ function QueryCtrl($scope, $modal, elastic, aggregateBuilder, queryStorage) {
if ($scope.query.type === 'phrase') {
matchQuery[fieldName].type = "phrase";
} else {
matchQuery[fieldName].operator = $scope.query.type;
console.log(tree[prop] + '-' + tree['_type_'+prop]);
matchQuery[fieldName].operator = tree['_type_'+prop];
}
boolQuery.bool.must.push({"match": matchQuery});
}
Expand Down Expand Up @@ -333,17 +339,18 @@ function QueryCtrl($scope, $modal, elastic, aggregateBuilder, queryStorage) {

}

function recurseTree(tree, newKey, value) {
function recurseTree(tree, newKey, value, type) {
var newKeys = newKey.split(".");

if (newKeys.length > 1) {
if (!tree.hasOwnProperty(newKeys[0])) {
tree[newKeys[0]] = {};
}
recurseTree(tree[newKeys[0]], newKeys.splice(1).join("."), value);
recurseTree(tree[newKeys[0]], newKeys.splice(1).join("."), value, type);
} else {
if (!tree.hasOwnProperty(newKey)) {
tree[newKey] = value;
tree['_type_' + newKey] = type;
}
}
}
Expand Down
75 changes: 43 additions & 32 deletions partials/query.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div class="col-md-12">
<div class="alert alert-info">
<p>This page is about providing a very basic search and check the query that is created. This way you
can learn about creating queries and the result of those query against the data in your Elasticsearch
can learn about creating queries and the result of those queries against the data in your Elasticsearch
instance.</p>
</div>
</div>
Expand Down Expand Up @@ -94,24 +94,22 @@
<div class="col-md-2">
<span class="text-info">Query config</span>
<button type="button" popover-placement="right"
popover="Here you can configure some additional items on the query. You can change the default OR searching to AND searching and even PHRASE searching. All queries use the Match query type. If you want to do more advanced queries using search by field for instance use another part of this application.
If you select the explain option you'll learn more about the scoring of the returned results. When selecting highlighting we also show the highlighted fields."
popover="Here you can configure some additional items on the query. All queries use the Match query
type. You can select the multi search option, than you can add multiple fields, values to search for
and the type (or, and, phrase). If you select the explain option you'll learn more about the scoring
of the returned results. When selecting highlighting we also show the highlighted fields."
class="btn btn-link"><span class="glyphicon glyphicon-question-sign"></span></button>

</div>
<div class="col-md-9">
<button class="btn btn-info btn-sm" ng-click="resetQuery()" title="Reset all choices"><span
class="glyphicon glyphicon-refresh glyphicon-white"></span></button>
<span class="text-info">Query Type</span>
<label class="radio-inline">
<input type="radio" ng-model="query.type" value="or">OR
</label>
<label class="radio-inline">
<input type="radio" ng-model="query.type" value="and">AND
</label>
<label class="radio-inline">
<input type="radio" ng-model="query.type" value="phrase">PHRASE
</label>
class="glyphicon glyphicon-refresh glyphicon-white"></span> Reset</button>
<button class="btn btn-info btn-sm" ng-click="saveQuery()"><span
class="glyphicon glyphicon-share glyphicon-white"></span> save
</button>
<button class="btn btn-info btn-sm" ng-click="loadQuery()"><span
class="glyphicon glyphicon-download-alt glyphicon-white"></span> load
</button>
<span class="text-info"> Use explain:</span>
<label class="checkbox-inline"><input type="checkbox" ng-model="query.explain"/></label>
<span class="text-info">, Use highlighting:</span>
Expand All @@ -127,38 +125,51 @@
placeholder="Enter search term"/></span>
<button type="submit" class="btn btn-primary" title="Execute query">Query <span
class="glyphicon glyphicon-search glyphicon-white"></span></button>
<button class="btn btn-sm btn-default" ng-click="saveQuery()"><span
class="glyphicon glyphicon-share"></span> save
</button>
<button class="btn btn-sm btn-default" ng-click="loadQuery()"><span
class="glyphicon glyphicon-download-alt"></span> load
</button>
<span class="text-info">Query Type</span>
<label class="radio-inline">
<input type="radio" ng-model="query.type" value="or">OR
</label>
<label class="radio-inline">
<input type="radio" ng-model="query.type" value="and">AND
</label>
<label class="radio-inline">
<input type="radio" ng-model="query.type" value="phrase">PHRASE
</label>
</div>
<div class="col-md-12 alert alert-info" ng-show="query.multiSearch">
<div>
<div class="col-xs-4">
<div class="row">
<div class="col-xs-3">
<select class="form-control" ng-model="query.advanced.newField"
ng-options="key as value.forPrint for (key,value) in fields">
<option value="">-- chose field --</option>
</select>
</div>
<div class="col-xs-4">
<div class="col-xs-3">
<input class="form-control" type="search" ng-model="query.advanced.newText" autofocus="true"
placeholder="Type your search string"/>
</div>
<div class="col-xs-1">
<select class="form-control" ng-model="query.advanced.newType">
<option value="or" selected>OR</option>
<option value="and">AND</option>
<option value="phrase">PHRASE</option>
</select>
</div>
<button class="btn btn-info" ng-click="addSearchField()"><span class="glyphicon glyphicon-plus-sign glyphicon-white"></span> Add</button>
</div>
<div>
<div ng-repeat="searchField in query.advanced.searchFields">
{{searchField.field}} = {{searchField.text}} <button class="btn btn-link" ng-click="removeSearchField($index)"><span
class="glyphicon glyphicon-remove"></span></button>
<div class="row">
<div class="col-md-12">
<div ng-repeat="searchField in query.advanced.searchFields">
{{searchField.field}} = {{searchField.text}} ({{searchField.type}}) <button class="btn btn-link" ng-click="removeSearchField($index)"><span
class="glyphicon glyphicon-remove"></span></button>
</div>
</div>
<br/>

<div>
</div>
<div class="row">
<div class="col-md-12">
<button class="btn btn-primary" type="submit"><span class="glyphicon glyphicon-search glyphicon-white"></span> Search</button>
<button class="btn btn-sm btn-default" ng-click="saveQuery()"><span class="glyphicon glyphicon-share"></span> save</button>
<button class="btn btn-sm btn-default" ng-click="loadQuery()"><span class="glyphicon glyphicon-download-alt"></span> load</button>
<button class="btn btn-sm btn-default" ng-click="saveQuery()"><span class="glyphicon glyphicon-share"></span> Save</button>
<button class="btn btn-sm btn-default" ng-click="loadQuery()"><span class="glyphicon glyphicon-download-alt"></span> Load</button>
</div>
</div>
</div>
Expand Down

0 comments on commit c05e97e

Please sign in to comment.