Skip to content

Commit

Permalink
fix invalid query nesting functionality
Browse files Browse the repository at this point in the history
bodybuilder now generates valid nested queries

fixes #142
fixes #162
  • Loading branch information
ferronrsmith committed Jan 29, 2018
1 parent f14b245 commit 849ff11
Showing 2 changed files with 130 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
@@ -134,7 +134,14 @@ export function pushQuery (existing, boolKey, type, ...args) {
existing[boolKey].push(
{[type]: Object.assign(buildClause(...args), nested.filter.bool)}
)
} else {
} else if (
type === 'bool' &&
_.has(nested, 'query.bool')
) {
existing[boolKey].push(
{[type]: Object.assign(buildClause(...args), nested.query.bool)}
)
} else {
// Usual case
existing[boolKey].push(
{[type]: Object.assign(buildClause(...args), nested)}
122 changes: 122 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -706,3 +706,125 @@ test('bodybuilder | minimum_should_match query and filter', (t) => {
}
})
})

test('bodybuilder | Nested bool query with must #162', (t) => {
t.plan(1)

const result = bodyBuilder()
.query('bool', b => b.orQuery('match', 'title', 'Solr').orQuery('match', 'title', 'Elasticsearch'))
.query('match', 'authors', 'clinton gormely')
.notQuery('match', 'authors', 'radu gheorge')
.build()

t.deepEqual(result,
{
query: {
bool: {
must: [
{
bool: {
should: [
{
match: {
title: "Solr"
}
},
{
match: {
title: "Elasticsearch"
}
}
]
}
},
{
match: {
authors: "clinton gormely"
}
}
],
must_not: [
{
match: {
authors: "radu gheorge"
}
}
]
}
}
}
)
})

test('bodybuilder | Invalid nested bool query with more "query" #142', (t) => {
t.plan(1)

const body = bodyBuilder()

body.query('bool', b => b
.query('term', 'field1', 1)
.query('term', 'field2', 2)
.orQuery('term', 'field3', 3))
body.query('bool', b => b
.query('term', 'field4', 10)
.query('term', 'field5', 20)
.orQuery('term', 'field6', 30))

t.deepEqual(body.build(),
{
query: {
bool: {
must: [
{
bool: {
must: [
{
term: {
field1: 1
}
},
{
term: {
field2: 2
}
}
],
should: [
{
term: {
field3: 3
}
}
]
}
},
{
bool: {
must: [
{
term: {
field4: 10
}
},
{
term: {
field5: 20
}
}
],
should: [
{
term: {
field6: 30
}
}
]
}
}
]
}
}
}
)
})

0 comments on commit 849ff11

Please sign in to comment.