how to get list of unique terms for a field

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

how to get list of unique terms for a field

Joel Nylund
Hi,

lets say I have a field called countryName, is there a way to get a  
list of all the countries for this field? Trying to figure out a nice  
way to keep my categories and the solr results in sync, would be nice  
to get these from solr instead of the database.

thanks
Joel

Reply | Threaded
Open this post in threaded view
|

Re: how to get list of unique terms for a field

Erik Hatcher-4

On Dec 4, 2009, at 8:59 AM, Joel Nylund wrote:
> lets say I have a field called countryName, is there a way to get a  
> list of all the countries for this field? Trying to figure out a  
> nice way to keep my categories and the solr results in sync, would  
> be nice to get these from solr instead of the database.

A couple of ways, depending on what you want:

   1) faceting, as part of the search results, filtered within  
constraints (q/fq's):  &facet=on&facet.field=countryName

   2) TermsComponent: http://wiki.apache.org/solr/TermsComponent

        Erik

Reply | Threaded
Open this post in threaded view
|

Re: how to get list of unique terms for a field

Bill Dueber-2
In reply to this post by Joel Nylund
Here's a pretty simple perl script. Call it as "scriptname facetindex" (or
"scriptname facetindex maxnum")

#############

#!/usr/local/bin/perl
use strict;
use JSON::XS;
use LWP::Simple;

### CHANGE THIS TO YOUR URL!! ###

my $select = 'http://solr-vufind:8026/solr/biblio/select';


# Get facet and (optional) maxnum from the command line
my ($facet, $num) = @ARGV;
$num ||= -1; # all values

my $url =
"$select?q=*:*&rows=0&facet=true&facet.limit=$num&facet.field=$facet&wt=json&
json.nl=arrarr";
my $json = decode_json(get($url));

foreach my $a (@{$json->{facet_counts}{facet_fields}{$facet}}) {
    print $a->[0], "\n";
}