1
0
Fork 0
mirror of synced 2024-07-08 07:46:10 +12:00

Add not contains option to lucene query builder

This commit is contained in:
Peter Clement 2021-08-25 14:05:00 +01:00
parent f000d44e39
commit 05568295b5
4 changed files with 27 additions and 5 deletions

View file

@ -86,7 +86,6 @@
let arr = field.constraints.inclusion let arr = field.constraints.inclusion
let newArr = [] let newArr = []
newArr.push(arr) newArr.push(arr)
console.log(newArr)
field.constraints.inclusion = newArr field.constraints.inclusion = newArr
} }
tables.saveField({ tables.saveField({

View file

@ -35,7 +35,10 @@ export const OperatorOptions = {
value: "contains", value: "contains",
label: "Contains", label: "Contains",
}, },
NotContains: {
value: "notContains",
label: "Does Not Contain",
}
} }
export const getValidOperatorsForType = type => { export const getValidOperatorsForType = type => {
@ -61,7 +64,7 @@ export const getValidOperatorsForType = type => {
} else if (type === "options") { } else if (type === "options") {
return [Op.Equals, Op.NotEquals, Op.Empty, Op.NotEmpty] return [Op.Equals, Op.NotEquals, Op.Empty, Op.NotEmpty]
} else if (type === "array") { } else if (type === "array") {
return [Op.Equals, Op.NotEquals, Op.Empty, Op.NotEmpty, Op.Contains] return [Op.Contains, Op.NotContains]
} else if (type === "boolean") { } else if (type === "boolean") {
return [Op.Equals, Op.NotEquals, Op.Empty, Op.NotEmpty] return [Op.Equals, Op.NotEquals, Op.Empty, Op.NotEmpty]
} else if (type === "longform") { } else if (type === "longform") {

View file

@ -18,6 +18,7 @@ class QueryBuilder {
empty: {}, empty: {},
notEmpty: {}, notEmpty: {},
contains: {}, contains: {},
notContains: {},
...base, ...base,
} }
this.limit = 50 this.limit = 50
@ -110,6 +111,10 @@ class QueryBuilder {
return this return this
} }
addNotContains(key, value) {
this.query.notContains[key] = value
return this
}
/** /**
* Preprocesses a value before going into a lucene search. * Preprocesses a value before going into a lucene search.
@ -232,6 +237,20 @@ class QueryBuilder {
} }
if (this.query.notContains) {
build(this.query.notContains, (key, value) => {
if (!value) {
return null
}
let opts = []
value.forEach(val => opts.push(`!${key}.${val}:${builder.preprocess(val, allPreProcessingOpts)}`))
const joined = opts.join(' AND ')
return joined
})
}
return query return query
} }
@ -273,7 +292,7 @@ const runQuery = async (url, body) => {
method: "POST", method: "POST",
}) })
const json = await response.json() const json = await response.json()
console.log(json)
let output = { let output = {
rows: [], rows: [],
} }

View file

@ -11,7 +11,8 @@ export const buildLuceneQuery = filter => {
notEqual: {}, notEqual: {},
empty: {}, empty: {},
notEmpty: {}, notEmpty: {},
contains: {} contains: {},
notContains: {},
} }
if (Array.isArray(filter)) { if (Array.isArray(filter)) {
filter.forEach(expression => { filter.forEach(expression => {