Skip to content

Commit

Permalink
fix: text and nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Pomar committed Dec 11, 2023
1 parent 2bbf9b0 commit 4c04696
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 31 deletions.
21 changes: 7 additions & 14 deletions src/faableql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,23 @@ test("multiple fields", (t) => {
test("text", (t) => {
const query = faableQL("this is me");
t.deepEqual(query, {
$and: [
{ $text: { $search: "this" } },
{ $text: { $search: "is" } },
{ $text: { $search: "me" } },
],
$text: { $search: "this is me", $language: "es" },
});
});

test("text quoted", (t) => {
const query = faableQL('"this is me"');
t.deepEqual(query, {
$and: [{ $text: { $search: "this is me" } }],
$text: { $search: "this is me", $language: "es" },
});
});

test("text and labels", (t) => {
const query = faableQL("me label:babies this label:yes");
console.log(JSON.stringify(query, null, 2));
const query = faableQL("label:babies label:yes this is me");
// console.log(JSON.stringify(query, null, 2));

t.deepEqual(query, {
$and: [
{ $text: { $search: "me" } },
{ labels: "babies" },
{ $text: { $search: "this" } },
{ labels: "yes" },
],
$and: [{ labels: "babies" }, { labels: "yes" }],
$text: { $search: "this is me", $language: "es" },
});
});
26 changes: 14 additions & 12 deletions src/faableql.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { create_semantics } from "./grammar";
import { SemanticOptions, create_semantics } from "./grammar";
import { Field } from "./Field";
import { FaableQLError } from "./errors";

export const create_faableql = (valid_fields: Field[]) => (query: string) => {
const { semantics, grammar } = create_semantics(valid_fields);
export const create_faableql =
(valid_fields: Field[], options: SemanticOptions = { searchLang: "es" }) =>
(query: string) => {
const { semantics, grammar } = create_semantics(valid_fields, options);

// Match grammar
const match = grammar.match(query);
// Match grammar
const match = grammar.match(query);

if (!match.succeeded()) {
throw new FaableQLError("❌ Invalid query");
}
//console.log("✅ Valid match");
const ops_dict = semantics(match);
return ops_dict.asMongo;
};
if (!match.succeeded()) {
throw new FaableQLError("❌ Invalid query");
}
//console.log("✅ Valid match");
const ops_dict = semantics(match);
return ops_dict.asMongo;
};
29 changes: 24 additions & 5 deletions src/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { FaableQLError } from "./errors";

const source = String.raw`
FaableQL {
Expr = (FieldExpr | TextExpr)+
TextExpr = simpletext | QuotedText
Exp = FieldExpr* TextExpr?
TextExpr = simpletext+ | QuotedText
simpletext = alnum+
QuotedText = ("\"" alnum+ "\"")
FieldExpr = field operator value
Expand All @@ -19,7 +19,14 @@ FaableQL {

const grammar = ohm.grammar(source);

export const create_semantics = (valid_fields: Field[]) => {
export interface SemanticOptions {
searchLang: string;
}

export const create_semantics = (
valid_fields: Field[],
options: SemanticOptions
) => {
const s = grammar.createSemantics();
s.addAttribute("asMongo", {
FieldExpr(name, operator, value) {
Expand All @@ -45,7 +52,19 @@ export const create_semantics = (valid_fields: Field[]) => {
return { [fieldname]: val };
},
TextExpr(text) {
return { $text: { $search: text.asMongo } };
return {
$text: {
$search: text.isIteration() ? text.asMongo.join(" ") : text.asMongo,
$language: options.searchLang,
},
};
},
Exp(fields, text) {
const merged = fields.children.map((e) => e?.asMongo);
return {
...text.child(0)?.asMongo,
...(merged.length > 0 && { $and: merged }),
};
},
QuotedText(_, text, __) {
return text.sourceString;
Expand All @@ -60,7 +79,7 @@ export const create_semantics = (valid_fields: Field[]) => {
return this.sourceString;
},
_iter(...child) {
return { $and: child.map((e) => e.asMongo) };
return child.map((e) => e.asMongo);
},
_terminal() {
return this.sourceString;
Expand Down

0 comments on commit 4c04696

Please sign in to comment.