How to search multi_index container using composite key with date ranges

Hi,
I am having syntax issues and wish to search a bmi container by the
composite index defined below. I have read about boost::lambdas and
understand how they work. I simply have gotten stuck in the syntax to
perform the get on the index and how to create the tuple(s) containing the
proper objects.
I want to search such that searchKey1="key1" and searchKey2="key2"
and ValidFrom is <= '2013-03-01' and ValidTo >= '2013-02-08'. Can anyone
help me please?
Many Thanks!!
Jeff R.
bmi=boost::multi_index
bg=boost::gregorian
typedef bmi::multi_index_container
TestCache;

Jeff
Hi,
I am having syntax issues and wish to search a bmi container by the composite index defined below. I have read about boost::lambdas and understand how they work. I simply have gotten stuck in the syntax to perform the get on the index and how to create the tuple(s) containing the proper objects.
I want to search such that searchKey1=”key1” and searchKey2=”key2” and ValidFrom is <= '2013-03-01' and ValidTo >= '2013-02-08'. Can anyone help me please?
Hi Jeff, There's no way to retrieve this range with a single lookup operation, basically because the elements satisfying the four criteria are not sorted to appear adjacently in the idx_test index. The closest you can get is * lookup for elements with searchKey1=”key1”, searchKey2=”key2”, ValidFrom <= '2013-03-01' * filter out from the latter range those elements with ValidTo >= '2013-02-08' which would look roughly like this (not tested): TestCache tc; ... date valid_to=date(from_string("2013-02-08")); for( auto it= tc.lower_bound(boost:make_tuple("key1","key2")), it_end=tc.upper_bound( boost:make_tuple("key1","key2",from_string("2013-03-01"))), it!=it_end;++it){ if(it->ValidTo>=valid_to){...} } HTH, Joaquín M López Muñoz Telefónica Digital

Hi Joaquín,
That did the trick and helps explain the relationship across the composite
index. Many thanks!
The solution did raise a question concerning the lower/upper iterators being
used in this example. I expected to search getting iterators from
referencing the index, but these iterators are obtained from the container
level. Is this possible because there is only one index defined? If 2 or
more indexes were defined on this container would the iterators used here
work the same?
I apologize for asking what seems I should be able to get from docs - I've
read them twice and the concepts are starting to sink in but not yet
solidified. Multi_index is a very impressive creation!
Thanks again,
Jeff
-----Original Message-----
From: Boost-users [mailto:boost-users-bounces@lists.boost.org] On Behalf Of
Joaquin M Lopez Munoz
Sent: Monday, February 11, 2013 2:30 AM
To: boost-users@lists.boost.org
Subject: Re: [Boost-users]How to search multi_index container using
composite key with date ranges
Jeff
Hi,
I am having syntax issues and wish to search a bmi container by the composite index defined below. I have read about boost::lambdas and understand how they work. I simply have gotten stuck in the syntax to perform the get on the index and how to create the tuple(s) containing the proper objects.
I want to search such that searchKey1=key1 and searchKey2=key2 and ValidFrom is <= '2013-03-01' and ValidTo >= '2013-02-08'. Can anyone help me please?
Hi Jeff, There's no way to retrieve this range with a single lookup operation, basically because the elements satisfying the four criteria are not sorted to appear adjacently in the idx_test index. The closest you can get is * lookup for elements with searchKey1=key1, searchKey2=key2, ValidFrom <= '2013-03-01' * filter out from the latter range those elements with ValidTo >= '2013-02-08' which would look roughly like this (not tested): TestCache tc; ... date valid_to=date(from_string("2013-02-08")); for( auto it= tc.lower_bound(boost:make_tuple("key1","key2")), it_end=tc.upper_bound( boost:make_tuple("key1","key2",from_string("2013-03-01"))), it!=it_end;++it){ if(it->ValidTo>=valid_to){...} } HTH, Joaquín M López Muñoz Telefónica Digital _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Jeff
The solution did raise a question concerning the lower/upper iterators being used in this example. I expected to search getting iterators from referencing the index, but these iterators are obtained from the container level. Is this possible because there is only one index defined? If 2 or more indexes were defined on this container would the iterators used here work the same?
Regardless of the number of indices, the "container level" interface corresponds with that of the first index. This is mentioned in several places across the documentation, for instance at the end of http://www.boost.org/libs/multi_index/doc/tutorial/basics.html#multiple_sort
[...] Multi_index is a very impressive creation!
It was a lot of fun to write it. Glad it's useful to you. Joaquín M López Muñoz Telefónica Digital
participants (2)
-
Jeff
-
Joaquin M Lopez Munoz