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.