Hi,
I have a place model that it has hours for week day as you see in below, Now I want to write a query to get places that open now (pass hour and minute as separate parameters to query), but don't know how to write a advanced nested query with json and not plugin framework.
Anyone can help me?
{
"_index" : "places",
"_type" : "place",
"_id" : "15a12fg690t81p",
"_score" : 1.0,
"_source" : {
"id" : "15a12fg690t81p",
"name" : "fast-food",
"hours" : [
{
"day" : 0,
"is_overnight" : false,
"start" : "08:00",
"end" : "22:00"
},
{
"day" : 1,
"is_overnight" : false,
"start" : "08:00",
"end" : "22:00"
},
{
"day" : 2,
"is_overnight" : false,
"start" : "08:00",
"end" : "22:00"
},
{
"day" : 3,
"is_overnight" : false,
"start" : "08:00",
"end" : "22:00"
},
{
"day" : 4,
"is_overnight" : false,
"start" : "08:00",
"end" : "22:00"
},
{
"day" : 5,
"is_overnight" : false,
"start" : "08:00",
"end" : "18:00"
}
]
}
}
We now have a range field type that is optimized for this use case.
Example:
// Setup index with new range field type
DELETE test
PUT test
{
"settings": {
"number_of_replicas": 0,
"number_of_shards": 1
},
"mappings": {
"_doc": {
"properties": {
"hours": {
"type": "nested",
"properties": {
"day": {
"type": "integer"
},
"hours": {
"type": "double_range"
}
}
}
}
}
}
}
Add a doc (note use of decimals to express hours - maybe you'd prefer integers to express minuteOfDay)
// Add doc
POST test/_doc/1
{
"id": "15a12fg690t81p",
"name": "fast-food",
"hours": [
{
"day": 0,
"is_overnight": false,
"hours": {
"gte": "8",
"lte": "22.5"
}
},
{
"day": 5,
"is_overnight": false,
"hours": {
"gte": "8",
"lte": "18"
}
}
]
}
Now search - need to use nested and regular term query that denotes a position somewhere in the time range,
POST test/_doc/_search
{
"query": {
"nested": {
"path": "hours",
"query": {
"bool": {
"must": [
{
"match": {
"hours.day": 5
}
},
{
"term":{
"hours.hours":19
}
}
]
}
}
}
}
}
1 Like
system
(system)
Closed
October 25, 2018, 11:00am
3
This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.