Reputation: 4365
Using this index:
PUT test
{
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"body": {
"type": "text"
},
"subjects": {
"type": "nested",
"properties": {
"id": {
"type": "keyword"
},
"path_id": {
"type": "keyword"
}
}
}
}
}
}
And adding these 3 documents:
POST test/_doc
{
"id": "1",
"body": "topic 1",
"subjects": [
{
"id": "calculus-ii",
"path_id": ["math", "advanced-math", "calculus-ii"]
},
{
"id": "calculus-iii",
"path_id": ["math", "advanced-math", "calculus-iii"]
}
]
}
POST test/_doc
{
"id": "2",
"body": "topic 2",
"subjects": [
{
"id": "calculus-i",
"path_id": ["math", "advanced-math", "calculus-i"]
},
{
"id": "calculus-ii",
"path_id": ["math", "advanced-math", "calculus-ii"]
}
]
}
POST test/_doc
{
"id": "3",
"body": "topic 3",
"subjects": [
{
"id": "equations",
"path_id": ["math", "basic-math", "equations"]
}
]
}
I want to search based on the nested property subjects.path_id
, only documents that have both subjects: calculus-ii
and calculus-iii
.
When I use should
it finds both topics 1 and 2. Which means that the structure of the search input is setup correctly:
GET test/_search
{
"query": {
"nested": {
"path": "subjects",
"query": {
"bool": {
"should": [
{
"terms": {
"subjects.path_id": ["calculus-ii"]
}
},
{
"terms": {
"subjects.path_id": ["calculus-iii"]
}
}
]
}
}
}
}
}
However, when I change should
to either must
or filter
, the result is empty.
How can I change this behavior from OR
to AND
?
Upvotes: 0
Views: 38
Reputation: 4365
The problem seems to be with the nested property. The query worked as expected after changing to:
GET test/_search
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "subjects",
"query": {
"term": {
"subjects.id": "calculus-ii"
}
}
}
},
{
"nested": {
"path": "subjects",
"query": {
"term": {
"subjects.id": "calculus-iii"
}
}
}
}
]
}
}
}
Upvotes: 0