5

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?

aaron
  • 349
  • 3
  • 10

3 Answers3

12

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.

Frederik Struck-Schøning
  • 12,981
  • 8
  • 59
  • 68
L.B
  • 114,136
  • 19
  • 178
  • 224
-2

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

Xodarap
  • 11,581
  • 11
  • 56
  • 94
  • That's what I'm using now, but I only need one field so I was just wondering if there was a way to only return that field instead of the whole document. – aaron Jan 27 '12 at 19:35
  • @aaron: I'm still not certain what you mean by "return." The resulting text from `Document.get` is just the text of that one field. If you mean you don't want to store the values of other fields, simply mark them as not stored. – Xodarap Jan 27 '12 at 21:57
  • -1 but `reader.Document` loads whole document, not just a single field – L.B Jan 28 '12 at 20:06
-2

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.

Frederik Struck-Schøning
  • 12,981
  • 8
  • 59
  • 68
SharpBarb
  • 1,590
  • 3
  • 16
  • 40
  • I get what you're saying and that's what I'm doing now. I'm trying to limit what fields are included in the doc. – aaron Jan 27 '12 at 21:37
  • You didnt ask how to limit which fields are included in the doc. You asked how to return a certain field in the results. Im not sure you're aaking the right question. – SharpBarb Jan 27 '12 at 22:25
  • @SharpBarb, `You asked how to return a certain field in the results. Im not sure you're aaking the right question` Please read the question again after reading my answer. I think I will make sense then – L.B Jan 28 '12 at 22:06