aaron
aaron

Reputation: 357

Return Only Certain Fields From Lucene Search

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?

Upvotes: 6

Views: 4556

Answers (3)

SharpBarb
SharpBarb

Reputation: 1590

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.

Upvotes: -2

L.B
L.B

Reputation: 116188

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.

Upvotes: 13

Xodarap
Xodarap

Reputation: 11859

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

Upvotes: -2

Related Questions