Navigation Menu

Skip to content

Commit

Permalink
Throw an error when the field is unsearchable
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Aug 24, 2012
1 parent 293fbb6 commit e9846d3
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 54 deletions.
5 changes: 4 additions & 1 deletion lib/api/2011-02-01/search.js
Expand Up @@ -103,9 +103,10 @@ function translateQueryToBooleanQuery(query) {
return "'" + query.replace(/(['\\])/g, '\\$1') + "'";
}

var dummyRid = '000000000000000000000000000000000000000000000000000000000000000';

exports.createHandler = function(context) {
return function(request, response) {
var dummyRid = '000000000000000000000000000000000000000000000000000000000000000';
var startedAt = new Date();
var domain = new Domain(request, context);
var query = request.query.q || '';
Expand Down Expand Up @@ -154,6 +155,8 @@ exports.createHandler = function(context) {
var errorData = { rid: dummyRid };
if (error.message == 'validation error') {
errorData.messages = [error.data];
} else if (error.message == 'unsearchable field') {
errorData.message = 'unsearchable field '+error.fieldName;
} else {
errorData.message = 'Invalid bq value: ' + (error.message || error);
}
Expand Down
123 changes: 70 additions & 53 deletions lib/bq-translator.js
Expand Up @@ -31,24 +31,33 @@ BooleanQueryTranslator.prototype = {
expression = this.translateExpression();
}
if (this.offset != this.query.length) {
this.throwTranslateError("garbages exist after valid boolean query");
this.throwSyntaxError("garbages exist after valid boolean query");
}
return expression;
},
throwTranslateError: function(detail) {
var message = "";
message += "<";
throwInternalError: function(message) {
var error = new Error('internal error');
error.data = message;
throw error;
},
throwSyntaxError: function(message) {
var detail = "";
detail += "<";
if (this.offset == this.query.length) {
message += this.query;
message += "||";
detail += this.query;
detail += "||";
} else {
message += this.query.substring(0, this.offset);
message += "|" + this.query[this.offset] + "|";
message += this.query.substring(this.offset + 1);
detail += this.query.substring(0, this.offset);
detail += "|" + this.query[this.offset] + "|";
detail += this.query.substring(this.offset + 1);
}
message += ">";
message += ": " + detail;
throw new Error(message);
detail += ">";

this.throwValidationError(
'CS-InvalidMatchSetExpression',
'[WARNING] Syntax error in match set expression: ' +
message + ' ' + detial
);
},
throwValidationError: function(code, message) {
var error = new Error('validation error');
Expand All @@ -59,6 +68,11 @@ BooleanQueryTranslator.prototype = {
};
throw error;
},
throwUnsearchableError: function(fieldName) {
var error = new Error('unsearchable field');
error.fieldName = fieldName;
throw error;
},
skipSpaces: function() {
for (; this.offset < this.query.length; this.offset++) {
if (this.query[this.offset] != " ") {
Expand All @@ -68,7 +82,7 @@ BooleanQueryTranslator.prototype = {
},
translateGroup: function() {
if (this.query[this.offset] != "(") {
this.throwTranslateError("open parenthesis is missing");
this.throwSyntaxError("open parenthesis is missing");
}

this.offset++;
Expand All @@ -86,8 +100,8 @@ BooleanQueryTranslator.prototype = {
expression = this.translateExpression();
this.skipSpaces();
if (this.query[this.offset] != ")") {
this.throwTranslateError("close parenthesis is missing: " +
"operator:<label>");
this.throwSyntaxError("close parenthesis is missing: " +
"operator:<label>");
}
this.offset++;
break;
Expand All @@ -105,19 +119,19 @@ BooleanQueryTranslator.prototype = {
break;
default:
this.offset = operatorEndOffset;
this.throwTranslateError("unknown operator: <" + operator + ">");
this.throwSyntaxError("unknown operator: <" + operator + ">");
break;
}
return expression;
} else if (character == ")") {
this.throwTranslateError("operator is missing");
this.throwSyntaxError("operator is missing");
} else {
this.throwTranslateError("invalid operator character: " +
"<" + character + ">");
this.throwSyntaxError("invalid operator character: " +
"<" + character + ">");
}
}

this.throwTranslateError("close parenthesis is missing");
this.throwSyntaxError("close parenthesis is missing");
},
translateGroupField: function() {
var field = "";
Expand All @@ -131,25 +145,25 @@ BooleanQueryTranslator.prototype = {
this.skipSpaces();
var character = this.query[this.offset];
if (character != ")") {
this.throwTranslateError("a garbage character after value: " +
"<" + character + ">");
this.throwSyntaxError("a garbage character after value: " +
"<" + character + ">");
}
this.offset++;
return expression;
} else if (character == ")") {
if (field.length == 0) {
this.throwTranslateError("field is missing");
this.throwSyntaxError("field is missing");
} else {
this.throwTranslateError("field value is missing: " +
"field:<" + field + ">");
this.throwSyntaxError("field value is missing: " +
"field:<" + field + ">");
}
} else {
this.throwTranslateError("invalid field character: " +
"<" + character + ">");
this.throwSyntaxError("invalid field character: " +
"<" + character + ">");
}
}

this.throwTranslateError("close parenthesis is missing: operator:<field>");
this.throwSyntaxError("close parenthesis is missing: operator:<field>");
},
translateGroupFilter: function() {
var field = "";
Expand All @@ -163,25 +177,25 @@ BooleanQueryTranslator.prototype = {
this.skipSpaces();
var character = this.query[this.offset];
if (character != ")") {
this.throwTranslateError("a garbage character after value: " +
"<" + character + ">");
this.throwSyntaxError("a garbage character after value: " +
"<" + character + ">");
}
this.offset++;
return expression;
} else if (character == ")") {
if (field.length == 0) {
this.throwTranslateError("field is missing");
this.throwSyntaxError("field is missing");
} else {
this.throwTranslateError("field value is missing: " +
"field:<" + field + ">");
this.throwSyntaxError("field value is missing: " +
"field:<" + field + ">");
}
} else {
this.throwTranslateError("invalid field character: " +
"<" + character + ">");
this.throwSyntaxError("invalid field character: " +
"<" + character + ">");
}
}

this.throwTranslateError("close parenthesis is missing: operator:<filter>");
this.throwSyntaxError("close parenthesis is missing: operator:<filter>");
},
translateGroupSetOperation: function(label, setOperator) {
var expressions = [];
Expand All @@ -195,8 +209,8 @@ BooleanQueryTranslator.prototype = {
}
}

this.throwTranslateError("close parenthesis is missing: " +
"operator:<" + label + ">");
this.throwSyntaxError("close parenthesis is missing: " +
"operator:<" + label + ">");
},
translateExpression: function() {
if (this.query[this.offset] == "(") {
Expand All @@ -215,7 +229,7 @@ BooleanQueryTranslator.prototype = {
field += character;
}
if (this.query[this.offset] != ":") {
this.throwTranslateError("field value separator is missing");
this.throwSyntaxError("field value separator is missing");
}
this.offset++;
}
Expand All @@ -227,11 +241,11 @@ BooleanQueryTranslator.prototype = {
return this.translateExpressionValueUnsignedInteger(field);
}

this.throwTranslateError("invalid value: field:<" + field + ">");
this.throwSyntaxError("invalid value: field:<" + field + ">");
},
translateExpressionValueString: function(field) {
if (this.query[this.offset] != "'") {
this.throwTranslateError("open single quote for string value is missing");
this.throwSyntaxError("open single quote for string value is missing");
}
this.offset++;

Expand Down Expand Up @@ -275,15 +289,15 @@ BooleanQueryTranslator.prototype = {
} else if (character == "\\") {
this.offset++;
if (this.offset == this.query.length) {
this.throwTranslateError("escaped character is missing " +
"in string value");
this.throwSyntaxError("escaped character is missing " +
"in string value");
}
character = this.query[this.offset];
value += character;
} else if (character == "\"") {
if (value.length > 0) {
this.throwTranslateError("operator is missing: " +
"keyword:<" + value + ">");
this.throwSyntaxError("operator is missing: " +
"keyword:<" + value + ">");
}
tokens.push(this.translateExpressionValueStringPhrase(field));
this.offset--;
Expand All @@ -292,7 +306,7 @@ BooleanQueryTranslator.prototype = {
}
}

this.throwTranslateError("close single quote for string value is missing");
this.throwSyntaxError("close single quote for string value is missing");
},
translateExpressionValueStringKeyword: function(field, value) {
var operator;
Expand All @@ -309,7 +323,7 @@ BooleanQueryTranslator.prototype = {
},
translateExpressionValueStringPhrase: function(field) {
if (this.query[this.offset] != "\"") {
this.throwTranslateError("open double quote for phrase value is missing");
this.throwSyntaxError("open double quote for phrase value is missing");
}

this.offset += 1;
Expand All @@ -319,7 +333,7 @@ BooleanQueryTranslator.prototype = {
if (character == "\\") {
this.offset++;
if (this.offset == this.query.length) {
this.throwTranslateError("escaped character is missing in phrase");
this.throwSyntaxError("escaped character is missing in phrase");
}
character = this.query[this.offset];
value += character;
Expand All @@ -339,7 +353,7 @@ BooleanQueryTranslator.prototype = {
}
}

this.throwTranslateError("close double quote for phrase is missing");
this.throwSyntaxError("close double quote for phrase is missing");
},
translateExpressionValueUnsignedInteger: function(field) {
var is_range = false;
Expand Down Expand Up @@ -367,7 +381,7 @@ BooleanQueryTranslator.prototype = {
if (is_range) {
var expressions = [];
if (min.length == 0 && max.length == 0) {
this.throwTranslateError("both min and max are missing");
this.throwSyntaxError("both min and max are missing");
}
if (min.length > 0) {
expressions.push(this.constructBinaryOperation(field, ">=", min));
Expand Down Expand Up @@ -396,10 +410,10 @@ BooleanQueryTranslator.prototype = {
return field + " " + operator + " " + value;
} else {
if (!this.defaultFieldNames) {
this.throwTranslateError("default fields are missing");
this.throwInternalError("default fields are missing");
}
if (this.defaultFieldNames.length == 0) {
this.throwTranslateError("no default field");
this.throwInternalError("no default field");
}
var expressions = this.defaultFieldNames.map(function (field) {
return this.constructBinaryOperation(field, operator, value);
Expand All @@ -417,13 +431,16 @@ BooleanQueryTranslator.prototype = {
var field;
if (this.domain && fieldName) {
field = this.domain.getIndexField(fieldName);
if (!field.exists())
if (!field.exists()) {
this.throwValidationError(
'CS-UnknownFieldInMatchExpression',
'Field \'' + fieldName + '\' is not defined in the metadata for this ' +
'collection. All fields used in the match expression must be ' +
'defined in the metadata.'
);
} else if (!field.searchEnabled) {
this.throwUnsearchableError(fieldName);
}
}
if (!field)
field = new IndexField(fieldName).setType("text");
Expand Down

0 comments on commit e9846d3

Please sign in to comment.