I'm new to Elasticsearch. I have a question about creating conditional search queries between nested and parent types.
I have a mapping below named "homes"
PUT /homes/_mapping
{
"properties": {
"name": { "type": "text" },
"ownerID": { "type": "integer" },
"status": { "type": "text" }, // active or inactive
"minDays": { "type": "integer" },
"dayData": {
"type": "nested",
"properties": {
"date": { "type": "date" },
"minDays": { "type": "integer" },
"closed": { "type": "boolean" }
}
}
}
}
Homeowners can define custom minDays
for homes day by day. Of course, there is a default value for minDays
in the parent.
I want to make a search query by date range
For example, users can search in homes by start and end date. If there is dayData
defined with minDays
on the start date, it will take the value here, if not, the parent value will be taken for filtering.
How can I achieve that?
Update
I have documents like below
For example if user is searching avaibility for the dates between "2021-01-01" and 2021-01-04. Total day count is "3" for requested dates. We need search for homes which has minDays value greater or equeal than "3".
In this situation:
- I need to get docs with status is "active"
- The min day value for _doc/1 is "2", cause there is a custom dayData value with minDays. So this property won't be available.
- The min day value for _doc/2 is "4". We get default value from parent, cause there is no custom dayData value. This property will be available.
- If they dayData includes a "closed" value with "true" between searched dates, home won't be available.
I have tried several search queries to achieve that but failed for this context.
Thank you in advance.
PUT homes/_doc/1
{
"name" : "Home Test",
"status" : "active",
"ownerID": 1,
"minDays" : "3",
"dayData" : [
{
"date" : "2021-01-01",
"minDays" : 2
},
{
"date" : "2021-01-04",
"minDays" : 5
},
{
"date" : "2021-01-05",
"closed": true
}
]
}
PUT homes/_doc/2
{
"name" : "Home Test 2",
"status" : "active",
"ownerID": 2,
"minDays" : "4",
"dayData" : [
{
"date" : "2021-05-02",
"minDays" : 2
},
{
"date" : "2021-05-03",
"minDays" : 2
},
{
"date" : "2021-05-03",
"minDays" : 2,
"closed" :true
},
{
"date" : "2021-05-10",
"minDays" : 5
}
]
}