Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only Individual Terms are supported for now.
- Loading branch information
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // -*- indent-tabs-mode: nil; js2-basic-offset: 2 -*- | ||
| /* | ||
| QueryTranslator translates Queries into Boolean Queries in Amazon | ||
| CloudSearch. | ||
| Expression Syntax for Queries: | ||
| http://docs.amazonwebservices.com/cloudsearch/latest/developerguide/Search.Requests.html#Search.ReqParams | ||
| Expression Syntax for Boolean Queries: | ||
| http://docs.amazonwebservices.com/cloudsearch/latest/developerguide/Search.Requests.html#Search.MatchSetExpression | ||
| FIXME: Returns null if the given query is not supported. | ||
| Shuold raise. | ||
| */ | ||
|
|
||
|
|
||
| function QueryTranslator() { | ||
| } | ||
|
|
||
| function throwTranslateError(query, context, detail) { | ||
| var message = ""; | ||
| message += "<"; | ||
| message += query.substring(0, context.offset); | ||
| message += "|" + query[context.offset] + "|"; | ||
| message += query.substring(context.offset + 1); | ||
| message += ">"; | ||
| message += ": " + detail; | ||
| throw new Error(message); | ||
| } | ||
|
|
||
| QueryTranslator.prototype = { | ||
| translateIndividualTerm: function(query, context) { | ||
| var term = ''; | ||
| for (; context.offset < query.length; context.offset++) { | ||
| if (/[^ \+\-\|]/.test(query[context.offset])) { | ||
| term += query[context.offset]; | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
| return context.defaultField + ":'" + term + "'"; | ||
| }, | ||
| skipSpaces: function(query, context) { | ||
| for (; context.offset < query.length; context.offset++) { | ||
| if (query[context.offset] != " ") { | ||
| return; | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| exports.QueryTranslator = QueryTranslator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // -*- indent-tabs-mode: nil; js2-basic-offset: 2 -*- | ||
|
|
||
| var utils = require('./test-utils'); | ||
| var assert = require('chai').assert; | ||
|
|
||
| var QueryTranslator = require('../lib/q-translator').QueryTranslator; | ||
|
|
||
| function testIndividualTerm(label, individualTerm, expectedBooleanQuery, | ||
| expectedOffset) { | ||
| test('term: ' + label + ': ' + | ||
| '<' + individualTerm + '> -> <' + expectedBooleanQuery + '>', function() { | ||
| var translator = new QueryTranslator(); | ||
| var context = { | ||
| offset: 0, | ||
| defaultField: 'field' | ||
| }; | ||
| var actualBooleanQuery = | ||
| translator.translateIndividualTerm(individualTerm, context); | ||
| assert.deepEqual({ | ||
| booleanQuery: actualBooleanQuery, | ||
| offset: context.offset | ||
| }, | ||
| { | ||
| booleanQuery: expectedBooleanQuery, | ||
| offset: expectedOffset | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| suite('QueryTranslator', function() { | ||
| testIndividualTerm("an individual term", | ||
| "star wars", | ||
| "field:'star'", | ||
| "star".length); | ||
| }); |