couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Robert Samuel Newson <rnew...@apache.org>
Subject Re: question about "complex" range queries
Date Thu, 03 Apr 2014 08:30:14 GMT

Sorry, but the value is not included in the ordering of the views, what you’re seeing there
is an artifact of how multiple emits from the same document (where the emitted key is the
same *and* the tie-breaking _id is also the same) are handled.

B.

On 3 Apr 2014, at 01:52, Rian R. Maloney <rian.maloney@yahoo.com> wrote:

> This is NOT what I am seeing ( just trying to understand how this example is not sorted
on value). Here is my test case:
> 
> 1) create database called test
> 2) create 1 doc with _id = test and a field called stuff ="this is test data"
> 3)  create the following map:
> 
> function(doc) {
> 
>   for( var i = 1;i < 5;i++ )
>   {
>      data = {};
>      data.sortkey = i;
>      data.stuff = doc.stuff;
>      emit( doc._id, data );
>   }
>   for( var i = 4;i > 0;i-- )
>   {
>      data = {};
>      data.sortkey = i;
>      data.stuff = doc.stuff;
>      emit( doc._id, data );
>   }
> 
> 
> Result:
> {"total_rows":8,"offset":0,"rows":[
> {"id":"test","key":"test","value":{"sortkey":1,"stuff":"this is test data"}},
> {"id":"test","key":"test","value":{"sortkey":1,"stuff":"this is test data"}},
> {"id":"test","key":"test","value":{"sortkey":2,"stuff":"this is test data"}},
> {"id":"test","key":"test","value":{"sortkey":2,"stuff":"this is test data"}},
> {"id":"test","key":"test","value":{"sortkey":3,"stuff":"this is test data"}},
> {"id":"test","key":"test","value":{"sortkey":3,"stuff":"this is test data"}},
> {"id":"test","key":"test","value":{"sortkey":4,"stuff":"this is test data"}},
> {"id":"test","key":"test","value":{"sortkey":4,"stuff":"this is test data"}}
> ]}
> 
> But if value was not included in the sort, it would be:
> 
> {"total_rows":8,"offset":0,"rows":[
> {"id":"test","key":"test","value":{"sortkey":1,"stuff":"this is test data"}},
> {"id":"test","key":"test","value":{"sortkey":2,"stuff":"this is test data"}},
> {"id":"test","key":"test","value":{"sortkey":3,"stuff":"this is test data"}},
> {"id":"test","key":"test","value":{"sortkey":4,"stuff":"this is test data"}},{"id":"test","key":"test","value":{"sortkey":1,"stuff":"this
is test data"}},{"id":"test","key":"test","value":{"sortkey":2,"stuff":"this is test data"}},{"id":"test","key":"test","value":{"sortkey":3,"stuff":"this
is test data"}},{"id":"test","key":"test","value":{"sortkey":4,"stuff":"this is test data"}},
> ]}
> On Wednesday, April 2, 2014 6:07 PM, Ryan Ramage <ryan.ramage@gmail.com> wrote:
> 
> The emitted value does not affect the sort.
> 
> 
> If you wanted to keep sorting add your sort field in an array key, so move
> from
> 
> emit(doc.key, doc.value)
> 
> 
> to
> 
> emit([doc.key, doc.value], doc.value)
> 
> 
> 
> On Wed, Apr 2, 2014 at 4:57 PM, <rian.maloney@yahoo.com> wrote:
> 
>> Thanks Robert. Let me clarify. Is the value included in the sort? I have
>> 10 entries all with identical keys. The behavior I am seeing indicates the
>> contents of the emitted value is included in the sort criteria and if so
>> can I count on this? Bottom line I am trying to sort on 2 fields, one is a
>> key and the other is the beginning of the value. I also don't want to sort
>> the results in a list due to performance constraints
>> 
>> Thanks
>> 
>> On Apr 2, 2014, at 5:45 PM, Robert Samuel Newson <rnewson@apache.org>
>> wrote:
>> 
>>> A view is a single dimension index, ordered by the full key in
>> accordance with the rules enumerated here:
>> https://wiki.apache.org/couchdb/View_collation#Collation_Specification
>>> 
>>> For example;
>>> 
>>> 9 sorts after false
>>> 999 sorts after 9
>>> "foo" sorts after 999
>>> ["foo"] sorts after "foo"
>>> ["foo", "bar"] sorts after ["foo"]
>>> 
>>> B.
>>> 
>>> On 2 Apr 2014, at 23:29, rian.maloney@yahoo.com wrote:
>>> 
>>>> I have a quick question related to this. What exactly is sorted? My
>> test cases indicated the key AND the entire value is sorted as if it was
>> one big string.
>>>> 
>>>> Thanks
>>>> Rian
>>>> 
>>>> On Apr 2, 2014, at 2:31 AM, Nick North <north.n@gmail.com> wrote:
>>>> 
>>>>> It doesn't work quite like this. There is a single order across all
>>>>> possible keys, including both simple and complex keys, as described
>>>>> here<https://wiki.apache.org/couchdb/View_collation>.
>>>>> In the case of keys that are lists, the two lists are compared element
>> by
>>>>> element and the sort order is the sort order of the first unequal
>> elements.
>>>>> 
>>>>> In your example, if a key has its first element between a1 and a2 (and
>> a1
>>>>> and a2 are different), then the second element will not be inspected
at
>>>>> all, so it does not matter whether it is between b1 and b2 or not. In
>> fact
>>>>> the second element will only be inspected if the first element is
>> either a1
>>>>> or a2.
>>>>> 
>>>>> This is usually the behaviour we want. For example, dates are often
>>>>> represented as lists of [year, month, day]. Then you can pull out all
>> the
>>>>> documents in a date range by specifying start date and end date as
>> startkey
>>>>> and endkey. For example, if these are [2012, 4, 5] and [2014, 8, 3]
>>>>> respectively, we want to include [2013, 3, 6] in the output even
>> though its
>>>>> second element does not lie between the second elements of the keys.
>>>>> 
>>>>> Nick
>>>>> 
>>>>> 
>>>>> On 2 April 2014 03:56, Schroiff, Klaus <KlausS@fast.au.fujitsu.com>
>> wrote:
>>>>> 
>>>>>> Hi,
>>>>>> 
>>>>>> Let's assume that I have the following view function:
>>>>>> 
>>>>>> function(doc) { emit([a, b], whatever) } }
>>>>>> 
>>>>>> In the query I'm running something like
>>>>>> startkey=[a1, b1] & endkey=[a2,b2]
>>>>>> Thus there're TWO explicit ranges here - a1->a2 and b1->b2.
>>>>>> 
>>>>>> What is the expected result ?
>>>>>> 
>>>>>> My understanding:
>>>>>> The query is executed in two phases
>>>>>> In the first phase, the index is filtered for qualifying results
>> where the
>>>>>> first key ranges from a1 to a2
>>>>>> In the second phase, these filtered results are filtered once more
>>>>>> according to the range of the second emitted key from b1 to b2.
>>>>>> Thus essentially an "AND" operation.
>>>>>> The filtering is performed using lexicographical rules.
>>>>>> 
>>>>>> Is that correct ? The doc about complex keys is a bit slim.
>>>>>> 
>>>>>> Thanks
>>>>>> 
>>>>>> Klaus
>>> 


Mime
View raw message