4
votes

I'm using Lucene to search an index and it works fine. My only issue is that I only need one particular field of what is being returned. Can you specify to Lucene to only return a certain field in the results and not the entire document?

3

3 Answers

11
votes

This is why FieldSelector class exists. You can implement a class like this

class MyFieldSelector : FieldSelector
{
    public FieldSelectorResult Accept(string fieldName)
    {
        if (fieldName == "field1") return FieldSelectorResult.LOAD_AND_BREAK;
        return FieldSelectorResult.NO_LOAD;
    }
}

and use it as indexReader.Document(docid,new MyFieldSelector());

If you are interested in loading a small field, this will prevent to load large fields which, in turn, means a speed-up in loading documents. I think you can find much more detailed info by some googling.

-2
votes

What do you mean "return certain fields"? The Document.get() function returns just the field you request.

-2
votes

Yes, you can definitely do what you are asking. All you have to do is include the field name (case-sensitive) in the document.get() method.

string fieldNameText = doc.Get("fieldName"); 

FYI, it's usually a good idea to include some code in your questions. It makes it easier to provide a good answer.