Skip to content

Commit

Permalink
Merge pull request #163 from danpaz/bug/query_nesting
Browse files Browse the repository at this point in the history
fixes invalid query nesting generation
  • Loading branch information
ferronrsmith committed Jan 30, 2018
2 parents 1ebcf85 + 400c71c commit 2402956
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 1 deletion.
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
}
}
]
}
}
]
}
}
}
)
})

0 comments on commit 2402956

Please sign in to comment.