Пример #1
0
        /// <summary>Returns the <see cref="FieldComparator" /> to use for
        /// sorting.
        ///
        /// <b>NOTE:</b> This API is experimental and might change in
        /// incompatible ways in the next release.
        ///
        /// </summary>
        /// <param name="numHits">number of top hits the queue will store
        /// </param>
        /// <param name="sortPos">position of this SortField within <see cref="Sort" />
        ///.  The comparator is primary if sortPos==0,
        /// secondary if sortPos==1, etc.  Some comparators can
        /// optimize themselves when they are the primary sort.
        /// </param>
        /// <returns> <see cref="FieldComparator" /> to use when sorting
        /// </returns>
        public virtual FieldComparator GetComparator(int numHits, int sortPos)
        {
            if (locale != null)
            {
                // TODO: it'd be nice to allow FieldCache.getStringIndex
                // to optionally accept a Locale so sorting could then use
                // the faster StringComparator impls
                return(new FieldComparator.StringComparatorLocale(numHits, field, locale));
            }

            switch (type)
            {
            case SortField.SCORE:
                return(new FieldComparator.RelevanceComparator(numHits));

            case SortField.DOC:
                return(new FieldComparator.DocComparator(numHits));

            case SortField.INT:
                return(new FieldComparator.IntComparator(numHits, field, parser));

            case SortField.FLOAT:
                return(new FieldComparator.FloatComparator(numHits, field, parser));

            case SortField.LONG:
                return(new FieldComparator.LongComparator(numHits, field, parser));

            case SortField.DOUBLE:
                return(new FieldComparator.DoubleComparator(numHits, field, parser));

            case SortField.BYTE:
                return(new FieldComparator.ByteComparator(numHits, field, parser));

            case SortField.SHORT:
                return(new FieldComparator.ShortComparator(numHits, field, parser));

            case SortField.CUSTOM:
                System.Diagnostics.Debug.Assert(comparatorSource != null);
                return(comparatorSource.NewComparator(field, numHits, sortPos, reverse));

            case SortField.STRING:
                return(new FieldComparator.StringOrdValComparator(numHits, field, sortPos, reverse));

            case SortField.STRING_VAL:
                return(new FieldComparator.StringValComparator(numHits, field));

            default:
                throw new System.SystemException("Illegal sort type: " + type);
            }
        }
Пример #2
0
        /// <summary>
        /// Returns the <seealso cref="FieldComparator"/> to use for
        /// sorting.
        ///
        /// @lucene.experimental
        /// </summary>
        /// <param name="numHits"> number of top hits the queue will store </param>
        /// <param name="sortPos"> position of this SortField within {@link
        ///   Sort}.  The comparator is primary if sortPos==0,
        ///   secondary if sortPos==1, etc.  Some comparators can
        ///   optimize themselves when they are the primary sort. </param>
        /// <returns> <seealso cref="FieldComparator"/> to use when sorting </returns>
        public virtual FieldComparator GetComparator(int numHits, int sortPos)
        {
            switch (type)
            {
            case Type_e.SCORE:
                return(new FieldComparator.RelevanceComparator(numHits));

            case Type_e.DOC:
                return(new FieldComparator.DocComparator(numHits));

            case Type_e.INT:
                return(new FieldComparator.IntComparator(numHits, field, parser, (int?)missingValue));

            case Type_e.FLOAT:
                return(new FieldComparator.FloatComparator(numHits, field, parser, (float?)missingValue));

            case Type_e.LONG:
                return(new FieldComparator.LongComparator(numHits, field, parser, (long?)missingValue));

            case Type_e.DOUBLE:
                return(new FieldComparator.DoubleComparator(numHits, field, parser, (double?)missingValue));

            case Type_e.BYTE:
                return(new FieldComparator.ByteComparator(numHits, field, parser, (sbyte?)missingValue));

            case Type_e.SHORT:
                return(new FieldComparator.ShortComparator(numHits, field, parser, (short?)missingValue));

            case Type_e.CUSTOM:
                Debug.Assert(comparatorSource != null);
                return(comparatorSource.NewComparator(field, numHits, sortPos, reverse));

            case Type_e.STRING:
                return(new FieldComparator.TermOrdValComparator(numHits, field, missingValue == STRING_LAST));

            case Type_e.STRING_VAL:
                // TODO: should we remove this?  who really uses it?
                return(new FieldComparator.TermValComparator(numHits, field));

            case Type_e.REWRITEABLE:
                throw new InvalidOperationException("SortField needs to be rewritten through Sort.rewrite(..) and SortField.rewrite(..)");

            default:
                throw new InvalidOperationException("Illegal sort type: " + type);
            }
        }