searchlucenelucene.net

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?


Solution

  • 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.