Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Expand Down
121 changes: 121 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -776,3 +776,124 @@ 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
}
}
]
}
}
]
}
}
}
)
})