Skip to content

Commit

Permalink
Fix post format (issue blevesearch#7)
Browse files Browse the repository at this point in the history
Closes blevesearch#7

#Hacktoberfest #Coinpaprika
  • Loading branch information
oskarwojciski committed Oct 26, 2018
1 parent 727a9c2 commit 0804a7e
Showing 1 changed file with 115 additions and 118 deletions.
233 changes: 115 additions & 118 deletions static/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function SearchCtrl($scope, $http, $routeParams, $log, $sce) {

$scope.minShouldOptions = [];
for (var i = 0; i <= 50; i++) {
$scope.minShouldOptions[i] = i;
$scope.minShouldOptions[i] = i;
}

var resetSchema = function() {
Expand All @@ -21,11 +21,11 @@ function SearchCtrl($scope, $http, $routeParams, $log, $sce) {
};

var resetForm = function() {
$scope.clauses = [];
$scope.phraseTerms = [];
$scope.size = "10";
$scope.minShould = "0";
resetSchema();
$scope.clauses = [];
$scope.phraseTerms = [];
$scope.size = "10";
$scope.minShould = "0";
resetSchema();
};

resetForm();
Expand All @@ -43,13 +43,13 @@ function SearchCtrl($scope, $http, $routeParams, $log, $sce) {

$scope.searchTerm = function() {
$http.post('/api/search', {
"size": 10,
"explain": true,
"highlight":{},
"query": {
"term": $scope.term,
"field": $scope.field,
}
"size": 10,
"explain": true,
"highlight":{},
"query": {
"term": $scope.term,
"field": $scope.field,
}
}).
success(function(data) {
$scope.processResults(data);
Expand Down Expand Up @@ -178,18 +178,18 @@ function SearchCtrl($scope, $http, $routeParams, $log, $sce) {
};

$scope.expl = function(explanation) {
rv = "" + $scope.roundScore(explanation.value) + " - " + explanation.message;
rv = rv + "<ul>";
for(var i in explanation.children) {
child = explanation.children[i];
rv = rv + "<li>" + $scope.expl(child) + "</li>";
}
rv = rv + "</ul>";
return rv;
rv = "" + $scope.roundScore(explanation.value) + " - " + explanation.message;
rv = rv + "<ul>";
for(var i in explanation.children) {
child = explanation.children[i];
rv = rv + "<li>" + $scope.expl(child) + "</li>";
}
rv = rv + "</ul>";
return rv;
};

$scope.roundScore = function(score) {
return Math.round(score*1000)/1000;
return Math.round(score*1000)/1000;
};

$scope.roundTook = function(took) {
Expand All @@ -201,21 +201,21 @@ function SearchCtrl($scope, $http, $routeParams, $log, $sce) {
roundMs = Math.round(took / (1000*1000));
return "" + roundMs/1000 + "s";
}
};
};

$scope.removePhraseTerm = function(index) {
$scope.phraseTerms.splice(index, 1);
};

$scope.addPhraseTerm = function() {
if($scope.phraseTerm === "") {
//$scope.errorMessage = "Phrase term cannot be empty";
//return;
$scope.phraseTerms.push(null);
//$scope.errorMessage = "Phrase term cannot be empty";
//return;
$scope.phraseTerms.push(null);
}else {

$scope.phraseTerms.push($scope.phraseTerm);
}
$scope.phraseTerms.push($scope.phraseTerm);
}

// reset form
delete $scope.errorMessage;
Expand All @@ -226,61 +226,58 @@ function SearchCtrl($scope, $http, $routeParams, $log, $sce) {
$scope.errorMessage = null;
$scope.results = data;
for(var i in $scope.results.hits) {
hit = $scope.results.hits[i];
hit.roundedScore = $scope.roundScore(hit.score);
hit.explanationString = $scope.expl(hit.explanation);
hit.explanationStringSafe = $sce.trustAsHtml(hit.explanationString);
for(var ff in hit.fragments) {
fragments = hit.fragments[ff];
newFragments = [];
for(var ffi in fragments) {
fragment = fragments[ffi];
safeFragment = $sce.trustAsHtml(fragment);
newFragments.push(safeFragment);
}
hit.fragments[ff] = newFragments;
hit = $scope.results.hits[i];
hit.roundedScore = $scope.roundScore(hit.score);
hit.explanationString = $scope.expl(hit.explanation);
hit.explanationStringSafe = $sce.trustAsHtml(hit.explanationString);
for(var ff in hit.fragments) {
fragments = hit.fragments[ff];
newFragments = [];
for(var ffi in fragments) {
fragment = fragments[ffi];
safeFragment = $sce.trustAsHtml(fragment);
newFragments.push(safeFragment);
}
hit.fragments[ff] = newFragments;
}
}
$scope.results.roundTook = $scope.roundTook(data.took);
};

$scope.searchPhrase = function() {
delete $scope.results;
if($scope.phraseTerms.length < 1) {
$scope.errorMessage = "Query requires at least one term";
return;
$scope.errorMessage = "Query requires at least one term";
return;
}
var requestBody = {
"query": {
"terms": [],
"boost": 1.0,
},
"highlight":{},
explain: true,
size: parseInt($scope.size, 10)
"query": {
"terms": [],
"boost": 1.0,
},
"highlight":{},
explain: true,
size: parseInt($scope.size, 10)
};
var terms = [];
for(var i in $scope.phraseTerms) {
var term = $scope.phraseTerms[i];
if (term !== null) {
var termQuery = {
"term": term,
"field": $scope.phraseField,
"boost": 1.0,
};
requestBody.query.terms.push(termQuery);
} else {
requestBody.query.terms.push(null);
}

var term = $scope.phraseTerms[i];
if (term !== null) {
terms.push(term)
}
}
if (terms.length > 0) {
requestBody.query.terms.push(terms);
requestBody.query.field = $scope.phraseField;
}

$http.post('/api/search', requestBody).
success(function(data) {
$scope.processResults(data);
}).
error(function(data, code) {
$scope.errorMessage = data;
return;
$scope.errorMessage = data;
return;
});
};

Expand All @@ -291,30 +288,30 @@ function SearchCtrl($scope, $http, $routeParams, $log, $sce) {
$scope.addClause = function() {

if($scope.clauseTerm === "") {
$scope.errorMessage = "Clause term cannot be empty";
return;
$scope.errorMessage = "Clause term cannot be empty";
return;
}

if($scope.clauseOccur === "") {
$scope.errorMessage = "Select clause occur";
return;
$scope.errorMessage = "Select clause occur";
return;
}

if($scope.clauseField === "") {
$scope.errorMessage = "Select a field";
return;
$scope.errorMessage = "Select a field";
return;
}

if($scope.clauseBoost === "") {
$scope.errorMessage = "Clause boost cannot be empty";
return;
$scope.errorMessage = "Clause boost cannot be empty";
return;
}

clause = {
"term": $scope.clauseTerm,
"occur": $scope.clauseOccur,
"field": $scope.clauseField,
"boost": $scope.clauseBoost
"term": $scope.clauseTerm,
"occur": $scope.clauseOccur,
"field": $scope.clauseField,
"boost": $scope.clauseBoost
};

$scope.clauses.push(clause);
Expand All @@ -327,66 +324,66 @@ function SearchCtrl($scope, $http, $routeParams, $log, $sce) {
$scope.searchBoolean = function() {
delete $scope.results;
if($scope.clauses.length < 1) {
$scope.errorMessage = "Query requires at least one clause";
return;
$scope.errorMessage = "Query requires at least one clause";
return;
}
var requestBody = {
"query": {
"must": {
"conjuncts":[],
"boost": 1.0,
},
"should":{
"disjuncts":[],
"boost": 1.0,
"min": parseInt($scope.minShould, 10)
},
"must_not": {
"disjuncts": [],
"boost": 1.0,
},
"boost": 1.0,
"query": {
"must": {
"conjuncts":[],
"boost": 1.0,
},
"should":{
"disjuncts":[],
"boost": 1.0,
"min": parseInt($scope.minShould, 10)
},
explain: true,
"highlight":{},
size: parseInt($scope.size, 10)
"must_not": {
"disjuncts": [],
"boost": 1.0,
},
"boost": 1.0,
},
explain: true,
"highlight":{},
size: parseInt($scope.size, 10)
};
for(var i in $scope.clauses) {
var clause = $scope.clauses[i];
var termQuery = {
"term": clause.term,
"field": clause.field,
"boost": parseFloat(clause.boost),
};
switch(clause.occur) {
case "MUST":
requestBody.query.must.conjuncts.push(termQuery);
break;
case "SHOULD":
requestBody.query.should.disjuncts.push(termQuery);
break;
case "MUST NOT":
requestBody.query.must_not.disjuncts.push(termQuery);
break;
}
var clause = $scope.clauses[i];
var termQuery = {
"term": clause.term,
"field": clause.field,
"boost": parseFloat(clause.boost),
};
switch(clause.occur) {
case "MUST":
requestBody.query.must.conjuncts.push(termQuery);
break;
case "SHOULD":
requestBody.query.should.disjuncts.push(termQuery);
break;
case "MUST NOT":
requestBody.query.must_not.disjuncts.push(termQuery);
break;
}
}
if (requestBody.query.must.conjuncts.length === 0) {
delete requestBody.query.must;
delete requestBody.query.must;
}
if (requestBody.query.should.disjuncts.length === 0) {
delete requestBody.query.should;
delete requestBody.query.should;
}
if (requestBody.query.must_not.disjuncts.length === 0) {
delete requestBody.query.must_not;
delete requestBody.query.must_not;
}

$http.post('/api/search', requestBody).
success(function(data) {
$scope.processResults(data);
}).
error(function(data, code) {
$scope.errorMessage = data;
return;
$scope.errorMessage = data;
return;
});
};

Expand Down

0 comments on commit 0804a7e

Please sign in to comment.