0

For example, if I have got a field in my lucene index that is called, say "color". And of course it's value differs.

Then i have a "Advanced search page" with a dropdown, in that dropdown I would like to display all the available values (distinct) that the "color" field contains.

Lucene version is Version.LUCENE_29.

EDIT: Found a solution, Find all available values for a field in lucene .net

   private List<string> GetAvailableFields(string fieldName)
   {
       List<string> fieldValues;

       using (var readerRepository = new LucineRepository(RepositoryPath))
       {
           var reader = readerRepository.Reader;
           fieldValues = reader.UniqueTermsFromField(fieldName).ToList(); 
           reader.Close();
       }

       return fieldValues;
   }

public static class ReaderExtentions
{
    public static IEnumerable<string> UniqueTermsFromField(this IndexReader reader, string field)
    {
        var termEnum = reader.Terms(new Term(field));

        do
        {
            var currentTerm = termEnum.Term();

            if (currentTerm.Field() != field)
                yield break;

            yield return currentTerm.Text();
        } while (termEnum.Next());
    }
}

Cheers, Tommy.

Community
  • 1
  • 1
noshitsherlock
  • 1,103
  • 2
  • 11
  • 28

1 Answers1

0

Lucene doesn't provide 'distinct' functionality.

you have to write your own code to achieve that

you can try the below code snippet:

var document = new Document();
document.Add(new Field("color", "foo", Field.Store.YES, Field.Index.NOT_ANALYZED));
...

TermEnum terms = indexReader.Terms(new Term("color"));
// enumerate the colors
Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70