Ahh I see. Thank you Shay and Clint.
For the nested-documents, does the sub-doc have its own _uid that's
different from the _uid of the parent?
Also if I want to update just 1 specific sub-doc, can I do that or I'd
have to update the entire parent doc together with all the sub-docs?
For example if a nested-doc has:
dates: [
{ startdate: "2011-08-01", enddate: "2011-08-05" },
{ startdate: "2011-08-07", enddate: "2011-08-09" }
]
and someone books the time block 2011-08-03 to 2011-08-04. In this
case I'd need to update the sub-doc
{ startdate: "2011-08-01", enddate: "2011-08-05" }
to 2 different sub-docs:
{ startdate: "2011-08-01", enddate: "2011-08-02" }
{ startdate: "2011-08-05", enddate: "2011-08-05" }
Is there a way to just "update" that specific sub-doc? Or I'd have to
update the entire document by specifying:
dates: [
{ startdate: "2011-08-01", enddate: "2011-08-02" },
{ startdate: "2011-08-05", enddate: "2011-08-05" },
{ startdate: "2011-08-07", enddate: "2011-08-09" }
]
Andy
On Aug 2, 3:10 pm, Clinton Gormley cl...@traveljury.com wrote:
Hi Andy
- I suppose a simple way is to index the class description multiple
times, once for each available block. So for the class "1-day
Argentine Tango entry level class in Brooklyn" above, 3 documents
would be indexed. They all have the text "1-day Argentine Tango entry
level class in Brooklyn" as description. The 1st document would have
startdate: "2011-8-5"
enddate:"2011-8-11"
The 2nd, 3rd documents would also have the appropriate startdate &
enddate.
Before 0.17, you wouldn't be able to do what you're wanting in a single
doc.
For instance, if you have the field "dates", with type "object":
dates: [
{ startdate: "2011-08-01", enddate: "2011-08-05" },
{ startdate: "2011-08-10", enddate: "2011-08-15" }
]
and you tried a range query like:
{ bool: {
must: [
{ range: { startdate: { 'gte': '2011-08-06' }},
{ range: { enddate: { 'lte': '2011-08-09' }}
]
}}
... it would have found this doc incorrectly, because it just looks at
ANY startdate and ANY enddate in the doc. It doesn't correlate
startdate and enddate within the same sub-doc.
However, as of 0.17, kimchy has performed a small miracle with the
introduction of "nested" docs.
A nested doc is pretty much the same as an "object" except that "nested"
means that it is treated as an independent document in its own right.
Which means that you CAN correlate fields within a single sub-doc.
To use nested docs, you have to specify them up front in the mapping:http://www.elasticsearch.org/guide/reference/mapping/nested-type.html
eg :
curl -XPUT 'http://127.0.0.1:9200/foo/?pretty=1' -d '
{
"mappings" : {
"class" : {
"properties" : {
"name" : { "type" : "string" },
"dates" : {
"include_in_root" : 1,
"type" : "nested",
"properties" : {
"enddate" : { "type" : "date" },
"startdate" : { "type" : "date" }
}
},
}
}
}}
'
Then you can do a nested query or filter like:
curl -XGET 'http://127.0.0.1:9200/foo/class/_search?pretty=1' -d '
{
"query" : {
"nested" : {
"path" : "dates",
"query" : {
"bool" : {
"must" : [
{ "range" : {
"dates.enddate" : { "gte" : "2011-08-05" }
}},
{ "range" : {
"dates.startdate" : { "gte" : "2011-08-01" }
}}
]
}
}
}
}}
'
http://www.elasticsearch.org/guide/reference/query-dsl/nested-query.htmlhttp://www.elasticsearch.org/guide/reference/query-dsl/nested-filter....
And when I want to search for classes between the 8/7 and 8/9, I could
just add the filter:
"range" : {
"startdate" : {
"lte" : "2011-8-7"
}
"enddate" : {
"gte" : "2011-8-9"
}
}
Note - your query won't work - you can't specify multiple fields under
the "range" key
- How do I index and filter on datetime such as "8/5/11 2pm"? Is
there a "datetime" field type?
Have a look athttp://www.elasticsearch.org/guide/reference/mapping/date-format.html
clint