Beispiel #1
0
		public override void  SetUp()
		{
			base.SetUp();
			RAMDirectory directory = new RAMDirectory();
			IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
			long theLong = System.Int64.MaxValue;
			double theDouble = System.Double.MaxValue;
			sbyte theByte = (sbyte) System.SByte.MaxValue;
			short theShort = System.Int16.MaxValue;
			int theInt = System.Int32.MaxValue;
			float theFloat = System.Single.MaxValue;
			for (int i = 0; i < NUM_DOCS; i++)
			{
				Document doc = new Document();
				doc.Add(new Field("theLong", System.Convert.ToString(theLong--), Field.Store.NO, Field.Index.NOT_ANALYZED));
				doc.Add(new Field("theDouble", (theDouble--).ToString("E16"), Field.Store.NO, Field.Index.NOT_ANALYZED));
				doc.Add(new Field("theByte", System.Convert.ToString((sbyte) theByte--), Field.Store.NO, Field.Index.NOT_ANALYZED));
				doc.Add(new Field("theShort", System.Convert.ToString(theShort--), Field.Store.NO, Field.Index.NOT_ANALYZED));
				doc.Add(new Field("theInt", System.Convert.ToString(theInt--), Field.Store.NO, Field.Index.NOT_ANALYZED));
				doc.Add(new Field("theFloat", (theFloat--).ToString("E8"), Field.Store.NO, Field.Index.NOT_ANALYZED));
				writer.AddDocument(doc);
			}
			writer.Close();
			reader = IndexReader.Open(directory);
		}
		public override DocIdSet GetDocIdSet(IndexReader reader)
		{
			if (cache == null)
			{
				cache = new System.Collections.Hashtable();
			}
			
			lock (cache.SyncRoot)
			{
				// check cache
				DocIdSet cached = (DocIdSet) cache[reader];
				if (shouldHaveCache)
				{
					Assert.IsNotNull(cached, "Cache should have data ");
				}
				else
				{
					Assert.IsNotNull( cached, "Cache should be null " + cached);
				}
				if (cached != null)
				{
					return cached;
				}
			}
			
			DocIdSet bits = filter.GetDocIdSet(reader);
			
			lock (cache.SyncRoot)
			{
				// update cache
				cache[reader] = bits;
			}
			
			return bits;
		}
		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);
			
			new IndexSearcher(reader).Search(query, new AnonymousClassHitCollector(bits, this));
			return bits;
		}
Beispiel #4
0
        public override System.Collections.BitArray Bits(IndexReader reader)
        {
            if (cache == null)
            {
                cache = new System.Collections.Hashtable();
            }

            lock (cache.SyncRoot)
            {
                // check cache
                System.Collections.BitArray cached = (System.Collections.BitArray) cache[reader];
                if (cached != null)
                {
                    return cached;
                }
            }

            System.Collections.BitArray bits = new System.Collections.BitArray((reader.MaxDoc() % 64 == 0?reader.MaxDoc() / 64:reader.MaxDoc() / 64 + 1) * 64);

            new IndexSearcher(reader).Search(query, new AnonymousClassHitCollector(bits, this));

            lock (cache.SyncRoot)
            {
                // update cache
                cache[reader] = bits;
            }

            return bits;
        }
 public static void BeforeClass()
 {
     Directory = NewDirectory();
     RandomIndexWriter writer = new RandomIndexWriter(Random(), Directory);
     writer.Dispose();
     Reader = DirectoryReader.Open(Directory);
 }
 public void BeforeClass()
 {
     Directory = NewDirectory();
     RandomIndexWriter writer = new RandomIndexWriter(Random(), Directory, Similarity, TimeZone);
     writer.Dispose();
     Reader = DirectoryReader.Open(Directory);
 }
		public override System.Collections.BitArray Bits(IndexReader reader)
		{
			if (cache == null)
			{
                cache = new SupportClass.WeakHashTable();
			}
			
			System.Object cached = null;
			lock (cache.SyncRoot)
			{
				// check cache
				cached = cache[reader];
			}
			
			if (cached != null)
			{
				if (cached is System.Collections.BitArray)
				{
					return (System.Collections.BitArray) cached;
				}
				else if (cached is DocIdBitSet)
					return ((DocIdBitSet) cached).GetBitSet();
				// It would be nice to handle the DocIdSet case, but that's not really possible
			}
			
			System.Collections.BitArray bits = filter.Bits(reader);
			
			lock (cache.SyncRoot)
			{
				// update cache
				cache[reader] = bits;
			}
			
			return bits;
		}
		public override void  SetUp()
		{
			base.SetUp();
			PayloadHelper helper = new PayloadHelper();
			searcher = helper.SetUp(similarity, 1000);
			indexReader = searcher.GetIndexReader();
		}
Beispiel #9
0
			public override System.Collections.BitArray Bits(IndexReader reader)
			{
				System.Collections.BitArray bitset = new System.Collections.BitArray((5 % 64 == 0?5 / 64:5 / 64 + 1) * 64);
				bitset.Set(1, true);
				bitset.Set(3, true);
				return bitset;
			}
		public override Query Rewrite(IndexReader reader)
		{
			Query orig = new RegexQuery(term).Rewrite(reader);
			
			// RegexQuery (via MultiTermQuery).rewrite always returns a BooleanQuery
			BooleanQuery bq = (BooleanQuery) orig;
			
			BooleanClause[] clauses = bq.GetClauses();
			SpanQuery[] sqs = new SpanQuery[clauses.Length];
			for (int i = 0; i < clauses.Length; i++)
			{
				BooleanClause clause = clauses[i];
				
				// Clauses from RegexQuery.rewrite are always TermQuery's
				TermQuery tq = (TermQuery) clause.GetQuery();
				
				sqs[i] = new SpanTermQuery(tq.GetTerm());
				sqs[i].SetBoost(tq.GetBoost());
			}
			
			SpanOrQuery query = new SpanOrQuery(sqs);
			query.SetBoost(orig.GetBoost());
			
			return query;
		}
Beispiel #11
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)));
        }
        public static void BeforeClass()
        {
            Directory = NewDirectory();
            RandomIndexWriter writer = new RandomIndexWriter(Random(), Directory);

            Document doc = new Document();
            Field field = NewStringField(FIELD, "meaninglessnames", Field.Store.NO);
            doc.Add(field);

            for (int i = 0; i < 5137; ++i)
            {
                writer.AddDocument(doc);
            }

            field.StringValue = "tangfulin";
            writer.AddDocument(doc);

            field.StringValue = "meaninglessnames";
            for (int i = 5138; i < 11377; ++i)
            {
                writer.AddDocument(doc);
            }

            field.StringValue = "tangfulin";
            writer.AddDocument(doc);

            Reader = writer.Reader;
            Searcher = NewSearcher(Reader);
            writer.Dispose();
        }
			protected internal override System.Object CreateValue(IndexReader reader, System.Object entryKey)
			{
				Entry entry = (Entry) entryKey;
				System.String field = entry.field;
				LongParser parser = (LongParser) entry.custom;
				long[] retArray = new long[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;
						long termval = parser.ParseLong(term.Text());
						termDocs.Seek(termEnum);
						while (termDocs.Next())
						{
							retArray[termDocs.Doc()] = termval;
						}
					}
					while (termEnum.Next());
				}
				finally
				{
					termDocs.Close();
					termEnum.Close();
				}
				return retArray;
			}
		/// <summary> Enumerates all terms greater/equal than <c>lowerTerm</c>
		/// but less/equal than <c>upperTerm</c>. 
		/// 
		/// If an endpoint is null, it is said to be "open". Either or both 
		/// endpoints may be open.  Open endpoints may not be exclusive 
		/// (you can't select all but the first or last term without 
		/// explicitly specifying the term to exclude.)
		/// 
		/// </summary>
		/// <param name="reader">
		/// </param>
		/// <param name="field">An interned field that holds both lower and upper terms.
		/// </param>
		/// <param name="lowerTermText">The term text at the lower end of the range
		/// </param>
		/// <param name="upperTermText">The term text at the upper end of the range
		/// </param>
		/// <param name="includeLower">If true, the <c>lowerTerm</c> is included in the range.
		/// </param>
		/// <param name="includeUpper">If true, the <c>upperTerm</c> is 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>
		/// <throws>  IOException </throws>
		public TermRangeTermEnum(IndexReader reader, System.String field, System.String lowerTermText, System.String upperTermText, bool includeLower, bool includeUpper, System.Globalization.CompareInfo collator)
		{
			this.collator = collator;
			this.upperTermText = upperTermText;
			this.lowerTermText = lowerTermText;
			this.includeLower = includeLower;
			this.includeUpper = includeUpper;
			this.field = StringHelper.Intern(field);
			
			// do a little bit of normalization...
			// open ended range queries should always be inclusive.
			if (this.lowerTermText == null)
			{
				this.lowerTermText = "";
				this.includeLower = true;
			}
			
			if (this.upperTermText == null)
			{
				this.includeUpper = true;
			}
			
			System.String startTermText = collator == null?this.lowerTermText:"";
			SetEnum(reader.Terms(new Term(this.field, startTermText)));
		}
Beispiel #15
0
 /// <summary> Creates a hit queue sorted by the given list of fields.</summary>
 /// <param name="reader"> Index to use.
 /// </param>
 /// <param name="fields">Fieldable names, in priority order (highest priority first).  Cannot be <c>null</c> or empty.
 /// </param>
 /// <param name="size"> The number of hits to retain.  Must be greater than zero.
 /// </param>
 /// <throws>  IOException </throws>
 public FieldSortedHitQueue(IndexReader reader, SortField[] fields, int size)
 {
     int n = fields.Length;
     comparators = new ScoreDocComparator[n];
     this.fields = new SortField[n];
     for (int i = 0; i < n; ++i)
     {
         System.String fieldname = fields[i].GetField();
         comparators[i] = GetCachedComparator(reader, fieldname, fields[i].GetType(), fields[i].GetParser(), fields[i].GetLocale(), fields[i].GetFactory());
         // new SortField instances must only be created when auto-detection is in use
         if (fields[i].GetType() == SortField.AUTO)
         {
             if (comparators[i].SortType() == SortField.STRING)
             {
                 this.fields[i] = new SortField(fieldname, fields[i].GetLocale(), fields[i].GetReverse());
             }
             else
             {
                 this.fields[i] = new SortField(fieldname, comparators[i].SortType(), fields[i].GetReverse());
             }
         }
         else
         {
             System.Diagnostics.Debug.Assert(comparators [i].SortType() == fields [i].GetType());
             this.fields[i] = fields[i];
         }
     }
     Initialize(size);
 }
			internal MatchAllScorer(MatchAllDocsQuery enclosingInstance, IndexReader reader, Similarity similarity) : base(similarity)
			{
				InitBlock(enclosingInstance);
				this.reader = reader;
				count = - 1;
				maxDoc = reader.MaxDoc();
			}
Beispiel #17
0
 public void Purge(IndexReader r)
 {
     foreach (Cache c in caches.Values)
     {
         c.Purge(r);
     }
 }
			internal MatchAllScorer(MatchAllDocsQuery enclosingInstance, IndexReader reader, Similarity similarity, Weight w, byte[] norms):base(similarity)
			{
				InitBlock(enclosingInstance);
				this.termDocs = reader.TermDocs(null);
				score = w.GetValue();
				this.norms = norms;
			}
		// inherit javadocs
		public virtual ScoreDocComparator NewComparator(IndexReader reader, System.String fieldname)
		{
			System.String field = String.Intern(fieldname);
			System.IComparable[] cachedValues = Lucene.Net.Search.FieldCache_Fields.DEFAULT.GetCustom(reader, field, this);
			
			return new AnonymousClassScoreDocComparator(cachedValues, this);
		}
Beispiel #20
0
			public override DocIdSet GetDocIdSet(IndexReader reader)
			{
				System.Collections.BitArray bitset = new System.Collections.BitArray((5 % 64 == 0?5 / 64:5 / 64 + 1) * 64);
				bitset.Set(1, true);
				bitset.Set(3, true);
				return new DocIdBitSet(bitset);
			}
		/// <summary> Returns sub-reader subIndex from reader.
		/// 
		/// </summary>
		/// <param name="reader">parent reader
		/// </param>
		/// <param name="subIndex">index of desired sub reader
		/// </param>
		/// <returns> the subreader at subINdex
		/// </returns>
		public static IndexReader SubReader(IndexReader reader, int subIndex)
		{
            var subReadersList = new System.Collections.Generic.List<IndexReader>();
			ReaderUtil.GatherSubReaders(subReadersList, reader);
			IndexReader[] subReaders = subReadersList.ToArray();
			return subReaders[subIndex];
		}
 public static void AfterClass()
 {
     Reader.Dispose();
     Small.Dispose();
     Reader = null;
     Small = null;
 }
		public override Query Rewrite(IndexReader reader)
		{
			if (!termContainsWildcard)
				return new TermQuery(GetTerm());
			else
				return base.Rewrite(reader);
		}
Beispiel #24
0
 public override Query Rewrite(IndexReader reader)
 {
     BooleanQuery query = new BooleanQuery(true);
     TermEnum enumerator = reader.Terms(prefix);
     try
     {
         System.String prefixText = prefix.Text();
         System.String prefixField = prefix.Field();
         do
         {
             Term term = enumerator.Term();
             if (term != null && term.Text().StartsWith(prefixText) && term.Field() == prefixField)
             {
                 TermQuery tq = new TermQuery(term); // found a match
                 tq.SetBoost(GetBoost()); // set the boost
                 query.Add(tq, BooleanClause.Occur.SHOULD); // add to query
                 //System.out.println("added " + term);
             }
             else
             {
                 break;
             }
         }
         while (enumerator.Next());
     }
     finally
     {
         enumerator.Close();
     }
     return query;
 }
 public static void AfterClass()
 {
     Reader.Dispose();
     Directory.Dispose();
     Reader = null;
     Directory = null;
 }
		public override System.Collections.BitArray Bits(IndexReader reader)
		{
			if (cache == null)
			{
				cache = new System.Collections.Hashtable();
			}
			
			lock (cache.SyncRoot)
			{
				// check cache
				System.Collections.BitArray cached = (System.Collections.BitArray) cache[reader];
				if (cached != null)
				{
					return cached;
				}
			}
			
			System.Collections.BitArray bits = filter.Bits(reader);
			
			lock (cache.SyncRoot)
			{
				// update cache
				cache[reader] = bits;
			}
			
			return bits;
		}
				private void  InitBlock(IndexReader reader, TermEnum enumerator, System.String field, AnonymousClassSortComparatorSource enclosingInstance)
				{
					this.reader = reader;
					this.enumerator = enumerator;
					this.field = field;
					this.enclosingInstance = enclosingInstance;
					cachedValues = FillCache(reader, enumerator, field);
				}
 public static void AfterClass()
 {
     s = null;
     r.Dispose();
     r = null;
     Index.Dispose();
     Index = null;
 }
			internal MatchAllScorer(MatchAllDocsQuery enclosingInstance, IndexReader reader, Similarity similarity, Weight w):base(similarity)
			{
				InitBlock(enclosingInstance);
				this.reader = reader;
				id = - 1;
				maxId = reader.MaxDoc() - 1;
				score = w.GetValue();
			}
 public static void BeforeClassBaseTestRangeFilter()
 {
     MaxId = AtLeast(500);
     SignedIndexDir = new TestIndex(Random(), int.MaxValue, int.MinValue, true);
     UnsignedIndexDir = new TestIndex(Random(), int.MaxValue, 0, false);
     SignedIndexReader = Build(Random(), SignedIndexDir);
     UnsignedIndexReader = Build(Random(), UnsignedIndexDir);
 }
Beispiel #31
0
 public IndexSearcher(Directory directory) : this(IndexReader.Open(directory), true)
 {
 }
            protected internal override System.Object CreateValue(IndexReader reader, Entry entryKey)
            {
                System.String   field    = StringHelper.Intern(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 || term.Field != field || t >= mterms.Length)
                        {
                            break;
                        }

                        // store term text
                        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);
            }
 /*(non-Javadoc) <see cref="Lucene.Net.Search.Function.ValueSource.getValues(Lucene.Net.Index.IndexReader) */
 public override DocValues GetValues(IndexReader reader)
 {
     int[] arr = Lucene.Net.Search.FieldCache_Fields.DEFAULT.GetStringIndex(reader, field).order;
     return(new AnonymousClassDocValues(arr, this));
 }
Beispiel #34
0
 protected internal override FilteredTermEnum GetEnum(IndexReader reader)
 {
     return(new PrefixTermEnum(reader, prefix));
 }
Beispiel #35
0
        /// <summary> Suggest similar words (restricted or not to a field of a user index)</summary>
        /// <param name="word">String the word you want a spell check done on
        /// </param>
        /// <param name="numSug">int the number of suggest words
        /// </param>
        /// <param name="ir">the indexReader of the user index (can be null see field param)
        /// </param>
        /// <param name="field">String the field of the user index: if field is not null, the suggested
        /// words are restricted to the words present in this field.
        /// </param>
        /// <param name="morePopular">boolean return only the suggest words that are more frequent than the searched word
        /// (only if restricted mode = (indexReader!=null and field!=null)
        /// </param>
        /// <throws>  IOException </throws>
        /// <returns> String[] the sorted list of the suggest words with this 2 criteria:
        /// first criteria: the edit distance, second criteria (only if restricted mode): the popularity
        /// of the suggest words in the field of the user index
        /// </returns>
        public virtual System.String[] SuggestSimilar(System.String word, int numSug, IndexReader ir, System.String field, bool morePopular)
        {    // obtainSearcher calls ensureOpen
            IndexSearcher indexSearcher = ObtainSearcher();

            try
            {
                float min        = this.minScore;
                int   lengthWord = word.Length;

                int freq     = (ir != null && field != null) ? ir.DocFreq(new Term(field, word)) : 0;
                int goalFreq = (morePopular && ir != null && field != null) ? freq : 0;
                // if the word exists in the real index and we don't care for word frequency, return the word itself
                if (!morePopular && freq > 0)
                {
                    return(new String[] { word });
                }

                BooleanQuery query = new BooleanQuery();
                String[]     grams;
                String       key;

                for (int ng = GetMin(lengthWord); ng <= GetMax(lengthWord); ng++)
                {
                    key = "gram" + ng;           // form key

                    grams = FormGrams(word, ng); // form word into ngrams (allow dups too)

                    if (grams.Length == 0)
                    {
                        continue; // hmm
                    }

                    if (bStart > 0)
                    {                                               // should we boost prefixes?
                        Add(query, "start" + ng, grams[0], bStart); // matches start of word
                    }
                    if (bEnd > 0)
                    {                                                          // should we boost suffixes
                        Add(query, "end" + ng, grams[grams.Length - 1], bEnd); // matches end of word
                    }
                    for (int i = 0; i < grams.Length; i++)
                    {
                        Add(query, key, grams[i]);
                    }
                }

                int maxHits = 10 * numSug;

                //    System.out.println("Q: " + query);
                ScoreDoc[] hits = indexSearcher.Search(query, null, maxHits).scoreDocs;
                //    System.out.println("HITS: " + hits.length());
                SuggestWordQueue sugQueue = new SuggestWordQueue(numSug);

                // go thru more than 'maxr' matches in case the distance filter triggers
                int         stop    = Math.Min(hits.Length, maxHits);
                SuggestWord sugWord = new SuggestWord();
                for (int i = 0; i < stop; i++)
                {
                    sugWord.string_Renamed = indexSearcher.Doc(hits[i].doc).Get(F_WORD); // get orig word

                    // don't suggest a word for itself, that would be silly
                    if (sugWord.string_Renamed.Equals(word))
                    {
                        continue;
                    }

                    // edit distance
                    sugWord.score = sd.GetDistance(word, sugWord.string_Renamed);
                    if (sugWord.score < min)
                    {
                        continue;
                    }

                    if (ir != null && field != null)
                    {                                                                       // use the user index
                        sugWord.freq = ir.DocFreq(new Term(field, sugWord.string_Renamed)); // freq in the index
                        // don't suggest a word that is not present in the field
                        if ((morePopular && goalFreq > sugWord.freq) || sugWord.freq < 1)
                        {
                            continue;
                        }
                    }
                    sugQueue.InsertWithOverflow(sugWord);
                    if (sugQueue.Size() == numSug)
                    {
                        // if queue full, maintain the minScore score
                        min = ((SuggestWord)sugQueue.Top()).score;
                    }
                    sugWord = new SuggestWord();
                }

                // convert to array string
                String[] list = new String[sugQueue.Size()];
                for (int i = sugQueue.Size() - 1; i >= 0; i--)
                {
                    list[i] = ((SuggestWord)sugQueue.Pop()).string_Renamed;
                }

                return(list);
            }
            finally
            {
                ReleaseSearcher(indexSearcher);
            }
        }
Beispiel #36
0
            public override Explanation Explain(IndexReader reader, int doc, IState state)
            {
                ComplexExplanation result = new ComplexExplanation();

                result.Description = "weight(" + Query + " in " + doc + "), product of:";

                Explanation expl = new Explanation(idf, idfExp.Explain());

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

                queryExpl.Description = "queryWeight(" + Query + "), product of:";

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

                if (Enclosing_Instance.Boost != 1.0f)
                {
                    queryExpl.AddDetail(boostExpl);
                }
                queryExpl.AddDetail(expl);

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

                queryExpl.AddDetail(queryNormExpl);

                queryExpl.Value = boostExpl.Value * expl.Value * queryNormExpl.Value;

                result.AddDetail(queryExpl);

                // explain field weight
                System.String      field     = Enclosing_Instance.term.Field;
                ComplexExplanation fieldExpl = new ComplexExplanation();

                fieldExpl.Description = "fieldWeight(" + Enclosing_Instance.term + " in " + doc + "), product of:";

                Explanation tfExplanation = new Explanation();
                int         tf            = 0;
                TermDocs    termDocs      = reader.TermDocs(enclosingInstance.term, state);

                if (termDocs != null)
                {
                    try
                    {
                        if (termDocs.SkipTo(doc, state) && termDocs.Doc == doc)
                        {
                            tf = termDocs.Freq;
                        }
                    }
                    finally
                    {
                        termDocs.Close();
                    }
                    tfExplanation.Value       = similarity.Tf(tf);
                    tfExplanation.Description = "tf(termFreq(" + enclosingInstance.term + ")=" + tf + ")";
                }
                else
                {
                    tfExplanation.Value       = 0.0f;
                    tfExplanation.Description = "no matching term";
                }
                fieldExpl.AddDetail(tfExplanation);
                fieldExpl.AddDetail(expl);

                Explanation fieldNormExpl = new Explanation();

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

                fieldNormExpl.Value       = fieldNorm;
                fieldNormExpl.Description = "fieldNorm(field=" + field + ", doc=" + doc + ")";
                fieldExpl.AddDetail(fieldNormExpl);

                fieldExpl.Match = tfExplanation.IsMatch;
                fieldExpl.Value = tfExplanation.Value * expl.Value * fieldNormExpl.Value;

                result.AddDetail(fieldExpl);
                System.Boolean?tempAux = fieldExpl.Match;
                result.Match = tempAux;

                // combine them
                result.Value = queryExpl.Value * fieldExpl.Value;

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

                return(result);
            }
Beispiel #37
0
 /// <summary>Creates a searcher searching the index in the named
 /// directory.  You should pass readOnly=true, since it
 /// gives much better concurrent performance, unless you
 /// intend to do write operations (delete documents or
 /// change norms) with the underlying IndexReader.
 /// </summary>
 /// <throws>  CorruptIndexException if the index is corrupt </throws>
 /// <throws>  IOException if there is a low-level IO error </throws>
 /// <param name="path">directory where IndexReader will be opened
 /// </param>
 /// <param name="readOnly">if true, the underlying IndexReader
 /// will be opened readOnly
 /// </param>
 public IndexSearcher(Directory path, bool readOnly) : this(IndexReader.Open(path, readOnly), true)
 {
 }
Beispiel #38
0
 /// <summary>Creates a searcher searching the provided index. </summary>
 public IndexSearcher(IndexReader r) : this(r, false)
 {
 }
 // inherit javadocs
 public virtual StringIndex GetStringIndex(IndexReader reader, System.String field)
 {
     return((StringIndex)caches[typeof(StringIndex)].Get(reader, new Entry(field, (Parser)null)));
 }
Beispiel #40
0
 protected internal virtual void  GatherSubReaders(System.Collections.IList allSubReaders, IndexReader r)
 {
     ReaderUtil.GatherSubReaders(allSubReaders, r);
 }
Beispiel #41
0
 public IndexSearcher(System.String path) : this(IndexReader.Open(path), true)
 {
 }
Beispiel #42
0
 /// <summary> Return cached DocValues for input field and reader.</summary>
 /// <param name="cache">FieldCache so that values of a field are loaded once per reader (RAM allowing)
 /// </param>
 /// <param name="field">Field for which values are required.
 /// </param>
 /// <seealso cref="ValueSource">
 /// </seealso>
 public abstract DocValues GetCachedFieldValues(FieldCache cache, System.String field, IndexReader reader);
Beispiel #43
0
 /* (non-Javadoc) @see Lucene.Net.Search.Function.ValueSource#getValues(Lucene.Net.Index.IndexReader) */
 public override DocValues GetValues(IndexReader reader)
 {
     return(GetCachedFieldValues(Lucene.Net.Search.FieldCache_Fields.DEFAULT, field, reader));
 }
 public override void  SetNextReader(IndexReader reader, int docBase)
 {
     c.SetNextReader(reader, docBase);
 }
Beispiel #45
0
 private IndexSearcher(IndexReader r, bool closeReader)
 {
     reader           = r;
     this.closeReader = closeReader;
 }
Beispiel #46
0
 private void  InitBlock(Lucene.Net.Search.Weight weight, Lucene.Net.Index.IndexReader reader, QueryWrapperFilter enclosingInstance)
 {
     this.weight            = weight;
     this.reader            = reader;
     this.enclosingInstance = enclosingInstance;
 }
Beispiel #47
0
            public override Explanation Explain(IndexReader reader, int doc)
            {
                ComplexExplanation result = new ComplexExplanation();

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

                Explanation expl = new Explanation(idf, idfExp.Explain());

                // 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(expl);

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

                queryExpl.AddDetail(queryNormExpl);

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

                result.AddDetail(queryExpl);

                // explain field weight
                System.String      field     = Enclosing_Instance.term.Field();
                ComplexExplanation fieldExpl = new ComplexExplanation();

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

                Explanation tfExpl = Scorer(reader, true, false).Explain(doc);

                fieldExpl.AddDetail(tfExpl);
                fieldExpl.AddDetail(expl);

                Explanation fieldNormExpl = new Explanation();

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

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

                fieldExpl.SetMatch(tfExpl.IsMatch());
                fieldExpl.SetValue(tfExpl.GetValue() * expl.GetValue() * fieldNormExpl.GetValue());

                result.AddDetail(fieldExpl);
                System.Boolean?tempAux = fieldExpl.GetMatch();
                result.SetMatch(tempAux);

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

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

                return(result);
            }
Beispiel #48
0
 public AnonymousClassDocIdSet(Lucene.Net.Search.Weight weight, Lucene.Net.Index.IndexReader reader, QueryWrapperFilter enclosingInstance)
 {
     InitBlock(weight, reader, enclosingInstance);
 }
 public override void  SetNextReader(IndexReader reader, int docBase)
 {
     collector.SetNextReader(reader, start + docBase);
 }
Beispiel #50
0
 protected override DocIdSet MergeDeletes(IndexReader reader, DocIdSet docIdSet)
 {
     return(new AnonymousFilteredDocIdSet(docIdSet, reader));
 }
Beispiel #51
0
 /*(non-Javadoc) @see Lucene.Net.Search.Function.FieldCacheSource#getCachedValues(Lucene.Net.Search.FieldCache, java.lang.String, Lucene.Net.Index.IndexReader) */
 public override DocValues GetCachedFieldValues(FieldCache cache, System.String field, IndexReader reader)
 {
     byte[] arr = (parser == null) ? cache.GetBytes(reader, field) : cache.GetBytes(reader, field, parser);
     return(new AnonymousClassDocValues(arr, this));
 }
Beispiel #52
0
 public AnonymousFilteredDocIdSet(DocIdSet innerSet, IndexReader r) : base(innerSet)
 {
     this.r = r;
 }
Beispiel #53
0
        public virtual void  TestNullOrSubScorer()
        {
            Directory   dir = new MockRAMDirectory();
            IndexWriter w   = new IndexWriter(dir, new WhitespaceAnalyzer(), IndexWriter.MaxFieldLength.UNLIMITED, null);
            Document    doc = new Document();

            doc.Add(new Field("field", "a b c d", Field.Store.NO, Field.Index.ANALYZED));
            w.AddDocument(doc, null);

            IndexReader   r = w.GetReader(null);
            IndexSearcher s = new IndexSearcher(r);
            BooleanQuery  q = new BooleanQuery();

            q.Add(new TermQuery(new Term("field", "a")), Occur.SHOULD);

            // LUCENE-2617: make sure that a term not in the index still contributes to the score via coord factor
            float score    = s.Search(q, 10, null).MaxScore;
            Query subQuery = new TermQuery(new Term("field", "not_in_index"));

            subQuery.Boost = 0;
            q.Add(subQuery, Occur.SHOULD);
            float score2 = s.Search(q, 10, null).MaxScore;

            Assert.AreEqual(score * .5, score2, 1e-6);

            // LUCENE-2617: make sure that a clause not in the index still contributes to the score via coord factor
            BooleanQuery qq     = (BooleanQuery)q.Clone();
            PhraseQuery  phrase = new PhraseQuery();

            phrase.Add(new Term("field", "not_in_index"));
            phrase.Add(new Term("field", "another_not_in_index"));
            phrase.Boost = 0;
            qq.Add(phrase, Occur.SHOULD);
            score2 = s.Search(qq, 10, null).MaxScore;
            Assert.AreEqual(score * (1.0 / 3), score2, 1e-6);

            // now test BooleanScorer2
            subQuery       = new TermQuery(new Term("field", "b"));
            subQuery.Boost = 0;
            q.Add(subQuery, Occur.MUST);
            score2 = s.Search(q, 10, null).MaxScore;
            Assert.AreEqual(score * (2.0 / 3), score2, 1e-6);

            // PhraseQuery w/ no terms added returns a null scorer
            PhraseQuery pq = new PhraseQuery();

            q.Add(pq, Occur.SHOULD);
            Assert.AreEqual(1, s.Search(q, 10, null).TotalHits);

            // A required clause which returns null scorer should return null scorer to
            // IndexSearcher.
            q  = new BooleanQuery();
            pq = new PhraseQuery();
            q.Add(new TermQuery(new Term("field", "a")), Occur.SHOULD);
            q.Add(pq, Occur.MUST);
            Assert.AreEqual(0, s.Search(q, 10, null).TotalHits);

            DisjunctionMaxQuery dmq = new DisjunctionMaxQuery(1.0f);

            dmq.Add(new TermQuery(new Term("field", "a")));
            dmq.Add(pq);
            Assert.AreEqual(1, s.Search(dmq, 10, null).TotalHits);

            r.Close();
            w.Close();
            dir.Close();
        }
Beispiel #54
0
 protected abstract T MergeDeletes(IndexReader reader, T value);
Beispiel #55
0
 public override DocIdSet GetDocIdSet(IndexReader reader)
 {
     wasCalled = true;
     return(new DocIdBitSet(new System.Collections.BitArray(64)));
 }
Beispiel #56
0
 public IndexSearcher(System.String path, bool readOnly) : this(IndexReader.Open(path, readOnly), true)
 {
 }
 // inherit javadocs
 public virtual double[] GetDoubles(IndexReader reader, System.String field, Lucene.Net.Search.DoubleParser parser)
 {
     return((double[])caches[typeof(double)].Get(reader, new Entry(field, parser)));
 }
            public override Explanation Explain(IndexReader reader, int doc)
            {
                Explanation result = new Explanation();

                result.Description = "weight(" + Query + " 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 = 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.Description = "queryWeight(" + Query + "), product of:";

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

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

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

                queryExpl.AddDetail(queryNormExpl);

                queryExpl.Value = boostExpl.Value * idfExpl.Value * queryNormExpl.Value;

                result.AddDetail(queryExpl);

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

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

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

                if (scorer == null)
                {
                    return(new Explanation(0.0f, "no matching docs"));
                }
                Explanation tfExplanation = new Explanation();
                int         d             = scorer.Advance(doc);
                float       phraseFreq    = (d == doc) ? scorer.CurrentFreq() : 0.0f;

                tfExplanation.Value       = similarity.Tf(phraseFreq);
                tfExplanation.Description = "tf(phraseFreq=" + phraseFreq + ")";

                fieldExpl.AddDetail(tfExplanation);
                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.Value       = fieldNorm;
                fieldNormExpl.Description = "fieldNorm(field=" + Enclosing_Instance.field + ", doc=" + doc + ")";
                fieldExpl.AddDetail(fieldNormExpl);

                fieldExpl.Value = tfExplanation.Value * idfExpl.Value * fieldNormExpl.Value;

                result.AddDetail(fieldExpl);

                // combine them
                result.Value = queryExpl.Value * fieldExpl.Value;

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

                return(result);
            }
 /*(non-Javadoc) @see Lucene.Net.Search.Function.FieldCacheSource#getCachedValues(Lucene.Net.Search.FieldCache, java.lang.String, Lucene.Net.Index.IndexReader) */
 public override DocValues GetCachedFieldValues(FieldCache cache, System.String field, IndexReader reader)
 {
     short[] arr = cache.GetShorts(reader, field, parser);
     return(new AnonymousClassDocValues(arr, this));
 }
 // inherit javadocs
 public virtual System.String[] GetStrings(IndexReader reader, System.String field)
 {
     return((System.String[])caches[typeof(string)].Get(reader, new Entry(field, (Parser)null)));
 }