Skip to content

Instantly share code, notes, and snippets.

@tpoljak
Created August 29, 2011 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tpoljak/1178344 to your computer and use it in GitHub Desktop.
Save tpoljak/1178344 to your computer and use it in GitHub Desktop.
date_range_query_on_child_types
1.Create index
curl -XPUT 'http://localhost:9200/index1'
2. Create parent mapping
curl -XPUT 'http://localhost:9200/index1/myparent/_mapping' -d '{
"parent": {
"properties": {
"fieldname": {
"omit_term_freq_and_positions": false,
"index": "analyzed",
"omit_norms": false,
"store": "yes",
"format": "MMM/dd/yyyy HH:mm:ss",
"type": "date"
}
}
}
}'
3. Create child mapping (with date prop)
curl -XPUT 'http://localhost:9200/index1/date1/_mapping' -d '
{
"date1" : {
"_parent" : {"type" : "myparent" },
"properties" : {
"fieldname": {
"omit_term_freq_and_positions": false,
"index": "analyzed",
"omit_norms": false,
"store": "yes",
"format": "MMM/dd/yyyy HH:mm:ss",
"type": "date"
}
}
}
}
'
4. Insert parent document
curl -XPUT 'http://localhost:9200/index1/myparent/1' -d '{"fieldname" :"Aug/28/2011 18:26:58"}'
5. Insert child document
curl -XPUT 'http://localhost:9200/index1/date1/1?parent=1' -d '{
"date1" : {
"fieldname" :"Aug/28/2011 18:26:58"
}
}
'
6. Search query with date range search on child type (document's date inside from/to range)
curl -XGET 'http://localhost:9200/index1/myparent/_search?search_type=dfs_query_then_fetch' -d '{"query":{ "has_child" : {"type" : "date1", "query" :{ "range" :{ "fieldname" : { "from" : "Aug/20/2011 00:00:00", "to" : "Sep/20/2011 00:00:00","include_lower" : true,"include_upper" : true,"boost" : 1.0 } } }} }}'
RESULT (as you can see there is a match id=1):
{"took":18,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":1.0,"hits":[{"_index":"index1","_type":"myparent","_id":"1","_score":1.0, "_source" : {"fieldname" :"Aug/28/2011 18:26:58"}}]}}
7. Search query with date range search on child type (document's date outside from/to range)
curl -XGET 'http://localhost:9200/index1/myparent/_search?search_type=dfs_query_then_fetch' -d '{"query":{ "has_child" : {"type" : "date1", "query" :{ "range" :{ "fieldname" : { "from" : "Aug/30/2011 00:00:00", "to" : "Sep/20/2011 00:00:00","include_lower" : true,"include_upper" : true,"boost" : 1.0 } } }} }}'
RESULT (as you can see there is no match -> hits:0):
{"took":15,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment