Expert: a FieldComparator compares hits so as to determine their sort order when collecting the top results with TopFieldCollector . The concrete public FieldComparator classes here correspond to the SortField types.

This API is designed to achieve high performance sorting, by exposing a tight interaction with FieldValueHitQueue as it visits hits. Whenever a hit is competitive, it's enrolled into a virtual slot, which is an int ranging from 0 to numHits-1. The FieldComparator is made aware of segment transitions during searching in case any internal state it's tracking needs to be recomputed during these transitions.

A comparator must define these functions:

Compare Compare a hit at 'slot a' with hit 'slot b'. SetBottom This method is called by FieldValueHitQueue to notify the FieldComparator of the current weakest ("bottom") slot. Note that this slot may not hold the weakest value according to your comparator, in cases where your comparator is not the primary one (ie, is only used to break ties from the comparators before it). CompareBottom Compare a new hit (docID) against the "weakest" (bottom) entry in the queue. Copy Installs a new hit into the priority queue. The FieldValueHitQueue calls this method when a new hit is competitive. SetNextReader Invoked when the search is switching to the next segment. You may need to update internal state of the comparator, for example retrieving new values from the FieldCache. Value Return the sort value stored in the specified slot. This is only called at the end of the search, in order to populate FieldDoc.fields when returning the top results. NOTE: This API is experimental and might change in incompatible ways in the next release.

        // prevent instantiation and extension.
        private FieldValueHitQueue(SortField[] fields)
        {
            // When we get here, fields.length is guaranteed to be > 0, therefore no
            // need to check it again.

            // All these are required by this class's API - need to return arrays.
            // Therefore even in the case of a single comparator, create an array
            // anyway.
            this.fields = fields;
            int numComparators = fields.Length;

            comparators = new FieldComparator[numComparators];
            reverseMul  = new int[numComparators];
        }
            public OneComparatorFieldValueHitQueue(SortField[] fields, int size) : base(fields)
            {
                if (fields.Length == 0)
                {
                    throw new System.ArgumentException("Sort must contain at least one field");
                }

                SortField field = fields[0];

                comparator    = field.GetComparator(size, 0);
                oneReverseMul = field.reverse?-1:1;

                comparators[0] = comparator;
                reverseMul[0]  = oneReverseMul;

                Initialize(size);
            }
Пример #3
0
            public OneComparatorFieldValueHitQueue(ArraySegment <SortField> fields, int size) : base(fields)
            {
                if (fields.Count == 0)
                {
                    throw new System.ArgumentException("Sort must contain at least one field");
                }

                SortField field = fields.Array[0 + fields.Offset];

                comparator    = field.GetComparator(size, 0);
                oneReverseMul = field.reverse?-1:1;

                comparators[0] = comparator;
                reverseMul[0]  = oneReverseMul;

                Initialize(size);
            }
Пример #4
0
            public OneComparatorFieldValueHitQueue(SortField[] fields, int size) : base(fields)
            {
                if (fields.Length == 0)
                {
                    throw new System.ArgumentException("Sort must contain at least one field");
                }

                SortField field = fields[0];

                // AUTO is resolved before we are called
                System.Diagnostics.Debug.Assert(field.GetType() != SortField.AUTO);
                comparator    = field.GetComparator(size, 0);
                oneReverseMul = field.reverse?-1:1;

                comparators[0] = comparator;
                reverseMul[0]  = oneReverseMul;

                Initialize(size);
            }
Пример #5
0
            // Returns true if first is < second
            public override bool LessThan(ShardRef first, ShardRef second)
            {
                Debug.Assert(first != second);
                FieldDoc firstFD  = (FieldDoc)ShardHits[first.ShardIndex][first.HitIndex];
                FieldDoc secondFD = (FieldDoc)ShardHits[second.ShardIndex][second.HitIndex];

                //System.out.println("  lessThan:\n     first=" + first + " doc=" + firstFD.doc + " score=" + firstFD.score + "\n    second=" + second + " doc=" + secondFD.doc + " score=" + secondFD.score);

                for (int compIDX = 0; compIDX < comparators.Length; compIDX++)
                {
                    FieldComparator comp = comparators[compIDX];
                    //System.out.println("    cmp idx=" + compIDX + " cmp1=" + firstFD.fields[compIDX] + " cmp2=" + secondFD.fields[compIDX] + " reverse=" + reverseMul[compIDX]);

                    int cmp = ReverseMul[compIDX] * comp.CompareValues(firstFD.Fields[compIDX], secondFD.Fields[compIDX]);

                    if (cmp != 0)
                    {
                        //System.out.println("    return " + (cmp < 0));
                        return(cmp < 0);
                    }
                }

                // Tie break: earlier shard wins
                if (first.ShardIndex < second.ShardIndex)
                {
                    //System.out.println("    return tb true");
                    return(true);
                }
                else if (first.ShardIndex > second.ShardIndex)
                {
                    //System.out.println("    return tb false");
                    return(false);
                }
                else
                {
                    // Tie break in same shard: resolve however the
                    // shard had resolved it:
                    //System.out.println("    return tb " + (first.hitIndex < second.hitIndex));
                    Debug.Assert(first.HitIndex != second.HitIndex);
                    return(first.HitIndex < second.HitIndex);
                }
            }
Пример #6
0
			public OneComparatorFieldValueHitQueue(SortField[] fields, int size):base(fields)
			{
				if (fields.Length == 0)
				{
					throw new System.ArgumentException("Sort must contain at least one field");
				}
				
				SortField field = fields[0];
				// AUTO is resolved before we are called
				System.Diagnostics.Debug.Assert(field.GetType() != SortField.AUTO);
				comparator = field.GetComparator(size, 0);
				oneReverseMul = field.reverse?- 1:1;
				
				comparators[0] = comparator;
				reverseMul[0] = oneReverseMul;
				
				Initialize(size);
			}
Пример #7
0
		// prevent instantiation and extension.
		private FieldValueHitQueue(SortField[] fields)
		{
			// When we get here, fields.length is guaranteed to be > 0, therefore no
			// need to check it again.
			
			// All these are required by this class's API - need to return arrays.
			// Therefore even in the case of a single comparator, create an array
			// anyway.
			this.fields = fields;
			int numComparators = fields.Length;
			comparators = new FieldComparator[numComparators];
			reverseMul = new int[numComparators];
		}
Пример #8
0
 public OneComparatorNonScoringCollector(FieldValueHitQueue queue, int numHits, bool fillFields) : base(queue, numHits, fillFields)
 {
     comparator = queue.GetComparators()[0];
     reverseMul = queue.GetReverseMul()[0];
 }
Пример #9
0
 public OneComparatorNonScoringCollector(FieldValueHitQueue queue, int numHits, bool fillFields)
     : base(queue, numHits, fillFields)
 {
     comparator = queue.GetComparators()[0];
     reverseMul = queue.GetReverseMul()[0];
 }
 public LuceneCustomDocComparator(FieldComparator luceneComparator)
 {
     this._luceneComparator = luceneComparator;
 }
 public LuceneCustomDocComparatorSource(string fieldname, FieldComparator luceneComparator)
 {
     _fieldname = fieldname;
     _luceneComparator = luceneComparator;
 }