lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Paul Sundling (Webdaddy)" <>
Subject Re: filtering by first letter
Date Mon, 26 Feb 2007 02:40:41 GMT
OK I'm not sure I understand your answer.  I thought TermEnum gave you 
all the terms in an index, not from a search result.

Let me clarify what I need.  I'm looking for a way to find out all the 
values of the FIELD_FILTER_LETTER field for any given search.

INDEX TIME:   (done for each indexed person, stores the first letter of 
their name as a field)
        if (person.getPersonName() != null) {
            String filterLetter = person.getPersonName().substring(0, 
            document.add(new Field(FIELD_FILTER_LETTER, filterLetter,
                    Field.Store.YES, Field.Index.UN_TOKENIZED));

SEARCH TIME:  (need to present a list of all values of 
        IndexSearcher searcher = getIndexSearcher();
        Hits result =, filter, sort);

If the filter letter has been picked, this is the filter used, otherwise 
the filter is null:
So params comes from
            TermQuery letterQuery = new TermQuery(new Term(
                    KEY_FILTER_LETTER, params.getFilterLetter()));
            QueryFilter letterFilter = new QueryFilter(letterQuery);
            result =, letterFilter, sort);

So where do I plug in the TermEnum at search time?  I haven't used 
TermEnum before.


Erick Erickson wrote:
> See TermEnum (I don't think you need TermDocs for this). If you 
> instantiate
> a TermEnum(new Term("firstletterfield", "")), it'll enumerate all the 
> terms
> in your 'firstletter' field and you can just collect them and go...
> For that matter, and assuming that your names are UN_TOKENIZED, you 
> could do
> something like this without a special field by iterating over your
> personName field. This might be reasonable if your index is fairly static
> and you could create this list at IndexReader open time, especially since
> you can use TermEnum.skipTo("personName", "a") etc.....
> Best
> Erick
> On 2/23/07, Paul Sundling (Webdaddy) <> wrote:
>> I have a requirement to support filtering search results by first 
>> letter.
>> This is relatively simple by adding a field to each index that
>> represents the first letter for that relevant index and then adding a
>> filter to the search.
>> The hard part is that I need to list all the letters you can filter BY.
>> So if there are no names that start with S, it shouldn't appear as an
>> option.
>> Is there a simple and performant way to get a set of all the unique
>> values for a Field in the Hits returned?  There would probably only be
>> low number of unique values.
>> So let's say I have the following in my index:
>> letter, personName
>> m, mike smith
>> p, paul smith
>> g, george smith
>> g, glenda smith
>> I need to be able to display to the user that they can filter based on
>> M, P or G within their search for George.
>> I could do a compromise and for search results above a certain level,
>> show all letters and numbers, but it won't always give correct values.
>> Imagine this edge case: A search for george has 50,000 results, but only
>> a couple people had george as their last name.  Not many of the letters
>> would be valid filters.
>> Thanks for any ideas or approaches I overlooked.
>> Paul Sundling
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail:
>> For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message