couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Magnus Ottosson <mag...@magnusottosson.se>
Subject Re: Search for city by name, order by population
Date Mon, 12 Apr 2010 11:34:29 GMT
I was thinking about something like this (correct me if this is a
stupid way of doing it):

For the map function of the view I have:

function(doc) {
    for(var i = 3, i <= doc.name.length; i++){
		emit(doc.name.substring(0, i), doc);
	}
}

This will create several keys for each doc. For the city "new york" it
would create the following keys:

"new", "new ", "new y", "new yo", "new yor", "new york".

This way I can search for all cities starting with the name "new" like
this: "?key=new" and this will return all cities with the "new" key
right?

To limit the result I create a reduce function like this:

function(keys, values, rereduce) {

	var vals = [];
	var resultLength = 20;

	for(var i = 0; i <= values.length;i++){

		if(vals.length <= resultLength){
			vals.push(values[i]);
		}
		else{
			vals.sort(function(a,b){
				return b.population - a.population;
			});
			
			if(vals[resultLength - 1].population < values[i].population){
				vals.pop();
				vals.push(values[i]);
			}
		}
	}
}

What this function does is that it creates an array of the x (defined
in the reduce function) largest cities and return them.

The downside of this is that the number of return cities is defined in
the result function but I can live with that.

Will this work as I expect? Will this have a large impact on the size
of the database? Does all keys point at the same document? Or is all
the documents store once for each key? Have I understand how the
reduce function works correctly?

Magnus



On Mon, Apr 12, 2010 at 11:34 AM, Nils Breunese <N.Breunese@vpro.nl> wrote:
> I'm sorry, I totally misread your use case. Yes, you are correct. Forget what I said.
:o)
>
> I found couchdb-footrest a while ago, which apparently features 'Order Docs By Value
Fields': http://github.com/assembly/couchdb-footrest Sadly, I never got couchdb-footrest to
work.
>
> Like others have suggested: I think you'll want to go and look into couchdb-lucene, which
will certainly do what you're looking for: http://github.com/rnewson/couchdb-lucene
>
> Nils Breunese.
> ________________________________________
> Van: Magnus Ottosson [magnus@magnusottosson.se]
> Verzonden: maandag 12 april 2010 11:19
> Aan: user@couchdb.apache.org
> Onderwerp: Re: Search for city by name, order by population
>
> Are you sure about that?
>
> Wouldn't [name, population] first order the cities by name and if
> there are more than one city with the same name they would be order by
> population?
>
> Magnus
>
> De informatie vervat in deze  e-mail en meegezonden bijlagen is uitsluitend bedoeld
voor gebruik door de geadresseerde en kan vertrouwelijke informatie bevatten. Openbaarmaking,
vermenigvuldiging, verspreiding en/of verstrekking van deze informatie aan derden is voorbehouden
aan geadresseerde. De VPRO staat niet in voor de juiste en volledige overbrenging van de inhoud
van een verzonden e-mail, noch voor tijdige ontvangst daarvan.
>

Mime
View raw message