In Lucene. Net, how does one obtain the next Document in FSDirectory storage mode?

Source: Internet
Author: User

The best way to prevent forgetting is to write it down.

This is the simplest Search code:

        public void Search()        {            var dir=FSDirectory.Open(new DirectoryInfo("xxx"));            var searcher = new IndexSearcher(dir, true);            var query = new TermQuery(new Term("Title", "jinzhao"));            var tops=searcher.Search(query,100);            foreach(var top in tops)            {                var doc=searcher.Doc(top);                Output(doc);            }        }

A complete document is returned in a red sentence, which is the document returned by IndexReader (Lucene. Net. Index. IndexReader) in search. The method is as follows:

public abstract Document Document(int n, FieldSelector fieldSelector);

The following is the implementation of this class:

Their relationships are as follows:

MultiReader and ParallelReader maintain a set of IndexReader (these IndexReader may be implemented by the following, but does not include SegmentReader) and encapsulate the method for accessing multiple readers, the principle is the most common offset method in lucene;

In addition to SegmentReader, DirectoryReader simulates a directory. Just like an index folder, it maintains the implementation of a set of SegmentReader. The principle is as above;

SegmentReader is the smallest unit for reading documents. It no longer maintains any sub-IndexReader. After receiving the ID, it reads the fields of this document through public sealed class FieldsReader (the core of Lucene is the document, A document consists of several fields). The loading methods include loading immediately, loading specified fields immediately, and loading the specified fields lazily. The method is as follows:

public /*internal*/ Document Doc(int n, FieldSelector fieldSelector){SeekIndex(n);long position = indexStream.ReadLong();fieldsStream.Seek(position);Document doc = new Document();int numFields = fieldsStream.ReadVInt();for (int i = 0; i < numFields; i++){int fieldNumber = fieldsStream.ReadVInt();FieldInfo fi = fieldInfos.FieldInfo(fieldNumber);FieldSelectorResult acceptField = fieldSelector == null?FieldSelectorResult.LOAD:fieldSelector.Accept(fi.name);byte bits = fieldsStream.ReadByte();System.Diagnostics.Debug.Assert(bits <= FieldsWriter.FIELD_IS_COMPRESSED + FieldsWriter.FIELD_IS_TOKENIZED + FieldsWriter.FIELD_IS_BINARY);bool compressed = (bits & FieldsWriter.FIELD_IS_COMPRESSED) != 0;bool tokenize = (bits & FieldsWriter.FIELD_IS_TOKENIZED) != 0;bool binary = (bits & FieldsWriter.FIELD_IS_BINARY) != 0;//TODO: Find an alternative approach here if this list continues to grow beyond the//list of 5 or 6 currently here.  See Lucene 762 for discussionif (acceptField.Equals(FieldSelectorResult.LOAD)){AddField(doc, fi, binary, compressed, tokenize);}else if (acceptField.Equals(FieldSelectorResult.LOAD_FOR_MERGE)){AddFieldForMerge(doc, fi, binary, compressed, tokenize);}else if (acceptField.Equals(FieldSelectorResult.LOAD_AND_BREAK)){AddField(doc, fi, binary, compressed, tokenize);break; //Get out of this loop}else if (acceptField.Equals(FieldSelectorResult.LAZY_LOAD)){AddFieldLazy(doc, fi, binary, compressed, tokenize);}else if (acceptField.Equals(FieldSelectorResult.SIZE)){SkipField(binary, compressed, AddFieldSize(doc, fi, binary, compressed));}else if (acceptField.Equals(FieldSelectorResult.SIZE_AND_BREAK)){AddFieldSize(doc, fi, binary, compressed);break;}else{SkipField(binary, compressed);}}return doc;}

The red mark is the implementation of IndexInput, which is a specific reading method. The implementation is generally implemented in the storage class in the form of nesting public. For example, the implementation of the example here is as follows:

        public /*protected internal*/class SimpleFSIndexInput : BufferedIndexInput, System.ICloneable        {            protected internal class Descriptor : System.IO.BinaryReader            {                // remember if the file is open, so that we don't try to close it                // more than once                protected internal volatile bool isOpen;                internal long position;                internal long length;                public Descriptor(/*FSIndexInput enclosingInstance,*/ System.IO.FileInfo file, System.IO.FileAccess mode)                    : base(new System.IO.FileStream(file.FullName, System.IO.FileMode.Open, mode, System.IO.FileShare.ReadWrite))                {                    isOpen = true;                    length = file.Length;                }                public override void Close()                {                    if (isOpen)                    {                        isOpen = false;                        base.Close();                    }                }                ~Descriptor()                {                    try                    {                        Close();                    }                    finally                    {                    }                }            }

We can see that the last field is read from the file by System. IO. BinaryReader.

.

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.