Skip to content

Commit 2402956

Browse files
authored
Merge pull request #163 from danpaz/bug/query_nesting
fixes invalid query nesting generation
2 parents 1ebcf85 + 400c71c commit 2402956

File tree

2 files changed

+129
-1
lines changed

2 files changed

+129
-1
lines changed

src/utils.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,14 @@ export function pushQuery (existing, boolKey, type, ...args) {
134134
existing[boolKey].push(
135135
{[type]: Object.assign(buildClause(...args), nested.filter.bool)}
136136
)
137-
} else {
137+
} else if (
138+
type === 'bool' &&
139+
_.has(nested, 'query.bool')
140+
) {
141+
existing[boolKey].push(
142+
{[type]: Object.assign(buildClause(...args), nested.query.bool)}
143+
)
144+
} else {
138145
// Usual case
139146
existing[boolKey].push(
140147
{[type]: Object.assign(buildClause(...args), nested)}

test/index.js

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,3 +776,124 @@ test('bodybuilder | minimum_should_match query and filter', (t) => {
776776
}
777777
})
778778
})
779+
780+
test('bodybuilder | Nested bool query with must #162', (t) => {
781+
t.plan(1)
782+
783+
const result = bodyBuilder()
784+
.query('bool', b => b.orQuery('match', 'title', 'Solr').orQuery('match', 'title', 'Elasticsearch'))
785+
.query('match', 'authors', 'clinton gormely')
786+
.notQuery('match', 'authors', 'radu gheorge')
787+
.build()
788+
789+
t.deepEqual(result,
790+
{
791+
query: {
792+
bool: {
793+
must: [
794+
{
795+
bool: {
796+
should: [
797+
{
798+
match: {
799+
title: "Solr"
800+
}
801+
},
802+
{
803+
match: {
804+
title: "Elasticsearch"
805+
}
806+
}
807+
]
808+
}
809+
},
810+
{
811+
match: {
812+
authors: "clinton gormely"
813+
}
814+
}
815+
],
816+
must_not: [
817+
{
818+
match: {
819+
authors: "radu gheorge"
820+
}
821+
}
822+
]
823+
}
824+
}
825+
}
826+
)
827+
})
828+
829+
test('bodybuilder | Invalid nested bool query with more "query" #142', (t) => {
830+
t.plan(1)
831+
832+
const body = bodyBuilder()
833+
834+
body.query('bool', b => b
835+
.query('term', 'field1', 1)
836+
.query('term', 'field2', 2)
837+
.orQuery('term', 'field3', 3))
838+
body.query('bool', b => b
839+
.query('term', 'field4', 10)
840+
.query('term', 'field5', 20)
841+
.orQuery('term', 'field6', 30))
842+
843+
t.deepEqual(body.build(),
844+
{
845+
query: {
846+
bool: {
847+
must: [
848+
{
849+
bool: {
850+
must: [
851+
{
852+
term: {
853+
field1: 1
854+
}
855+
},
856+
{
857+
term: {
858+
field2: 2
859+
}
860+
}
861+
],
862+
should: [
863+
{
864+
term: {
865+
field3: 3
866+
}
867+
}
868+
]
869+
}
870+
},
871+
{
872+
bool: {
873+
must: [
874+
{
875+
term: {
876+
field4: 10
877+
}
878+
},
879+
{
880+
term: {
881+
field5: 20
882+
}
883+
}
884+
],
885+
should: [
886+
{
887+
term: {
888+
field6: 30
889+
}
890+
}
891+
]
892+
}
893+
}
894+
]
895+
}
896+
}
897+
}
898+
)
899+
})

0 commit comments

Comments
 (0)