You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Tito Ciuro <tc...@mac.com> on 2014/02/12 23:35:38 UTC
Matching docs which values ending with a specific string
Hello,
I have a database with documents with the following format:
{
"_id" : "12345",
"location" : <some_city>.<some town>
}
I have values like:
California.San Francisco
California.Los Angeles
Florida.Miami
...
...
What I'm trying to do is to match documents that "end with" a particular string. Say I want to match all states where the town 'Anytown' exists:
California.Anytown
Florida.Anytown
Texas.Anytown
If I use the following query:
curl http://127.0.0.1:5984/example/_design/test/_view/ends-with-city?key=%22California.Anytown%22
It works and returns one document (as expected), but I cannot seem to make it work when I look for an "end with" string. I have seen examples where %007F is used to match "begins with". When I use it, I get a {"error":"bad_request","reason":"invalid_json"} message. So I'm not sure how to proceed.
Any ideas? Thanks!
-- Tito
Re: Matching docs which values ending with a specific string
Posted by Johannes Jörg Schmidt <sc...@netzmerk.com>.
You need to escape the url (& is evaluated by the shell):
curl 'http://127.0.0.1:5984/test/_design/test/_view/test-byTown?startkey=%5B%22foo%20ca%22%5D&endkey=%5B%22foo%20ca%22,%7B%7D%5D'
Greetings
Johannes
2014-02-13 17:24 GMT+01:00 Tito Ciuro <tc...@mac.com>:
> Hello again,
>
> I'm confused about one behavior I see with curl. If I enter the following URL:
>
> http://127.0.0.1:5984/test/_design/test/_view/test-byTown?startkey=%5B%22foo%20ca%22%5D&endkey=%5B%22foo%20ca%22,%7B%7D%5D
>
> the browser I get the following:
>
> {"total_rows":2,"offset":0,"rows":[
> {"id":"9b857ca67a2611d51595aa3ad0306f30","key":["foo ca","9b857ca67a2611d51595aa3ad0306f30"],"value":null}
> ]}
>
> This works as expected. However, if I issue the same command with curl I get more than I expected:
>
> curl http://127.0.0.1:5984/test/_design/test/_view/test-byTown?startkey=%5B%22foo%20ca%22%5D&endkey=%5B%22foo%20ca%22,%7B%7D%5D
>
> TurboMonkey:_design tito$ {"total_rows":2,"offset":0,"rows":[
> {"id":"9b857ca67a2611d51595aa3ad0306f30","key":["foo ca","9b857ca67a2611d51595aa3ad0306f30"],"value":null},
> {"id":"9b857ca67a2611d51595aa3ad030733c","key":["foo fl","9b857ca67a2611d51595aa3ad030733c"],"value":null}
> ]}
>
> Why do I get different outputs? (and why is curl not doing the right thing?)
>
> Thanks again,
>
> -- Tito
>
> On Feb 12, 2014, at 8:38 PM, Tito Ciuro <tc...@mac.com> wrote:
>
>> Hi Kevin,
>>
>> Yes, you're right. I'm still getting my head around Map/Reduce and I'm learning how to better lay out the data constantly.
>>
>> Thanks for the pointers. Much appreciated!
>>
>> -- Tito
>>
>> On Feb 12, 2014, at 6:26 PM, Kevin Coombes <ke...@gmail.com> wrote:
>>
>>> Maybe you should completely describe the structure you want.
>>>
>>> {
>>> "_id": "12345",
>>> "location": {
>>> "state": "California",
>>> "city": "Florence"
>>> }
>>> }
>>>
>>> Because (1) you can define multiple views that emit any reasonable combination of city and state in any order you might want to query and (2) some day you might want to add an entry like
>>>
>>> {
>>> "_id": "98765",
>>> "location": {
>>> "country": "Italy",
>>> "province": "Tuscany",
>>> "city": "Florence"
>>> }
>>> }
>>>
>>> On 2/12/2014 6:10 PM, Tito Ciuro wrote:
>>>> Hello,
>>>>
>>>> Ah! I see. Well, what if I stored the path in an array instead?:
>>>>
>>>> {
>>>> "_id" : "12345",
>>>> "location" : [<some_city>,<some town>]
>>>> }
>>>>
>>>> If I emit (doc.location,doc._id), would I be able to write a query that matches the second element of the doc.location array?
>>>>
>>>> Thanks again for all the help,
>>>>
>>>> -- Tito
>>>>
>>>> On Feb 12, 2014, at 2:48 PM, Jason Winshell <ja...@bearriver.com> wrote:
>>>>
>>>>> AFAIK, you're describing a CouchDB limitation. You may need to use CouchDB-Lucene to get your answers. CouchDB views match keys in an index. You can match a prefix, but not a tail. And you can't deal with case sensitivity. You might be able to generate a view in which you reverse the chars of your location and then match in reverse order (i.e. turning the problem into a prefix). I think trying to coerce Couch to do that is nutty. Just use CouchDB-Lucene.
>>>>>
>>>>> On Feb 12, 2014, at 2:35 PM, Tito Ciuro <tc...@mac.com> wrote:
>>>>>
>>>>>> Hello,
>>>>>>
>>>>>> I have a database with documents with the following format:
>>>>>>
>>>>>> {
>>>>>> "_id" : "12345",
>>>>>> "location" : <some_city>.<some town>
>>>>>> }
>>>>>>
>>>>>> I have values like:
>>>>>>
>>>>>> California.San Francisco
>>>>>> California.Los Angeles
>>>>>> Florida.Miami
>>>>>> ...
>>>>>> ...
>>>>>>
>>>>>> What I'm trying to do is to match documents that "end with" a particular string. Say I want to match all states where the town 'Anytown' exists:
>>>>>>
>>>>>> California.Anytown
>>>>>> Florida.Anytown
>>>>>> Texas.Anytown
>>>>>>
>>>>>> If I use the following query:
>>>>>>
>>>>>> curl http://127.0.0.1:5984/example/_design/test/_view/ends-with-city?key=%22California.Anytown%22
>>>>>>
>>>>>> It works and returns one document (as expected), but I cannot seem to make it work when I look for an "end with" string. I have seen examples where %007F is used to match "begins with". When I use it, I get a {"error":"bad_request","reason":"invalid_json"} message. So I'm not sure how to proceed.
>>>>>>
>>>>>> Any ideas? Thanks!
>>>>>>
>>>>>> -- Tito
>>>
>>
>
Re: Matching docs which values ending with a specific string
Posted by Tito Ciuro <tc...@mac.com>.
Hi Jens,
This is the command, really. Actually, I discovered that if I put quotes around the URL I don't need to escape the offending character(s) works fine:
TurboMonkey:_design tito$ curl "http://127.0.0.1:5984/test/_design/test/_view/test-byTown?startkey=%5B%22foo%20ca%22%5D&endkey=%5B%22foo%20ca%22,%7B%7D%5D"
{"total_rows":2,"offset":0,"rows":[
{"id":"9b857ca67a2611d51595aa3ad0306f30","key":["foo ca","9b857ca67a2611d51595aa3ad0306f30"],"value":null}
]}
Thank you,
-- Tito
On Feb 13, 2014, at 8:34 AM, Jens Alfke <je...@couchbase.com> wrote:
>
> On Feb 13, 2014, at 8:24 AM, Tito Ciuro <tc...@mac.com> wrote:
>
>> This works as expected. However, if I issue the same command with curl I get more than I expected:
>>
>> curl http://127.0.0.1:5984/test/_design/test/_view/test-byTown?startkey=%5B%22foo%20ca%22%5D&endkey=%5B%22foo%20ca%22,%7B%7D%5D
>
> I don't think that's the exact command line you used, because without quotes the shell's going to interpret the '?' character as a wildcard. Could you post the exact command line?
>
> (Also, could you show what the startkey and endkey expand to? I'm not good at doing URL-unescaping in my head.)
>
> —Jens
>
> PS: I've found that the 'httpie' tool saves a lot of sanity when sending command-line requests to CouchDB. It's similar to curl but a lot friendlier, and makes it very easy to add custom queries and JSON body properties without doing a ton of quoting.
Re: Matching docs which values ending with a specific string
Posted by Andy Wenk <an...@nms.de>.
Hi Tito,
On 15 February 2014 20:01, Tito Ciuro <tc...@mac.com> wrote:
> Hi Andy,
>
> That’s great. This are awesome news. Is there a way to download 1.6 to
> take it for a spin? The download section takes me to the 1.5 downloads.
>
the best way is to fork the repository at github (you have a github account
... don't you :) ), clone it locally to your machine, push your changes to
your CouchDB fork and then start a pull request ...
If you need any further help, please let me know!
Cheers
Andy
> Thanks,
>
> — Tito
>
> On Feb 15, 2014, 7:37:03 AM, Andy Wenk <an...@nms.de> wrote:
> ------------------------------
> Hi everyone,
>
> I just want to drop a side note. Everyone knows, that the documentation is
> partially outdated and that the Wiki has some older parts which are not
> that regularly maintained. I generally suggest to first have a look at the
> "new" documentation site found at http://docs.couchdb.org.
>
> I kindly ask everyone to update the documentation if errors are found or
> if
> there are missing bits. The source for doc.couchdb.org is living in the
> CouchDB source code at share/doc/src. Simply clone the repo and fire a
> pull
> request if you have changed or added things. For editing the Wiki pages,
> please register there and drop a note to the user@ or dev@ list. We will
> add you to the contributors list.
>
> Btw. - Noah started to fill the new Wiki :)
>
> Thanks a lot and keep on discussing :)
>
> Cheers
>
> Andy
>
>
> On 13 February 2014 17:47, Tito Ciuro <tc...@mac.com> wrote:
>
> > Hi Aurélien,
> >
> > Awesome tip. I entered the following value in the URL and worked right
> > away:
> >
> > http://127.0.0.1:5984/test/_design/test/_view/test-byTown?startkey=["foo
>
> > ca"]&endkey=["foo ca",{}]
> >
> > Cool! :-)
> >
> > -- Tito
> >
> > On Feb 13, 2014, at 8:41 AM, Aurélien Bénel <au...@utt.fr>
> wrote:
> >
> > >> I'm not good at doing URL-unescaping in my head.
> > >
> > > For those of us that are not native "escapers", I really recommend
> > RESTClient [1].
> > > This is *so* easier to use than `curl`.
> > >
> > >
> > > Regards,
> > >
> > > Aurélien
> > >
> > > [1] https://addons.mozilla.org/en-US/firefox/addon/restclient/
> >
> >
>
>
> --
> Andy Wenk
> Hamburg - Germany
> RockIt!
>
> http://www.couchdb-buch.de
> http://www.pg-praxisbuch.de
>
> GPG fingerprint: C044 8322 9E12 1483 4FEC 9452 B65D 6BE3 9ED3 9588
>
> https://people.apache.org/keys/committer/andywenk.asc
>
--
Andy Wenk
Hamburg - Germany
RockIt!
http://www.couchdb-buch.de
http://www.pg-praxisbuch.de
GPG fingerprint: C044 8322 9E12 1483 4FEC 9452 B65D 6BE3 9ED3 9588
https://people.apache.org/keys/committer/andywenk.asc
Re: Matching docs which values ending with a specific string
Posted by Tito Ciuro <tc...@mac.com>.
Hi Andy,
That’s great. This are awesome news. Is there a way to download 1.6 to take it for a spin? The download section takes me to the 1.5 downloads.
Thanks,
— Tito
On Feb 15, 2014, 7:37:03 AM, Andy Wenk <an...@nms.de> wrote: Hi everyone,
I just want to drop a side note. Everyone knows, that the documentation is
partially outdated and that the Wiki has some older parts which are not
that regularly maintained. I generally suggest to first have a look at the
"new" documentation site found at http://docs.couchdb.org.
I kindly ask everyone to update the documentation if errors are found or if
there are missing bits. The source for doc.couchdb.org is living in the
CouchDB source code at share/doc/src. Simply clone the repo and fire a pull
request if you have changed or added things. For editing the Wiki pages,
please register there and drop a note to the user@ or dev@ list. We will
add you to the contributors list.
Btw. - Noah started to fill the new Wiki :)
Thanks a lot and keep on discussing :)
Cheers
Andy
On 13 February 2014 17:47, Tito Ciuro <tc...@mac.com> wrote:
> Hi Aurélien,
>
> Awesome tip. I entered the following value in the URL and worked right
> away:
>
> http://127.0.0.1:5984/test/_design/test/_view/test-byTown?startkey=["foo
> ca"]&endkey=["foo ca",{}]
>
> Cool! :-)
>
> -- Tito
>
> On Feb 13, 2014, at 8:41 AM, Aurélien Bénel <au...@utt.fr> wrote:
>
> >> I'm not good at doing URL-unescaping in my head.
> >
> > For those of us that are not native "escapers", I really recommend
> RESTClient [1].
> > This is *so* easier to use than `curl`.
> >
> >
> > Regards,
> >
> > Aurélien
> >
> > [1] https://addons.mozilla.org/en-US/firefox/addon/restclient/
>
>
--
Andy Wenk
Hamburg - Germany
RockIt!
http://www.couchdb-buch.de
http://www.pg-praxisbuch.de
GPG fingerprint: C044 8322 9E12 1483 4FEC 9452 B65D 6BE3 9ED3 9588
https://people.apache.org/keys/committer/andywenk.asc
Re: Matching docs which values ending with a specific string
Posted by Andy Wenk <an...@nms.de>.
Hi everyone,
I just want to drop a side note. Everyone knows, that the documentation is
partially outdated and that the Wiki has some older parts which are not
that regularly maintained. I generally suggest to first have a look at the
"new" documentation site found at http://docs.couchdb.org.
I kindly ask everyone to update the documentation if errors are found or if
there are missing bits. The source for doc.couchdb.org is living in the
CouchDB source code at share/doc/src. Simply clone the repo and fire a pull
request if you have changed or added things. For editing the Wiki pages,
please register there and drop a note to the user@ or dev@ list. We will
add you to the contributors list.
Btw. - Noah started to fill the new Wiki :)
Thanks a lot and keep on discussing :)
Cheers
Andy
On 13 February 2014 17:47, Tito Ciuro <tc...@mac.com> wrote:
> Hi Aurélien,
>
> Awesome tip. I entered the following value in the URL and worked right
> away:
>
> http://127.0.0.1:5984/test/_design/test/_view/test-byTown?startkey=["foo
> ca"]&endkey=["foo ca",{}]
>
> Cool! :-)
>
> -- Tito
>
> On Feb 13, 2014, at 8:41 AM, Aurélien Bénel <au...@utt.fr> wrote:
>
> >> I'm not good at doing URL-unescaping in my head.
> >
> > For those of us that are not native "escapers", I really recommend
> RESTClient [1].
> > This is *so* easier to use than `curl`.
> >
> >
> > Regards,
> >
> > Aurélien
> >
> > [1] https://addons.mozilla.org/en-US/firefox/addon/restclient/
>
>
--
Andy Wenk
Hamburg - Germany
RockIt!
http://www.couchdb-buch.de
http://www.pg-praxisbuch.de
GPG fingerprint: C044 8322 9E12 1483 4FEC 9452 B65D 6BE3 9ED3 9588
https://people.apache.org/keys/committer/andywenk.asc
Re: Matching docs which values ending with a specific string
Posted by Tito Ciuro <tc...@mac.com>.
Hi Aurélien,
Awesome tip. I entered the following value in the URL and worked right away:
http://127.0.0.1:5984/test/_design/test/_view/test-byTown?startkey=["foo ca"]&endkey=["foo ca",{}]
Cool! :-)
-- Tito
On Feb 13, 2014, at 8:41 AM, Aurélien Bénel <au...@utt.fr> wrote:
>> I'm not good at doing URL-unescaping in my head.
>
> For those of us that are not native "escapers", I really recommend RESTClient [1].
> This is *so* easier to use than `curl`.
>
>
> Regards,
>
> Aurélien
>
> [1] https://addons.mozilla.org/en-US/firefox/addon/restclient/
Re: Matching docs which values ending with a specific string
Posted by Aurélien Bénel <au...@utt.fr>.
> I'm not good at doing URL-unescaping in my head.
For those of us that are not native "escapers", I really recommend RESTClient [1].
This is *so* easier to use than `curl`.
Regards,
Aurélien
[1] https://addons.mozilla.org/en-US/firefox/addon/restclient/
Re: Matching docs which values ending with a specific string
Posted by Jens Alfke <je...@couchbase.com>.
On Feb 13, 2014, at 8:24 AM, Tito Ciuro <tc...@mac.com> wrote:
> This works as expected. However, if I issue the same command with curl I get more than I expected:
>
> curl http://127.0.0.1:5984/test/_design/test/_view/test-byTown?startkey=%5B%22foo%20ca%22%5D&endkey=%5B%22foo%20ca%22,%7B%7D%5D
I don't think that's the exact command line you used, because without quotes the shell's going to interpret the '?' character as a wildcard. Could you post the exact command line?
(Also, could you show what the startkey and endkey expand to? I'm not good at doing URL-unescaping in my head.)
—Jens
PS: I've found that the 'httpie' tool saves a lot of sanity when sending command-line requests to CouchDB. It's similar to curl but a lot friendlier, and makes it very easy to add custom queries and JSON body properties without doing a ton of quoting.
Re: Matching docs which values ending with a specific string
Posted by Tito Ciuro <tc...@mac.com>.
Hello again,
I'm confused about one behavior I see with curl. If I enter the following URL:
http://127.0.0.1:5984/test/_design/test/_view/test-byTown?startkey=%5B%22foo%20ca%22%5D&endkey=%5B%22foo%20ca%22,%7B%7D%5D
the browser I get the following:
{"total_rows":2,"offset":0,"rows":[
{"id":"9b857ca67a2611d51595aa3ad0306f30","key":["foo ca","9b857ca67a2611d51595aa3ad0306f30"],"value":null}
]}
This works as expected. However, if I issue the same command with curl I get more than I expected:
curl http://127.0.0.1:5984/test/_design/test/_view/test-byTown?startkey=%5B%22foo%20ca%22%5D&endkey=%5B%22foo%20ca%22,%7B%7D%5D
TurboMonkey:_design tito$ {"total_rows":2,"offset":0,"rows":[
{"id":"9b857ca67a2611d51595aa3ad0306f30","key":["foo ca","9b857ca67a2611d51595aa3ad0306f30"],"value":null},
{"id":"9b857ca67a2611d51595aa3ad030733c","key":["foo fl","9b857ca67a2611d51595aa3ad030733c"],"value":null}
]}
Why do I get different outputs? (and why is curl not doing the right thing?)
Thanks again,
-- Tito
On Feb 12, 2014, at 8:38 PM, Tito Ciuro <tc...@mac.com> wrote:
> Hi Kevin,
>
> Yes, you're right. I'm still getting my head around Map/Reduce and I'm learning how to better lay out the data constantly.
>
> Thanks for the pointers. Much appreciated!
>
> -- Tito
>
> On Feb 12, 2014, at 6:26 PM, Kevin Coombes <ke...@gmail.com> wrote:
>
>> Maybe you should completely describe the structure you want.
>>
>> {
>> "_id": "12345",
>> "location": {
>> "state": "California",
>> "city": "Florence"
>> }
>> }
>>
>> Because (1) you can define multiple views that emit any reasonable combination of city and state in any order you might want to query and (2) some day you might want to add an entry like
>>
>> {
>> "_id": "98765",
>> "location": {
>> "country": "Italy",
>> "province": "Tuscany",
>> "city": "Florence"
>> }
>> }
>>
>> On 2/12/2014 6:10 PM, Tito Ciuro wrote:
>>> Hello,
>>>
>>> Ah! I see. Well, what if I stored the path in an array instead?:
>>>
>>> {
>>> "_id" : "12345",
>>> "location" : [<some_city>,<some town>]
>>> }
>>>
>>> If I emit (doc.location,doc._id), would I be able to write a query that matches the second element of the doc.location array?
>>>
>>> Thanks again for all the help,
>>>
>>> -- Tito
>>>
>>> On Feb 12, 2014, at 2:48 PM, Jason Winshell <ja...@bearriver.com> wrote:
>>>
>>>> AFAIK, you're describing a CouchDB limitation. You may need to use CouchDB-Lucene to get your answers. CouchDB views match keys in an index. You can match a prefix, but not a tail. And you can't deal with case sensitivity. You might be able to generate a view in which you reverse the chars of your location and then match in reverse order (i.e. turning the problem into a prefix). I think trying to coerce Couch to do that is nutty. Just use CouchDB-Lucene.
>>>>
>>>> On Feb 12, 2014, at 2:35 PM, Tito Ciuro <tc...@mac.com> wrote:
>>>>
>>>>> Hello,
>>>>>
>>>>> I have a database with documents with the following format:
>>>>>
>>>>> {
>>>>> "_id" : "12345",
>>>>> "location" : <some_city>.<some town>
>>>>> }
>>>>>
>>>>> I have values like:
>>>>>
>>>>> California.San Francisco
>>>>> California.Los Angeles
>>>>> Florida.Miami
>>>>> ...
>>>>> ...
>>>>>
>>>>> What I'm trying to do is to match documents that "end with" a particular string. Say I want to match all states where the town 'Anytown' exists:
>>>>>
>>>>> California.Anytown
>>>>> Florida.Anytown
>>>>> Texas.Anytown
>>>>>
>>>>> If I use the following query:
>>>>>
>>>>> curl http://127.0.0.1:5984/example/_design/test/_view/ends-with-city?key=%22California.Anytown%22
>>>>>
>>>>> It works and returns one document (as expected), but I cannot seem to make it work when I look for an "end with" string. I have seen examples where %007F is used to match "begins with". When I use it, I get a {"error":"bad_request","reason":"invalid_json"} message. So I'm not sure how to proceed.
>>>>>
>>>>> Any ideas? Thanks!
>>>>>
>>>>> -- Tito
>>
>
Re: Matching docs which values ending with a specific string
Posted by Tito Ciuro <tc...@mac.com>.
Hi Kevin,
Yes, you're right. I'm still getting my head around Map/Reduce and I'm learning how to better lay out the data constantly.
Thanks for the pointers. Much appreciated!
-- Tito
On Feb 12, 2014, at 6:26 PM, Kevin Coombes <ke...@gmail.com> wrote:
> Maybe you should completely describe the structure you want.
>
> {
> "_id": "12345",
> "location": {
> "state": "California",
> "city": "Florence"
> }
> }
>
> Because (1) you can define multiple views that emit any reasonable combination of city and state in any order you might want to query and (2) some day you might want to add an entry like
>
> {
> "_id": "98765",
> "location": {
> "country": "Italy",
> "province": "Tuscany",
> "city": "Florence"
> }
> }
>
> On 2/12/2014 6:10 PM, Tito Ciuro wrote:
>> Hello,
>>
>> Ah! I see. Well, what if I stored the path in an array instead?:
>>
>> {
>> "_id" : "12345",
>> "location" : [<some_city>,<some town>]
>> }
>>
>> If I emit (doc.location,doc._id), would I be able to write a query that matches the second element of the doc.location array?
>>
>> Thanks again for all the help,
>>
>> -- Tito
>>
>> On Feb 12, 2014, at 2:48 PM, Jason Winshell <ja...@bearriver.com> wrote:
>>
>>> AFAIK, you're describing a CouchDB limitation. You may need to use CouchDB-Lucene to get your answers. CouchDB views match keys in an index. You can match a prefix, but not a tail. And you can't deal with case sensitivity. You might be able to generate a view in which you reverse the chars of your location and then match in reverse order (i.e. turning the problem into a prefix). I think trying to coerce Couch to do that is nutty. Just use CouchDB-Lucene.
>>>
>>> On Feb 12, 2014, at 2:35 PM, Tito Ciuro <tc...@mac.com> wrote:
>>>
>>>> Hello,
>>>>
>>>> I have a database with documents with the following format:
>>>>
>>>> {
>>>> "_id" : "12345",
>>>> "location" : <some_city>.<some town>
>>>> }
>>>>
>>>> I have values like:
>>>>
>>>> California.San Francisco
>>>> California.Los Angeles
>>>> Florida.Miami
>>>> ...
>>>> ...
>>>>
>>>> What I'm trying to do is to match documents that "end with" a particular string. Say I want to match all states where the town 'Anytown' exists:
>>>>
>>>> California.Anytown
>>>> Florida.Anytown
>>>> Texas.Anytown
>>>>
>>>> If I use the following query:
>>>>
>>>> curl http://127.0.0.1:5984/example/_design/test/_view/ends-with-city?key=%22California.Anytown%22
>>>>
>>>> It works and returns one document (as expected), but I cannot seem to make it work when I look for an "end with" string. I have seen examples where %007F is used to match "begins with". When I use it, I get a {"error":"bad_request","reason":"invalid_json"} message. So I'm not sure how to proceed.
>>>>
>>>> Any ideas? Thanks!
>>>>
>>>> -- Tito
>
Re: Matching docs which values ending with a specific string
Posted by Kevin Coombes <ke...@gmail.com>.
Maybe you should completely describe the structure you want.
{
"_id": "12345",
"location": {
"state": "California",
"city": "Florence"
}
}
Because (1) you can define multiple views that emit any reasonable
combination of city and state in any order you might want to query and
(2) some day you might want to add an entry like
{
"_id": "98765",
"location": {
"country": "Italy",
"province": "Tuscany",
"city": "Florence"
}
}
On 2/12/2014 6:10 PM, Tito Ciuro wrote:
> Hello,
>
> Ah! I see. Well, what if I stored the path in an array instead?:
>
> {
> "_id" : "12345",
> "location" : [<some_city>,<some town>]
> }
>
> If I emit (doc.location,doc._id), would I be able to write a query that matches the second element of the doc.location array?
>
> Thanks again for all the help,
>
> -- Tito
>
> On Feb 12, 2014, at 2:48 PM, Jason Winshell <ja...@bearriver.com> wrote:
>
>> AFAIK, you're describing a CouchDB limitation. You may need to use CouchDB-Lucene to get your answers. CouchDB views match keys in an index. You can match a prefix, but not a tail. And you can't deal with case sensitivity. You might be able to generate a view in which you reverse the chars of your location and then match in reverse order (i.e. turning the problem into a prefix). I think trying to coerce Couch to do that is nutty. Just use CouchDB-Lucene.
>>
>> On Feb 12, 2014, at 2:35 PM, Tito Ciuro <tc...@mac.com> wrote:
>>
>>> Hello,
>>>
>>> I have a database with documents with the following format:
>>>
>>> {
>>> "_id" : "12345",
>>> "location" : <some_city>.<some town>
>>> }
>>>
>>> I have values like:
>>>
>>> California.San Francisco
>>> California.Los Angeles
>>> Florida.Miami
>>> ...
>>> ...
>>>
>>> What I'm trying to do is to match documents that "end with" a particular string. Say I want to match all states where the town 'Anytown' exists:
>>>
>>> California.Anytown
>>> Florida.Anytown
>>> Texas.Anytown
>>>
>>> If I use the following query:
>>>
>>> curl http://127.0.0.1:5984/example/_design/test/_view/ends-with-city?key=%22California.Anytown%22
>>>
>>> It works and returns one document (as expected), but I cannot seem to make it work when I look for an "end with" string. I have seen examples where %007F is used to match "begins with". When I use it, I get a {"error":"bad_request","reason":"invalid_json"} message. So I'm not sure how to proceed.
>>>
>>> Any ideas? Thanks!
>>>
>>> -- Tito
Re: Matching docs which values ending with a specific string
Posted by Tito Ciuro <tc...@mac.com>.
Hello,
Ah! I see. Well, what if I stored the path in an array instead?:
{
"_id" : "12345",
"location" : [<some_city>,<some town>]
}
If I emit (doc.location,doc._id), would I be able to write a query that matches the second element of the doc.location array?
Thanks again for all the help,
-- Tito
On Feb 12, 2014, at 2:48 PM, Jason Winshell <ja...@bearriver.com> wrote:
> AFAIK, you're describing a CouchDB limitation. You may need to use CouchDB-Lucene to get your answers. CouchDB views match keys in an index. You can match a prefix, but not a tail. And you can't deal with case sensitivity. You might be able to generate a view in which you reverse the chars of your location and then match in reverse order (i.e. turning the problem into a prefix). I think trying to coerce Couch to do that is nutty. Just use CouchDB-Lucene.
>
> On Feb 12, 2014, at 2:35 PM, Tito Ciuro <tc...@mac.com> wrote:
>
>> Hello,
>>
>> I have a database with documents with the following format:
>>
>> {
>> "_id" : "12345",
>> "location" : <some_city>.<some town>
>> }
>>
>> I have values like:
>>
>> California.San Francisco
>> California.Los Angeles
>> Florida.Miami
>> ...
>> ...
>>
>> What I'm trying to do is to match documents that "end with" a particular string. Say I want to match all states where the town 'Anytown' exists:
>>
>> California.Anytown
>> Florida.Anytown
>> Texas.Anytown
>>
>> If I use the following query:
>>
>> curl http://127.0.0.1:5984/example/_design/test/_view/ends-with-city?key=%22California.Anytown%22
>>
>> It works and returns one document (as expected), but I cannot seem to make it work when I look for an "end with" string. I have seen examples where %007F is used to match "begins with". When I use it, I get a {"error":"bad_request","reason":"invalid_json"} message. So I'm not sure how to proceed.
>>
>> Any ideas? Thanks!
>>
>> -- Tito
>
Re: Matching docs which values ending with a specific string
Posted by Jason Winshell <ja...@bearriver.com>.
AFAIK, you're describing a CouchDB limitation. You may need to use CouchDB-Lucene to get your answers. CouchDB views match keys in an index. You can match a prefix, but not a tail. And you can't deal with case sensitivity. You might be able to generate a view in which you reverse the chars of your location and then match in reverse order (i.e. turning the problem into a prefix). I think trying to coerce Couch to do that is nutty. Just use CouchDB-Lucene.
On Feb 12, 2014, at 2:35 PM, Tito Ciuro <tc...@mac.com> wrote:
> Hello,
>
> I have a database with documents with the following format:
>
> {
> "_id" : "12345",
> "location" : <some_city>.<some town>
> }
>
> I have values like:
>
> California.San Francisco
> California.Los Angeles
> Florida.Miami
> ...
> ...
>
> What I'm trying to do is to match documents that "end with" a particular string. Say I want to match all states where the town 'Anytown' exists:
>
> California.Anytown
> Florida.Anytown
> Texas.Anytown
>
> If I use the following query:
>
> curl http://127.0.0.1:5984/example/_design/test/_view/ends-with-city?key=%22California.Anytown%22
>
> It works and returns one document (as expected), but I cannot seem to make it work when I look for an "end with" string. I have seen examples where %007F is used to match "begins with". When I use it, I get a {"error":"bad_request","reason":"invalid_json"} message. So I'm not sure how to proceed.
>
> Any ideas? Thanks!
>
> -- Tito
Re: Matching docs which values ending with a specific string
Posted by Tito Ciuro <tc...@mac.com>.
Cool! Definitely works for me.
Thanks everyone for the help,
-- Tito
On Feb 12, 2014, at 3:20 PM, Robert Samuel Newson <rn...@apache.org> wrote:
> Tito,
>
> You’re missing the point. You can’t select by the second element of the array for the same reason you can’t select by the second half of your original string. :)
>
> Benoit’s suggestion to do both is good, as long as you don’t care to know whether you’ve found a town or a city. If you do, then;
>
> emit(["town_city", doc.town, doc.city], null);
> emit(["city_town", doc.city, doc.town], null);
>
> (or two separate views).
>
> B.
>
> On 12 Feb 2014, at 23:17, Benoit Chesneau <bc...@gmail.com> wrote:
>
>> On Wed, Feb 12, 2014 at 11:35 PM, Tito Ciuro <tc...@mac.com> wrote:
>>> Hello,
>>>
>>> I have a database with documents with the following format:
>>>
>>> {
>>> "_id" : "12345",
>>> "location" : <some_city>.<some town>
>>> }
>>>
>>
>> you can emit[<some_city>,<some town>] *and* [<some town>, <some_city>]
>> so you can query either by city or town.
>>
>>
>> - benoit
>
Re: Matching docs which values ending with a specific string
Posted by Robert Samuel Newson <rn...@apache.org>.
Tito,
You’re missing the point. You can’t select by the second element of the array for the same reason you can’t select by the second half of your original string. :)
Benoit’s suggestion to do both is good, as long as you don’t care to know whether you’ve found a town or a city. If you do, then;
emit(["town_city", doc.town, doc.city], null);
emit(["city_town", doc.city, doc.town], null);
(or two separate views).
B.
On 12 Feb 2014, at 23:17, Benoit Chesneau <bc...@gmail.com> wrote:
> On Wed, Feb 12, 2014 at 11:35 PM, Tito Ciuro <tc...@mac.com> wrote:
>> Hello,
>>
>> I have a database with documents with the following format:
>>
>> {
>> "_id" : "12345",
>> "location" : <some_city>.<some town>
>> }
>>
>
> you can emit[<some_city>,<some town>] *and* [<some town>, <some_city>]
> so you can query either by city or town.
>
>
> - benoit
Re: Matching docs which values ending with a specific string
Posted by Benoit Chesneau <bc...@gmail.com>.
On Wed, Feb 12, 2014 at 11:35 PM, Tito Ciuro <tc...@mac.com> wrote:
> Hello,
>
> I have a database with documents with the following format:
>
> {
> "_id" : "12345",
> "location" : <some_city>.<some town>
> }
>
you can emit[<some_city>,<some town>] *and* [<some town>, <some_city>]
so you can query either by city or town.
- benoit
Re: Matching docs which values ending with a specific string
Posted by Robert Samuel Newson <rn...@apache.org>.
Views are ordered by key, you can’t do "ends with" queries without reading the entire view and filtering at the client.
You *can* do "begins with" using startkey/endkey, since those are adjacent rows.
for example,
map:
function(doc) {
emit([doc.town, doc.city], null);
}
query with ?startkey=["Anytown"]&endkey=["Anytown",{}];
{} is *not* a wildcard, it’s just an empty JSON object, but objects sort higher than strings, so this startkey/endkey combination captures any of the emitted values where the first item was "Anytown".
The full details of what values sort higher than other values is here: https://wiki.apache.org/couchdb/View_collation
B.
On 12 Feb 2014, at 22:35, Tito Ciuro <tc...@mac.com> wrote:
> Hello,
>
> I have a database with documents with the following format:
>
> {
> "_id" : "12345",
> "location" : <some_city>.<some town>
> }
>
> I have values like:
>
> California.San Francisco
> California.Los Angeles
> Florida.Miami
> ...
> ...
>
> What I'm trying to do is to match documents that "end with" a particular string. Say I want to match all states where the town 'Anytown' exists:
>
> California.Anytown
> Florida.Anytown
> Texas.Anytown
>
> If I use the following query:
>
> curl http://127.0.0.1:5984/example/_design/test/_view/ends-with-city?key=%22California.Anytown%22
>
> It works and returns one document (as expected), but I cannot seem to make it work when I look for an "end with" string. I have seen examples where %007F is used to match "begins with". When I use it, I get a {"error":"bad_request","reason":"invalid_json"} message. So I'm not sure how to proceed.
>
> Any ideas? Thanks!
>
> -- Tito
Re: Matching docs which values ending with a specific string
Posted by Jens Alfke <je...@couchbase.com>.
On Feb 12, 2014, at 5:08 PM, Tito Ciuro <tc...@mac.com> wrote:
> This is taken verbatim from the "Getting Started with CouchDB" book, page 49:
Hm, I have not seen that book. But I agree that the general documentation situation is not good. At least the online docs are better than they used to be.
> [...] If we want to restrict it to those starting with Apricot, we can use the UTF-8 sorting to our advantage. If we add the UTF-8 character 007F to ‘Apricot’, the range will only include recipes with the title starting with Apricot, even if the document ID contains other characters.
I see what they're getting at — it's the same trick as adding a "z" as a suffix (endkey="apricots") to stop at a key that starts with anything beyond "apricot", except that they're intending \u007F as a sort of "super-z" that sorts greater than anything else.
But that's wrong, because CouchDB doesn't use UTF-8 sorting, it uses Unicode sorting. From the wiki: "Comparison of strings is done using ICU which implements the Unicode Collation Algorithm…" [1] So a \u007f character isn't a particularly high value; it's greater than any ASCII character but lower than anything else including other non-English Roman characters.
On the same page[2] the wiki suggests using the character \ufff0 as a suffix for this purpose. That sounds more reasonable, although the details depend on whether the collation is really being done on true Unicode code points or a UTF-16 encoding. If the former, \ufff0 isn't at the top of the range and things like emoji will sort after it.
I hope you see that this is simply a trick of string range comparisons, not a special CouchDB feature — you could use the same trick in a SQL query (if the database were sufficiently Unicode-savvy.)
> Let’s see that in action:
> http://127.0.0.1:5984/recipes/_design/simple/_view/by_title?startkey=%22Apricot %22&endkey=%22Apricot%007F%22
This looks like a typo: they must have meant %7F but wrote %007F instead.
—Jens
[1] http://wiki.apache.org/couchdb/View_collation#Collation_Specification
[2] http://wiki.apache.org/couchdb/View_collation#String_Ranges
Re: Matching docs which values ending with a specific string
Posted by Tito Ciuro <tc...@mac.com>.
Hi Jens,
This is taken verbatim from the "Getting Started with CouchDB" book, page 49:
[...] If we want to restrict it to those starting with Apricot, we can use the UTF-8 sorting to our advantage. If we add the UTF-8 character 007F to ‘Apricot’, the range will only include recipes with the title starting with Apricot, even if the document ID contains other characters. Let’s see that in action:
http://127.0.0.1:5984/recipes/_design/simple/_view/by_title?startkey=%22Apricot %22&endkey=%22Apricot%007F%22
[...]
In general, I've found the books/guides on CouchDB to be either outdated, plain wrong or short in details. :-/
-- Tito
On Feb 12, 2014, at 4:45 PM, Jens Alfke <je...@couchbase.com> wrote:
>
> On Feb 12, 2014, at 2:35 PM, Tito Ciuro <tc...@mac.com> wrote:
>
>> I have seen examples where %007F is used to match "begins with".
>
> In a URL? That would decode to a NUL (zero byte) followed by the digits '7' and 'F' … not meaningful.
> If you meant %7F, that's a DEL control character … also not meaningful.
>
> I don't think these are examples for CouchDB. It doesn't use any special metacharacters like this for queries.
>
> —Jens
Re: Matching docs which values ending with a specific string
Posted by Jens Alfke <je...@couchbase.com>.
On Feb 12, 2014, at 2:35 PM, Tito Ciuro <tc...@mac.com> wrote:
> I have seen examples where %007F is used to match "begins with".
In a URL? That would decode to a NUL (zero byte) followed by the digits '7' and 'F' … not meaningful.
If you meant %7F, that's a DEL control character … also not meaningful.
I don't think these are examples for CouchDB. It doesn't use any special metacharacters like this for queries.
—Jens