Navigation Menu

Skip to content

Commit

Permalink
q: support phrase term
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Aug 3, 2012
1 parent b1985d8 commit ecbf310
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/q-translator.js
Expand Up @@ -34,6 +34,27 @@ QueryTranslator.prototype = {
}
return this.defaultField + ":'" + escapeTerm(term) + "'";
},
translatePhraseTerm: function() {
if (this.query[this.offset] != '"') {
this.thorwTranslateError("phrase must start with <\">");
}
this.offset++;
var phrase = "";
for (; this.offset < this.query.length; this.offset++) {
var character = this.query[this.offset];
if (character == '"') {
this.offset++;
return "'\"" + phrase + "\"'";
}

if (character == "\\") {
this.offset++;
character = this.query[this.offset];
}
phrase += character;
}
this.thorwTranslateError("phrase is unterminated");
},
translateTerm: function() {
this.skipSpaces(this.query, this);
if (this.query[this.offset] == '"') {
Expand Down
23 changes: 23 additions & 0 deletions test/q-translator.test.js
Expand Up @@ -23,6 +23,24 @@ function testIndividualTerm(label, individualTerm,
});
}

function testPhraseTerm(label, phraseTerm,
expectedOffset, expectedBooleanQuery) {
test('phrase term: ' + label + ': ' +
'<' + phraseTerm + '> -> <' + expectedBooleanQuery + '>', function() {
var translator = new QueryTranslator(phraseTerm);
translator.defaultField = "field";
var actualBooleanQuery = translator.translatePhraseTerm();
assert.deepEqual({
booleanQuery: actualBooleanQuery,
offset: translator.offset
},
{
booleanQuery: expectedBooleanQuery,
offset: expectedOffset
});
});
}

function testTerm(label, term, expectedOffset, expectedBooleanQuery) {
test('term: ' + label + ': ' +
'<' + term + '> -> <' + expectedBooleanQuery + '>', function() {
Expand Down Expand Up @@ -50,6 +68,11 @@ suite('QueryTranslator', function() {
"let's".length,
"field:'let\\'s'");

testPhraseTerm("no special character",
'"star wars" luke',
'"star wars"'.length,
"'\"star wars\"'");

testTerm("a term",
" star wars",
" star".length,
Expand Down

0 comments on commit ecbf310

Please sign in to comment.