예제 #1
0
 public WildcardQuery(Term term)
     : base(term)
 {
     //will be removed in 3.0
     this.term = term;
     this.termContainsWildcard = (term.Text().IndexOf('*') != - 1) || (term.Text().IndexOf('?') != - 1);
 }
예제 #2
0
        public RegexTermEnum(IndexReader reader, Term term)
            : base()
        {
            field = term.Field();
            System.String text = term.Text();

            pattern = new Pattern(text);

            // Find the first regex character position, to find the
            // maximum prefix to use for term enumeration
            int index = 0;
            while (index < text.Length)
            {
                char c = text[index];

                if (!System.Char.IsLetterOrDigit(c))
                    break;

                index++;
            }

            pre = text.Substring(0, (index) - (0));

            SetEnum(reader.Terms(new Term(term.Field(), pre)));
        }
예제 #3
0
		public /*protected internal*/ override bool TermCompare(Term term)
		{
			if ((System.Object) term.Field() == (System.Object) prefix.Field() && term.Text().StartsWith(prefix.Text()))
			{
				return true;
			}
			endEnum = true;
			return false;
		}
예제 #4
0
		protected internal override bool TermCompare(Term term)
		{
			if (field == term.Field())
			{
				System.String searchText = term.Text();
				if (searchText.StartsWith(pre))
				{
					return WildcardEquals(text, 0, searchText, preLen);
				}
			}
			endEnum = true;
			return false;
		}
예제 #5
0
		protected internal override bool TermCompare(Term term)
		{
			if ((System.Object) field == (System.Object) term.Field())
			{
				System.String searchText = term.Text();
				if (searchText.StartsWith(pre))
				{
                    return pattern.Match(searchText).Success;
				}
			}
			endEnum = true;
			return false;
		}
예제 #6
0
			public override int DocFreq(Term term)
			{
				int df;
				try
				{
					df = ((System.Int32) dfMap[term]);
				}
				catch (System.NullReferenceException)
				{
					throw new System.ArgumentException("df for term " + term.Text() + " not available");
				}
				return df;
			}
예제 #7
0
		/// <summary> Creates a new <code>WildcardTermEnum</code>.  Passing in a
		/// {@link Lucene.Net.index.Term Term} that does not contain a
		/// <code>WILDCARD_CHAR</code> will cause an exception to be thrown.
		/// <p>
		/// After calling the constructor the enumeration is already pointing to the first 
		/// valid term if such a term exists.
		/// </summary>
		public WildcardTermEnum(IndexReader reader, Term term):base()
		{
			searchTerm = term;
			field = searchTerm.Field();
			text = searchTerm.Text();
			
			int sidx = text.IndexOf((System.Char) WILDCARD_STRING);
			int cidx = text.IndexOf((System.Char) WILDCARD_CHAR);
			int idx = sidx;
			if (idx == - 1)
			{
				idx = cidx;
			}
			else if (cidx >= 0)
			{
				idx = System.Math.Min(idx, cidx);
			}
			
			pre = searchTerm.Text().Substring(0, (idx) - (0));
			preLen = pre.Length;
			text = text.Substring(preLen);
			SetEnum(reader.Terms(new Term(searchTerm.Field(), pre)));
		}
예제 #8
0
            protected internal override System.Object CreateValue(IndexReader reader, Entry entryKey)
            {
                Entry entry = (Entry)entryKey;

                System.String field  = entry.field;
                ShortParser   parser = (ShortParser)entry.custom;

                if (parser == null)
                {
                    return(wrapper.GetShorts(reader, field, Lucene.Net.Search.FieldCache_Fields.DEFAULT_SHORT_PARSER));
                }
                short[]  retArray = new short[reader.MaxDoc()];
                TermDocs termDocs = reader.TermDocs();
                TermEnum termEnum = reader.Terms(new Term(field));

                try
                {
                    do
                    {
                        Term term = termEnum.Term();
                        if (term == null || (System.Object)term.Field() != (System.Object)field)
                        {
                            break;
                        }
                        short termval = parser.ParseShort(term.Text());
                        termDocs.Seek(termEnum);
                        while (termDocs.Next())
                        {
                            retArray[termDocs.Doc()] = termval;
                        }
                    }while (termEnum.Next());
                }
                catch (StopFillCacheException stop)
                {
                }
                finally
                {
                    termDocs.Close();
                    termEnum.Close();
                }
                return(retArray);
            }
예제 #9
0
		/// <summary> Create a new FuzzyQuery that will match terms with a similarity 
		/// of at least <code>minimumSimilarity</code> to <code>term</code>.
		/// If a <code>prefixLength</code> &gt; 0 is specified, a common prefix
		/// of that length is also required.
		/// 
		/// </summary>
		/// <param name="term">the term to search for
		/// </param>
		/// <param name="minimumSimilarity">a value between 0 and 1 to set the required similarity
		/// between the query term and the matching terms. For example, for a
		/// <code>minimumSimilarity</code> of <code>0.5</code> a term of the same length
		/// as the query term is considered similar to the query term if the edit distance
		/// between both terms is less than <code>length(term)*0.5</code>
		/// </param>
		/// <param name="prefixLength">length of common (non-fuzzy) prefix
		/// </param>
		/// <throws>  IllegalArgumentException if minimumSimilarity is &gt;= 1 or &lt; 0 </throws>
		/// <summary> or if prefixLength &lt; 0
		/// </summary>
		public FuzzyQuery(Term term, float minimumSimilarity, int prefixLength):base(term)
		{ // will be removed in 3.0
			this.term = term;
			
			if (minimumSimilarity >= 1.0f)
				throw new System.ArgumentException("minimumSimilarity >= 1");
			else if (minimumSimilarity < 0.0f)
				throw new System.ArgumentException("minimumSimilarity < 0");
			if (prefixLength < 0)
				throw new System.ArgumentException("prefixLength < 0");
			
			if (term.Text().Length > 1.0f / (1.0f - minimumSimilarity))
			{
				this.termLongEnough = true;
			}
			
			this.minimumSimilarity = minimumSimilarity;
			this.prefixLength = prefixLength;
			rewriteMethod = SCORING_BOOLEAN_QUERY_REWRITE;
		}
        //fieldname MUST be interned prior to this call
        private static void GetTerms(Query query, System.Collections.Hashtable terms, bool prohibited, System.String fieldName)
        {
            try
            {
                System.Collections.Hashtable nonWeightedTerms = new System.Collections.Hashtable();
                query.ExtractTerms(nonWeightedTerms);

                System.Collections.IDictionaryEnumerator iter = nonWeightedTerms.GetEnumerator();
                while (iter.MoveNext())
                {
                    Term term = (Term)iter.Value;
                    if ((fieldName == null) || (term.Field() == fieldName))
                    {
                        WeightedTerm temp = new WeightedTerm(query.GetBoost(), term.Text());
                        terms.Add(temp, temp);
                    }
                }
            }
            catch (System.NotSupportedException ignore)
            {
                //this is non-fatal for our purposes
            }
        }
예제 #11
0
 // inherit javadocs
 public virtual float[] GetFloats(IndexReader reader, System.String field, FloatParser parser)
 {
     field = String.Intern(field);
     System.Object ret = Lookup(reader, field, parser);
     if (ret == null)
     {
         float[]  retArray = new float[reader.MaxDoc()];
         TermDocs termDocs = reader.TermDocs();
         TermEnum termEnum = reader.Terms(new Term(field, ""));
         try
         {
             do
             {
                 Term term = termEnum.Term();
                 if (term == null || term.Field() != field)
                 {
                     break;
                 }
                 float termval;
                 termval = SupportClass.Single.Parse(term.Text());
                 termDocs.Seek(termEnum);
                 while (termDocs.Next())
                 {
                     retArray[termDocs.Doc()] = termval;
                 }
             }while (termEnum.Next());
         }
         finally
         {
             termDocs.Close();
             termEnum.Close();
         }
         Store(reader, field, parser, retArray);
         return(retArray);
     }
     return((float[])ret);
 }
예제 #12
0
 // inherit javadocs
 public virtual System.IComparable[] GetCustom(IndexReader reader, System.String field, SortComparator comparator)
 {
     field = String.Intern(field);
     System.Object ret = Lookup(reader, field, comparator);
     if (ret == null)
     {
         System.IComparable[] retArray = new System.IComparable[reader.MaxDoc()];
         TermDocs             termDocs = reader.TermDocs();
         TermEnum             termEnum = reader.Terms(new Term(field, ""));
         try
         {
             do
             {
                 Term term = termEnum.Term();
                 if (term == null || term.Field() != field)
                 {
                     break;
                 }
                 System.IComparable termval = comparator.GetComparable(term.Text());
                 termDocs.Seek(termEnum);
                 while (termDocs.Next())
                 {
                     retArray[termDocs.Doc()] = termval;
                 }
             }while (termEnum.Next());
         }
         finally
         {
             termDocs.Close();
             termEnum.Close();
         }
         Store(reader, field, comparator, retArray);
         return(retArray);
     }
     return((System.IComparable[])ret);
 }
예제 #13
0
		public PrefixTermEnum(IndexReader reader, Term prefix)
		{
			this.prefix = prefix;
			
			SetEnum(reader.Terms(new Term(prefix.Field(), prefix.Text())));
		}
예제 #14
0
		public /*protected internal*/ override bool TermCompare(Term term)
		{
			if (collator == null)
			{
				// Use Unicode code point ordering
				bool checkLower = false;
				if (!includeLower)
				// make adjustments to set to exclusive
					checkLower = true;
				if (term != null && (System.Object) term.Field() == (System.Object) field)
				{
					// interned comparison
					if (!checkLower || null == lowerTermText || String.CompareOrdinal(term.Text(), lowerTermText) > 0)
					{
						checkLower = false;
						if (upperTermText != null)
						{
							int compare = String.CompareOrdinal(upperTermText, term.Text());
							/*
							* if beyond the upper term, or is exclusive and this is equal to
							* the upper term, break out
							*/
							if ((compare < 0) || (!includeUpper && compare == 0))
							{
								endEnum = true;
								return false;
							}
						}
						return true;
					}
				}
				else
				{
					// break
					endEnum = true;
					return false;
				}
				return false;
			}
			else
			{
				if (term != null && (System.Object) term.Field() == (System.Object) field)
				{
					// interned comparison
					if ((lowerTermText == null || (includeLower?collator.Compare(term.Text().ToString(), lowerTermText.ToString()) >= 0:collator.Compare(term.Text().ToString(), lowerTermText.ToString()) > 0)) && (upperTermText == null || (includeUpper?collator.Compare(term.Text().ToString(), upperTermText.ToString()) <= 0:collator.Compare(term.Text().ToString(), upperTermText.ToString()) < 0)))
					{
						return true;
					}
					return false;
				}
				endEnum = true;
				return false;
			}
		}
            protected internal override object CreateValue(IndexReader reader, object fieldKey)
            {
                System.String field      = String.Intern(((System.String)fieldKey));
                TermEnum      enumerator = reader.Terms(new Term(field));

                try
                {
                    Term term = enumerator.Term();
                    if (term == null)
                    {
                        throw new System.SystemException("no terms in field " + field + " - cannot determine sort type");
                    }
                    object ret = null;
                    if ((object)term.Field() == (object)field)
                    {
                        System.String termtext = term.Text().Trim();

                        /**
                         * Java 1.4 level code:
                         *
                         * if (pIntegers.matcher(termtext).matches())
                         * return IntegerSortedHitQueue.comparator (reader, enumerator, field);
                         *
                         * else if (pFloats.matcher(termtext).matches())
                         * return FloatSortedHitQueue.comparator (reader, enumerator, field);
                         */

                        // Java 1.3 level code:
                        try
                        {
                            int  parsedIntValue;
                            long parsedLongValue;
                            if (int.TryParse(termtext, out parsedIntValue))
                            {
                                ret = Enclosing_Instance.GetInts(reader, field);
                            }
                            else if (long.TryParse(termtext, out parsedLongValue))
                            {
                                ret = ((ExtendedFieldCacheImpl)Enclosing_Instance).GetLongs(reader, field);
                            }
                            else
                            {
                                float f = 0.0f;
                                if (SupportClass.Single.TryParse(termtext, out f))
                                {
                                    ret = Enclosing_Instance.GetFloats(reader, field);
                                }
                                else
                                {
                                    ret = Enclosing_Instance.GetStringIndex(reader, field);
                                }
                            }
                        }
                        catch (System.Exception)
                        {
                            ret = Enclosing_Instance.GetStringIndex(reader, field);
                        }
                    }
                    else
                    {
                        throw new System.SystemException("field \"" + field + "\" does not appear to be indexed");
                    }
                    return(ret);
                }
                finally
                {
                    enumerator.Close();
                }
            }
예제 #16
0
            public override Explanation Explain(IndexReader reader, int doc)
            {
                Explanation result = new Explanation();

                result.SetDescription("weight(" + GetQuery() + " in " + doc + "), product of:");

                System.Text.StringBuilder docFreqs = new System.Text.StringBuilder();
                System.Text.StringBuilder query    = new System.Text.StringBuilder();
                query.Append('\"');
                docFreqs.Append(idfExp.Explain());
                for (int i = 0; i < Enclosing_Instance.terms.Count; i++)
                {
                    if (i != 0)
                    {
                        query.Append(" ");
                    }

                    Term term = (Term)Enclosing_Instance.terms[i];

                    query.Append(term.Text());
                }
                query.Append('\"');

                Explanation idfExpl = new Explanation(idf, "idf(" + Enclosing_Instance.field + ":" + docFreqs + ")");

                // explain query weight
                Explanation queryExpl = new Explanation();

                queryExpl.SetDescription("queryWeight(" + GetQuery() + "), product of:");

                Explanation boostExpl = new Explanation(Enclosing_Instance.GetBoost(), "boost");

                if (Enclosing_Instance.GetBoost() != 1.0f)
                {
                    queryExpl.AddDetail(boostExpl);
                }
                queryExpl.AddDetail(idfExpl);

                Explanation queryNormExpl = new Explanation(queryNorm, "queryNorm");

                queryExpl.AddDetail(queryNormExpl);

                queryExpl.SetValue(boostExpl.GetValue() * idfExpl.GetValue() * queryNormExpl.GetValue());

                result.AddDetail(queryExpl);

                // explain field weight
                Explanation fieldExpl = new Explanation();

                fieldExpl.SetDescription("fieldWeight(" + Enclosing_Instance.field + ":" + query + " in " + doc + "), product of:");

                Scorer scorer = Scorer(reader, true, false);

                if (scorer == null)
                {
                    return(new Explanation(0.0f, "no matching docs"));
                }
                Explanation tfExpl = scorer.Explain(doc);

                fieldExpl.AddDetail(tfExpl);
                fieldExpl.AddDetail(idfExpl);

                Explanation fieldNormExpl = new Explanation();

                byte[] fieldNorms = reader.Norms(Enclosing_Instance.field);
                float  fieldNorm  = fieldNorms != null?Similarity.DecodeNorm(fieldNorms[doc]) : 1.0f;

                fieldNormExpl.SetValue(fieldNorm);
                fieldNormExpl.SetDescription("fieldNorm(field=" + Enclosing_Instance.field + ", doc=" + doc + ")");
                fieldExpl.AddDetail(fieldNormExpl);

                fieldExpl.SetValue(tfExpl.GetValue() * idfExpl.GetValue() * fieldNormExpl.GetValue());

                result.AddDetail(fieldExpl);

                // combine them
                result.SetValue(queryExpl.GetValue() * fieldExpl.GetValue());

                if (queryExpl.GetValue() == 1.0f)
                {
                    return(fieldExpl);
                }

                return(result);
            }
예제 #17
0
        public virtual Explanation Explain(IndexReader reader, int doc)
        {
            Explanation result = new Explanation();

            result.SetDescription("weight(" + GetQuery() + " in " + doc + "), product of:");
            System.String field = ((SpanQuery)GetQuery()).GetField();

            System.Text.StringBuilder      docFreqs = new System.Text.StringBuilder();
            System.Collections.IEnumerator i        = terms.GetEnumerator();
            while (i.MoveNext())
            {
                Term term = (Term)i.Current;
                docFreqs.Append(term.Text());
                docFreqs.Append("=");
                docFreqs.Append(reader.DocFreq(term));

                if (i.MoveNext())
                {
                    docFreqs.Append(" ");
                }
            }

            Explanation idfExpl = new Explanation(idf, "idf(" + field + ": " + docFreqs + ")");

            // explain query weight
            Explanation queryExpl = new Explanation();

            queryExpl.SetDescription("queryWeight(" + GetQuery() + "), product of:");

            Explanation boostExpl = new Explanation(GetQuery().GetBoost(), "boost");

            if (GetQuery().GetBoost() != 1.0f)
            {
                queryExpl.AddDetail(boostExpl);
            }
            queryExpl.AddDetail(idfExpl);

            Explanation queryNormExpl = new Explanation(queryNorm, "queryNorm");

            queryExpl.AddDetail(queryNormExpl);

            queryExpl.SetValue(boostExpl.GetValue() * idfExpl.GetValue() * queryNormExpl.GetValue());

            result.AddDetail(queryExpl);

            // explain field weight
            Explanation fieldExpl = new Explanation();

            fieldExpl.SetDescription("fieldWeight(" + field + ":" + query.ToString(field) + " in " + doc + "), product of:");

            Explanation tfExpl = Scorer(reader).Explain(doc);

            fieldExpl.AddDetail(tfExpl);
            fieldExpl.AddDetail(idfExpl);

            Explanation fieldNormExpl = new Explanation();

            byte[] fieldNorms = reader.Norms(field);
            float  fieldNorm  = fieldNorms != null?Similarity.DecodeNorm(fieldNorms[doc]) : 0.0f;

            fieldNormExpl.SetValue(fieldNorm);
            fieldNormExpl.SetDescription("fieldNorm(field=" + field + ", doc=" + doc + ")");
            fieldExpl.AddDetail(fieldNormExpl);

            fieldExpl.SetValue(tfExpl.GetValue() * idfExpl.GetValue() * fieldNormExpl.GetValue());

            result.AddDetail(fieldExpl);

            // combine them
            result.SetValue(queryExpl.GetValue() * fieldExpl.GetValue());

            if (queryExpl.GetValue() == 1.0f)
            {
                return(fieldExpl);
            }

            return(result);
        }
예제 #18
0
        /// <summary> Returns a DocIdSet with documents that should be
        /// permitted in search results.
        /// </summary>
        public override DocIdSet GetDocIdSet(IndexReader reader)
        {
            OpenBitSet bits = new OpenBitSet(reader.MaxDoc());

            TermEnum enumerator = (null != lowerTerm && collator == null ?
                                   reader.Terms(new Term(fieldName, lowerTerm)) :
                                   reader.Terms(new Term(fieldName)));

            try
            {
                if (enumerator.Term() == null)
                {
                    return(bits);
                }

                TermDocs termDocs = reader.TermDocs();
                try
                {
                    if (collator != null)
                    {
                        do
                        {
                            Term term = enumerator.Term();
                            if (term != null && term.Field().Equals(fieldName))
                            {
                                if ((lowerTerm == null ||
                                     (includeLower ? collator.Compare(term.Text(), lowerTerm) >= 0 : collator.Compare(term.Text(), lowerTerm) > 0)) &&
                                    (upperTerm == null ||
                                     (includeUpper ? collator.Compare(term.Text(), upperTerm) <= 0 : collator.Compare(term.Text(), upperTerm) < 0)))
                                {
                                    // term in range, lookup docs
                                    termDocs.Seek(enumerator.Term());
                                    while (termDocs.Next())
                                    {
                                        bits.Set(termDocs.Doc());
                                    }
                                }
                            }
                        }while (enumerator.Next());
                    }
                    else // null collator; using Unicode code point ordering
                    {
                        bool checkLower = false;
                        if (!includeLower) // make adjustments to set to exclusive
                        {
                            checkLower = true;
                        }
                        do
                        {
                            Term term = enumerator.Term();
                            if (term != null && term.Field().Equals(fieldName))
                            {
                                if (!checkLower || null == lowerTerm || String.CompareOrdinal(term.Text(), lowerTerm) > 0)
                                {
                                    checkLower = false;
                                    if (upperTerm != null)
                                    {
                                        int compare = String.CompareOrdinal(upperTerm, term.Text());

                                        /* if beyond the upper term, or is exclusive and
                                         * this is equal to the upper term, break out */
                                        if ((compare < 0) || (!includeUpper && compare == 0))
                                        {
                                            break;
                                        }
                                    }
                                    /* we have a good term, find the docs */

                                    termDocs.Seek(enumerator.Term());
                                    while (termDocs.Next())
                                    {
                                        bits.Set(termDocs.Doc());
                                    }
                                }
                            }
                            else
                            {
                                break;
                            }
                        }while (enumerator.Next());
                    }
                }
                finally
                {
                    termDocs.Close();
                }
            }
            finally
            {
                enumerator.Close();
            }

            return(bits);
        }
예제 #19
0
 private static void AddField(Document doc)
 {
     doc.Add(new StringField(A.Field(), A.Text(), Store.NO));
 }
예제 #20
0
            protected internal override System.Object CreateValue(IndexReader reader, Entry entryKey)
            {
                System.String   field    = StringHelper.Intern((System.String)entryKey.field);
                int[]           retArray = new int[reader.MaxDoc()];
                System.String[] mterms   = new System.String[reader.MaxDoc() + 1];
                TermDocs        termDocs = reader.TermDocs();
                TermEnum        termEnum = reader.Terms(new Term(field));
                int             t        = 0; // current term number

                // an entry for documents that have no terms in this field
                // should a document with no terms be at top or bottom?
                // this puts them at the top - if it is changed, FieldDocSortedHitQueue
                // needs to change as well.
                mterms[t++] = null;

                try
                {
                    do
                    {
                        Term term = termEnum.Term();
                        if (term == null || (System.Object)term.Field() != (System.Object)field)
                        {
                            break;
                        }

                        // store term text
                        // we expect that there is at most one term per document
                        if (t >= mterms.Length)
                        {
                            throw new System.SystemException("there are more terms than " + "documents in field \"" + field + "\", but it's impossible to sort on " + "tokenized fields");
                        }
                        mterms[t] = term.Text();

                        termDocs.Seek(termEnum);
                        while (termDocs.Next())
                        {
                            retArray[termDocs.Doc()] = t;
                        }

                        t++;
                    }while (termEnum.Next());
                }
                finally
                {
                    termDocs.Close();
                    termEnum.Close();
                }

                if (t == 0)
                {
                    // if there are no terms, make the term array
                    // have a single null entry
                    mterms = new System.String[1];
                }
                else if (t < mterms.Length)
                {
                    // if there are less terms than documents,
                    // trim off the dead array space
                    System.String[] terms = new System.String[t];
                    Array.Copy(mterms, 0, terms, 0, t);
                    mterms = terms;
                }

                StringIndex value_Renamed = new StringIndex(retArray, mterms);

                return(value_Renamed);
            }
예제 #21
0
        /// <summary>The pattern used to detect integer values in a field </summary>
        /// <summary>removed for java 1.3 compatibility
        /// protected static final Pattern pIntegers = Pattern.compile ("[0-9\\-]+");
        ///
        /// </summary>

        /// <summary>The pattern used to detect float values in a field </summary>
        /// <summary> removed for java 1.3 compatibility
        /// protected static final Object pFloats = Pattern.compile ("[0-9+\\-\\.eEfFdD]+");
        /// </summary>

        // inherit javadocs
        public virtual System.Object GetAuto(IndexReader reader, System.String field)
        {
            field = String.Intern(field);
            System.Object ret = Lookup(reader, field, SortField.AUTO);
            if (ret == null)
            {
                TermEnum enumerator = reader.Terms(new Term(field, ""));
                try
                {
                    Term term = enumerator.Term();
                    if (term == null)
                    {
                        throw new System.SystemException("no terms in field " + field + " - cannot determine sort type");
                    }
                    if (term.Field() == field)
                    {
                        System.String termtext = term.Text().Trim();

                        /**
                         * Java 1.4 level code:
                         *
                         * if (pIntegers.matcher(termtext).matches())
                         * return IntegerSortedHitQueue.comparator (reader, enumerator, field);
                         *
                         * else if (pFloats.matcher(termtext).matches())
                         * return FloatSortedHitQueue.comparator (reader, enumerator, field);
                         */

                        // Java 1.3 level code:
                        try
                        {
                            System.Int32.Parse(termtext);
                            ret = GetInts(reader, field);
                        }
                        catch (System.FormatException nfe1)
                        {
                            try
                            {
                                System.Single.Parse(termtext);
                                ret = GetFloats(reader, field);
                            }
                            catch (System.FormatException nfe2)
                            {
                                ret = GetStringIndex(reader, field);
                            }
                        }
                        if (ret != null)
                        {
                            Store(reader, field, SortField.AUTO, ret);
                        }
                    }
                    else
                    {
                        throw new System.SystemException("field \"" + field + "\" does not appear to be indexed");
                    }
                }
                finally
                {
                    enumerator.Close();
                }
            }
            return(ret);
        }
예제 #22
0
        /// <summary> Constructor for enumeration of all terms from specified <code>reader</code> which share a prefix of
        /// length <code>prefixLength</code> with <code>term</code> and which have a fuzzy similarity &gt;
        /// <code>minSimilarity</code>.
        /// <p>
        /// After calling the constructor the enumeration is already pointing to the first 
        /// valid term if such a term exists. 
        /// 
        /// </summary>
        /// <param name="reader">Delivers terms.
        /// </param>
        /// <param name="term">Pattern term.
        /// </param>
        /// <param name="minSimilarity">Minimum required similarity for terms from the reader. Default value is 0.5f.
        /// </param>
        /// <param name="prefixLength">Length of required common prefix. Default value is 0.
        /// </param>
        /// <throws>  IOException </throws>
        public FuzzyTermEnum(IndexReader reader, Term term, float minSimilarity, int prefixLength)
            : base()
        {
            if (minSimilarity >= 1.0f)
                throw new System.ArgumentException("minimumSimilarity cannot be greater than or equal to 1");
            else if (minSimilarity < 0.0f)
                throw new System.ArgumentException("minimumSimilarity cannot be less than 0");
            if (prefixLength < 0)
                throw new System.ArgumentException("prefixLength cannot be less than 0");

            this.minimumSimilarity = minSimilarity;
            this.scale_factor = 1.0f / (1.0f - minimumSimilarity);
            this.searchTerm = term;
            this.field = searchTerm.Field();

            //The prefix could be longer than the word.
            //It's kind of silly though.  It means we must match the entire word.
            int fullSearchTermLength = searchTerm.Text().Length;
            int realPrefixLength = prefixLength > fullSearchTermLength?fullSearchTermLength:prefixLength;

            this.text = searchTerm.Text().Substring(realPrefixLength);
            this.prefix = searchTerm.Text().Substring(0, (realPrefixLength) - (0));

            InitializeMaxDistances();
            this.d = InitDistanceArray();

            SetEnum(reader.Terms(new Term(searchTerm.Field(), prefix)));
        }
예제 #23
0
        /// <summary>Constructs a query selecting all terms greater than
        /// <code>lowerTerm</code> but less than <code>upperTerm</code>.
        /// There must be at least one term and either term may be null,
        /// in which case there is no bound on that side, but if there are
        /// two terms, both terms <b>must</b> be for the same field.
        /// <p/>
        /// If <code>collator</code> is not null, it will be used to decide whether
        /// index terms are within the given range, rather than using the Unicode code
        /// point order in which index terms are stored.
        /// <p/>
        /// <strong>WARNING:</strong> Using this constructor and supplying a non-null
        /// value in the <code>collator</code> parameter will cause every single
        /// index Term in the Field referenced by lowerTerm and/or upperTerm to be
        /// examined.  Depending on the number of index Terms in this Field, the
        /// operation could be very slow.
        ///
        /// </summary>
        /// <param name="lowerTerm">The Term at the lower end of the range
        /// </param>
        /// <param name="upperTerm">The Term at the upper end of the range
        /// </param>
        /// <param name="inclusive">If true, both <code>lowerTerm</code> and
        /// <code>upperTerm</code> will themselves be included in the range.
        /// </param>
        /// <param name="collator">The collator to use to collate index Terms, to determine
        /// their membership in the range bounded by <code>lowerTerm</code> and
        /// <code>upperTerm</code>.
        /// </param>
        public RangeQuery(Term lowerTerm, Term upperTerm, bool inclusive, System.Globalization.CompareInfo collator)
        {
            if (lowerTerm == null && upperTerm == null)
            {
                throw new System.ArgumentException("At least one term must be non-null");
            }
            if (lowerTerm != null && upperTerm != null && (System.Object)lowerTerm.Field() != (System.Object)upperTerm.Field())
            {
                throw new System.ArgumentException("Both terms must have the same field");
            }

            delegate_Renamed = new TermRangeQuery((lowerTerm == null)?upperTerm.Field():lowerTerm.Field(), (lowerTerm == null)?null:lowerTerm.Text(), (upperTerm == null)?null:upperTerm.Text(), inclusive, inclusive, collator);
            delegate_Renamed.SetRewriteMethod(TermRangeQuery.SCORING_BOOLEAN_QUERY_REWRITE);
        }
예제 #24
0
 public WildcardQuery(Term term) : base(term)
 {
     this.termContainsWildcard = (term.Text().IndexOf('*') != -1) || (term.Text().IndexOf('?') != -1);
 }
예제 #25
0
        /// <summary>
        /// Convert Lucene wildcard syntax into an automaton.
        /// @lucene.internal
        /// </summary>
        public static Automaton ToAutomaton(Term wildcardquery)
        {
            IList<Automaton> automata = new List<Automaton>();

            string wildcardText = wildcardquery.Text();

            for (int i = 0; i < wildcardText.Length; )
            {
                int c = Character.CodePointAt(wildcardText, i);
                int length = Character.CharCount(c);
                switch (c)
                {
                    case WILDCARD_STRING:
                        automata.Add(BasicAutomata.MakeAnyString());
                        break;

                    case WILDCARD_CHAR:
                        automata.Add(BasicAutomata.MakeAnyChar());
                        break;

                    case WILDCARD_ESCAPE:
                        // add the next codepoint instead, if it exists
                        if (i + length < wildcardText.Length)
                        {
                            int nextChar = Character.CodePointAt(wildcardText, i + length);
                            length += Character.CharCount(nextChar);
                            automata.Add(BasicAutomata.MakeChar(nextChar));
                            break;
                        } // else fallthru, lenient parsing with a trailing \
                        goto default;
                    default:
                        automata.Add(BasicAutomata.MakeChar(c));
                        break;
                }
                i += length;
            }

            return BasicOperations.Concatenate(automata);
        }
예제 #26
0
 /// <summary> Compares if current upper bound is reached,
 /// this also updates the term count for statistics.
 /// In contrast to {@link FilteredTermEnum}, a return value
 /// of <code>false</code> ends iterating the current enum
 /// and forwards to the next sub-range.
 /// </summary>
 //@Override
 public /*protected internal*/ override bool TermCompare(Term term)
 {
     return((System.Object)term.Field() == (System.Object)Enclosing_Instance.field && String.CompareOrdinal(term.Text(), currentUpperBound) <= 0);
 }
예제 #27
0
 public WildcardQuery(Term term) : base(term)
 {         //will be removed in 3.0
     this.term = term;
     this.termContainsWildcard = (term.Text().IndexOf('*') != -1) || (term.Text().IndexOf('?') != -1);
 }
예제 #28
0
 internal DumbRegexpQuery(Term term, int flags)
     : base(term.Field)
 {
     RegExp re = new RegExp(term.Text(), flags);
     Automaton = re.ToAutomaton();
 }
예제 #29
0
        /// <summary> Returns a BitSet with true for documents which should be
        /// permitted in search results, and false for those that should
        /// not.
        /// </summary>
        public override System.Collections.BitArray Bits(IndexReader reader)
        {
            System.Collections.BitArray bits = new System.Collections.BitArray((reader.MaxDoc() % 64 == 0 ? reader.MaxDoc() / 64 : reader.MaxDoc() / 64 + 1) * 64);
            TermEnum enumerator = (null != lowerTerm ? reader.Terms(new Term(fieldName, lowerTerm)) : reader.Terms(new Term(fieldName, "")));

            try
            {
                if (enumerator.Term() == null)
                {
                    return(bits);
                }

                bool checkLower = false;
                if (!includeLower)
                {
                    // make adjustments to set to exclusive
                    checkLower = true;
                }

                TermDocs termDocs = reader.TermDocs();
                try
                {
                    do
                    {
                        Term term = enumerator.Term();
                        if (term != null && term.Field().Equals(fieldName))
                        {
                            if (!checkLower || null == lowerTerm || String.CompareOrdinal(term.Text(), lowerTerm) > 0)
                            {
                                checkLower = false;
                                if (upperTerm != null)
                                {
                                    int compare = String.CompareOrdinal(upperTerm, term.Text());

                                    /* if beyond the upper term, or is exclusive and
                                     * this is equal to the upper term, break out */
                                    if ((compare < 0) || (!includeUpper && compare == 0))
                                    {
                                        break;
                                    }
                                }
                                /* we have a good term, find the docs */

                                termDocs.Seek(enumerator.Term());
                                while (termDocs.Next())
                                {
                                    bits.Set(termDocs.Doc(), true);
                                }
                            }
                        }
                        else
                        {
                            break;
                        }
                    }while (enumerator.Next());
                }
                finally
                {
                    termDocs.Close();
                }
            }
            finally
            {
                enumerator.Close();
            }

            return(bits);
        }
예제 #30
0
 /// <summary> The termCompare method in FuzzyTermEnum uses Levenshtein distance to 
 /// calculate the distance between the given term and the comparing term. 
 /// </summary>
 /*protected internal*/
 public override bool TermCompare(Term term)
 {
     if ((System.Object) field == (System.Object) term.Field() && term.Text().StartsWith(prefix))
     {
         System.String target = term.Text().Substring(prefix.Length);
         this.similarity = Similarity(target);
         return (similarity > minimumSimilarity);
     }
     endEnum = true;
     return false;
 }
예제 #31
0
파일: RangeQuery.cs 프로젝트: sinsay/SSE
        /// <summary>Constructs a query selecting all terms greater than
        /// <c>lowerTerm</c> but less than <c>upperTerm</c>.
        /// There must be at least one term and either term may be null,
        /// in which case there is no bound on that side, but if there are
        /// two terms, both terms <b>must</b> be for the same field.
        /// <p/>
        /// If <c>collator</c> is not null, it will be used to decide whether
        /// index terms are within the given range, rather than using the Unicode code
        /// point order in which index terms are stored.
        /// <p/>
        /// <strong>WARNING:</strong> Using this constructor and supplying a non-null
        /// value in the <c>collator</c> parameter will cause every single 
        /// index Term in the Field referenced by lowerTerm and/or upperTerm to be
        /// examined.  Depending on the number of index Terms in this Field, the 
        /// operation could be very slow.
        /// 
        /// </summary>
        /// <param name="lowerTerm">The Term at the lower end of the range
        /// </param>
        /// <param name="upperTerm">The Term at the upper end of the range
        /// </param>
        /// <param name="inclusive">If true, both <c>lowerTerm</c> and
        /// <c>upperTerm</c> will themselves be included in the range.
        /// </param>
        /// <param name="collator">The collator to use to collate index Terms, to determine
        /// their membership in the range bounded by <c>lowerTerm</c> and
        /// <c>upperTerm</c>.
        /// </param>
        public RangeQuery(Term lowerTerm, Term upperTerm, bool inclusive, System.Globalization.CompareInfo collator)
        {
            if (lowerTerm == null && upperTerm == null)
                throw new System.ArgumentException("At least one term must be non-null");
            if (lowerTerm != null && upperTerm != null && (System.Object) lowerTerm.Field() != (System.Object) upperTerm.Field())
                throw new System.ArgumentException("Both terms must have the same field");

            delegate_Renamed = new TermRangeQuery((lowerTerm == null)?upperTerm.Field():lowerTerm.Field(), (lowerTerm == null)?null:lowerTerm.Text(), (upperTerm == null)?null:upperTerm.Text(), inclusive, inclusive, collator);
            delegate_Renamed.SetRewriteMethod(TermRangeQuery.SCORING_BOOLEAN_QUERY_REWRITE);
        }
예제 #32
0
        // inherit javadocs
        public virtual StringIndex GetStringIndex(IndexReader reader, System.String field)
        {
            field = String.Intern(field);
            System.Object ret = Lookup(reader, field, Lucene.Net.Search.FieldCache_Fields.STRING_INDEX);
            if (ret == null)
            {
                int[]           retArray = new int[reader.MaxDoc()];
                System.String[] mterms   = new System.String[reader.MaxDoc() + 1];
                if (retArray.Length > 0)
                {
                    TermDocs termDocs = reader.TermDocs();
                    TermEnum termEnum = reader.Terms(new Term(field, ""));
                    int      t        = 0;         // current term number

                    // an entry for documents that have no terms in this field
                    // should a document with no terms be at top or bottom?
                    // this puts them at the top - if it is changed, FieldDocSortedHitQueue
                    // needs to change as well.
                    mterms[t++] = null;

                    try
                    {
                        if (termEnum.Term() == null)
                        {
                            throw new System.SystemException("no terms in field " + field);
                        }
                        do
                        {
                            Term term = termEnum.Term();
                            if (term.Field() != field)
                            {
                                break;
                            }

                            // store term text
                            // we expect that there is at most one term per document
                            if (t >= mterms.Length)
                            {
                                throw new System.SystemException("there are more terms than " + "documents in field \"" + field + "\", but it's impossible to sort on " + "tokenized fields");
                            }
                            mterms[t] = term.Text();

                            termDocs.Seek(termEnum);
                            while (termDocs.Next())
                            {
                                retArray[termDocs.Doc()] = t;
                            }

                            t++;
                        }while (termEnum.Next());
                    }
                    finally
                    {
                        termDocs.Close();
                        termEnum.Close();
                    }

                    if (t == 0)
                    {
                        // if there are no terms, make the term array
                        // have a single null entry
                        mterms = new System.String[1];
                    }
                    else if (t < mterms.Length)
                    {
                        // if there are less terms than documents,
                        // trim off the dead array space
                        System.String[] terms = new System.String[t];
                        Array.Copy(mterms, 0, terms, 0, t);
                        mterms = terms;
                    }
                }
                StringIndex value_Renamed = new StringIndex(retArray, mterms);
                Store(reader, field, Lucene.Net.Search.FieldCache_Fields.STRING_INDEX, value_Renamed);
                return(value_Renamed);
            }
            return((StringIndex)ret);
        }
예제 #33
0
 /// <summary> Compares if current upper bound is reached,
 /// this also updates the term count for statistics.
 /// In contrast to <see cref="FilteredTermEnum" />, a return value
 /// of <c>false</c> ends iterating the current enum
 /// and forwards to the next sub-range.
 /// </summary>
 //@Override
 /*protected internal*/
 public override bool TermCompare(Term term)
 {
     return ((System.Object) term.Field() == (System.Object) Enclosing_Instance.field && String.CompareOrdinal(term.Text(), currentUpperBound) <= 0);
 }
예제 #34
0
        public PrefixTermEnum(IndexReader reader, Term prefix)
        {
            this.prefix = prefix;

            SetEnum(reader.Terms(new Term(prefix.Field(), prefix.Text())));
        }
예제 #35
0
 public /*protected internal*/ override bool TermCompare(Term term)
 {
     if ((System.Object)term.Field() == (System.Object)prefix.Field() && term.Text().StartsWith(prefix.Text()))
     {
         return(true);
     }
     endEnum = true;
     return(false);
 }
예제 #36
0
		public WildcardQuery(Term term) : base(term)
		{
			this.termContainsWildcard = (term.Text().IndexOf((System.Char) '*') != - 1) || (term.Text().IndexOf((System.Char) '?') != - 1);
		}
예제 #37
0
        public override Query Rewrite(IndexReader reader)
        {
            BooleanQuery query     = new BooleanQuery(true);
            string       testField = GetField();

            if (collator != null)
            {
                TermEnum enumerator    = reader.Terms(new Term(testField, ""));
                string   lowerTermText = lowerTerm != null?lowerTerm.Text() : null;

                string upperTermText = upperTerm != null?upperTerm.Text() : null;

                try
                {
                    do
                    {
                        Term term = enumerator.Term();
                        if (term != null && term.Field() == testField) // interned comparison
                        {
                            if ((lowerTermText == null ||
                                 (inclusive ? collator.Compare(term.Text(), lowerTermText) >= 0 : collator.Compare(term.Text(), lowerTermText) > 0))
                                &&
                                (upperTermText == null ||
                                 (inclusive ? collator.Compare(term.Text(), upperTermText) <= 0 : collator.Compare(term.Text(), upperTermText) < 0))
                                )
                            {
                                AddTermToQuery(term, query);
                            }
                        }
                    }while (enumerator.Next());
                }
                finally
                {
                    enumerator.Close();
                }
            }
            else
            {
                TermEnum enumerator = reader.Terms(lowerTerm);

                try
                {
                    bool checkLower = false;
                    if (!inclusive)
                    {
                        // make adjustments to set to exclusive
                        checkLower = true;
                    }

                    do
                    {
                        Term term = enumerator.Term();
                        if (term != null && term.Field() == testField)
                        {
                            // interned comparison
                            if (!checkLower || String.CompareOrdinal(term.Text(), lowerTerm.Text()) > 0)
                            {
                                checkLower = false;
                                if (upperTerm != null)
                                {
                                    int compare = String.CompareOrdinal(upperTerm.Text(), term.Text());

                                    /* if beyond the upper term, or is exclusive and
                                     * this is equal to the upper term, break out */
                                    if ((compare < 0) || (!inclusive && compare == 0))
                                    {
                                        break;
                                    }
                                }
                                AddTermToQuery(term, query); // Found a match
                            }
                        }
                        else
                        {
                            break;
                        }
                    }while (enumerator.Next());
                }
                finally
                {
                    enumerator.Close();
                }
            }
            return(query);
        }