Skip to content

Commit

Permalink
feat: support regexp equal query
Browse files Browse the repository at this point in the history
  • Loading branch information
kelp404 committed Jan 2, 2023
1 parent 6afea93 commit f3ef8c9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 65 deletions.
97 changes: 42 additions & 55 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,14 +610,22 @@
switch (queryCell.operation) {
case QueryOperation.equal:
if (queryCell.value != null) {
return {
match: {
[`${queryCell.dbField}`]: {
query: queryCell.value,
operator: 'and'
if (queryCell.value.constructor === RegExp) {
return {
regexp: {
[`${queryCell.dbField}`]: queryCell.value.toString().slice(1, -1)
}
}
};
};
} else {
return {
match: {
[`${queryCell.dbField}`]: {
query: queryCell.value,
operator: 'and'
}
}
};
}
} else {
return {
bool: {
Expand All @@ -632,18 +640,30 @@
break;
case QueryOperation.unequal:
if (queryCell.value != null) {
return {
bool: {
must_not: {
match: {
[`${queryCell.dbField}`]: {
query: queryCell.value,
operator: 'and'
if (queryCell.value.constructor === RegExp) {
return {
bool: {
must_not: {
regexp: {
[`${queryCell.dbField}`]: queryCell.value.toString().slice(1, -1)
}
}
}
}
};
};
} else {
return {
bool: {
must_not: {
match: {
[`${queryCell.dbField}`]: {
query: queryCell.value,
operator: 'and'
}
}
}
}
};
}
} else {
return {
bool: {
Expand Down Expand Up @@ -740,52 +760,19 @@
case QueryOperation.like:
value = utils.bleachRegexWords(queryCell.value);
return {
bool: {
should: [
{
match: {
[`${queryCell.dbField}`]: {
query: queryCell.value,
operator: 'and'
}
}
},
{
regexp: {
[`${queryCell.dbField}`]: `.*${value}.*`
}
}
]
regexp: {
[`${queryCell.dbField}`]: `.*${value}.*`
}
};
case QueryOperation.unlike:
value = utils.bleachRegexWords(queryCell.value);
return {
bool: {
minimum_should_match: 2,
should: [
{
bool: {
must_not: {
match: {
[`${queryCell.dbField}`]: {
query: queryCell.value,
operator: 'and'
}
}
}
}
},
{
bool: {
must_not: {
regexp: {
[`${queryCell.dbField}`]: `.*${value}.*`
}
}
}
must_not: {
regexp: {
[`${queryCell.dbField}`]: `.*${value}.*`
}
]
}
}
};
}
Expand Down
30 changes: 20 additions & 10 deletions src/lib/query.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -444,23 +444,33 @@ module.exports = class Query
switch queryCell.operation
when QueryOperation.equal
if queryCell.value?
match:
"#{queryCell.dbField}":
query: queryCell.value
operator: 'and'
if queryCell.value.constructor is RegExp
regexp:
"#{queryCell.dbField}": queryCell.value.toString().slice(1, -1)
else
match:
"#{queryCell.dbField}":
query: queryCell.value
operator: 'and'
else
bool:
must_not:
exists:
field: queryCell.dbField
when QueryOperation.unequal
if queryCell.value?
bool:
must_not:
match:
"#{queryCell.dbField}":
query: queryCell.value
operator: 'and'
if queryCell.value.constructor is RegExp
bool:
must_not:
regexp:
"#{queryCell.dbField}": queryCell.value.toString().slice(1, -1)
else
bool:
must_not:
match:
"#{queryCell.dbField}":
query: queryCell.value
operator: 'and'
else
bool:
must:
Expand Down

0 comments on commit f3ef8c9

Please sign in to comment.