1

i just started learning how lucene works and am trying to implement it in a site i allready wrote with mysql.

i have a field named city in my documents, and i want to get all the values for city from the documents.

i have found this question ( which is exactly what i need ) Get all lucene values that have a certain fieldName but all they show there is a line of code, and as i said, i am not experienced enough to understand how to implement that.

can someone please help me with some code to implement IndexReader.Open(directory,true).Terms(new Term("city", String.Empty));

what comes before / after that declaration ?

i have tried this:

System.IO.DirectoryInfo directoryPath = new System.IO.DirectoryInfo(Server.MapPath("LuceneIndex"));
    Directory directory = FSDirectory.Open(directoryPath);
    Lucene.Net.Index.TermEnum iReader = IndexReader.Open(directory,true).Terms(new Term("city", String.Empty));

but how do i iterate over the results?

Community
  • 1
  • 1
Rafael Herscovici
  • 16,558
  • 19
  • 65
  • 93
  • See this code http://stackoverflow.com/questions/7327375/find-all-available-values-for-a-field-in-lucene-net/7330670#7330670 – L.B Oct 16 '11 at 14:54

2 Answers2

0

I'm not familiar with C# API, but it looks very similar to the Java one.

What this code does is to get an instance of IndexReader with read-only access that is used to read data from Lucene index segments stored in directory. Then it gets an enumeration of all terms, starting at the given one. Dictionary (index part that stores the terms) in Lucene is organized .tis files, ordered lexicographically first by field name and then by term text.

So this statement gives you an enumeration of all term texts, starting at the beginning of the field city (besides: in Java you would rather write new Term("city")). You now need to find out the C# API of this enumeration, and then walk through it till you will get a Term that have something different as the field().

A final note: generally, you should avoid doing things like this: it may for example limit your ability to distribute the index. If it turns out that this is a thing that you are doing at the very beginning of using Lucene, then it's probable that you are using it more like a document database than a search library.

Artur Nowak
  • 5,254
  • 3
  • 22
  • 32
  • basicly i am using it as a document database, since its a lot faster then anyother DB i tried. i build a phonebook using lucene, and at some point i need to show only cities, thats why i followed that path. – Rafael Herscovici Oct 16 '11 at 11:28
0

This loop should iterate over all the terms:

Term curTerm = iReader.Term();
bool hasNext = true;
while (curTerm != null && hasNext)
{
    //do whatever you need with the current term....
    hasNext = iReader.Next();
    curTerm = iReader.Term();
}
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208