Example #1
0
        void LUCENENET_100_ClientSearch()
        {
            try
            {
                Lucene.Net.Search.Searchable    s        = (Lucene.Net.Search.Searchable)Activator.GetObject(typeof(Lucene.Net.Search.Searchable), @"tcp://localhost:" + ANYPORT + "/Searcher");
                Lucene.Net.Search.MultiSearcher searcher = new Lucene.Net.Search.MultiSearcher(new Lucene.Net.Search.Searchable[] { s });

                Lucene.Net.Search.Query q = new Lucene.Net.Search.TermQuery(new Lucene.Net.Index.Term("field1", "moon"));

                Lucene.Net.Search.Sort sort = new Lucene.Net.Search.Sort();
                sort.SetSort(new Lucene.Net.Search.SortField("field2", Lucene.Net.Search.SortField.INT));

                Lucene.Net.Search.TopDocs h = searcher.Search(q, null, 100, sort);
                if (h.ScoreDocs.Length != 2)
                {
                    LUCENENET_100_Exception = new SupportClassException("Test_Search_FieldDoc Error. ");
                }
            }
            catch (SupportClassException ex)
            {
                LUCENENET_100_Exception = ex;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                LUCENENET_100_testFinished = true;
            }
        }
Example #2
0
 private Hits GetHitsByFiled(string filedname,Analyzer analyzer, string searchStr, IndexSearcher searcher)
 {
     MultiFieldQueryParser parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_CURRENT, new string[] { filedname}, analyzer);
     Query query = parser.Parse(searchStr);
     Sort sort = new Sort();
     SortField f = new SortField("publish_time", SortField.STRING, true);//按照publish_time字段排序,true表示降序
     sort.SetSort(f);
     Hits hits = searcher.Search(query, sort);
     return hits;
 }
Example #3
0
        public virtual void  TestEmptyIndex()
        {
            Searcher empty = EmptyIndex;

            sort = new Sort();
            AssertMatches(empty, queryX, sort, "");

            sort.SetSort(SortField.FIELD_DOC);
            AssertMatches(empty, queryX, sort, "");

            sort.SetSort(new SortField[] { new SortField("int", SortField.INT), SortField.FIELD_DOC });
            AssertMatches(empty, queryX, sort, "");

            sort.SetSort(new SortField[] { new SortField("string", SortField.STRING, true), SortField.FIELD_DOC });
            AssertMatches(empty, queryX, sort, "");

            sort.SetSort(new SortField[] { new SortField("float", SortField.FLOAT), new SortField("string", SortField.STRING) });
            AssertMatches(empty, queryX, sort, "");
        }
Example #4
0
        public virtual void  TestBuiltInSorts()
        {
            sort = new Sort();
            AssertMatches(full, queryX, sort, "ACEGI");
            AssertMatches(full, queryY, sort, "BDFHJ");

            sort.SetSort(SortField.FIELD_DOC);
            AssertMatches(full, queryX, sort, "ACEGI");
            AssertMatches(full, queryY, sort, "BDFHJ");
        }
        void LUCENENET_100_ClientSearch()
        {
            try
            {
                Lucene.Net.Search.Searchable    s        = (Lucene.Net.Search.Searchable)Activator.GetObject(typeof(Lucene.Net.Search.Searchable), @"tcp://localhost:" + ANYPORT + "/Searcher");
                Lucene.Net.Search.MultiSearcher searcher = new Lucene.Net.Search.MultiSearcher(new Lucene.Net.Search.Searchable[] { s });

                Lucene.Net.Search.Query q = new Lucene.Net.Search.TermQuery(new Lucene.Net.Index.Term("field1", "moon"));

                Lucene.Net.Search.Sort sort = new Lucene.Net.Search.Sort();
                sort.SetSort(new Lucene.Net.Search.SortField("field2", Lucene.Net.Search.SortField.INT));

                Lucene.Net.Search.TopDocs h = searcher.Search(q, null, 100, sort);
            }
            catch (Exception ex)
            {
                LUCENENET_100_Exception = ex;
            }
            finally
            {
                LUCENENET_100_testFinished = true;
            }
        }
Example #6
0
		public virtual void  TestNormalizedScores()
		{
			
			// capture relevancy scores
			System.Collections.Hashtable scoresX = GetScores(full.Search(queryX));
			System.Collections.Hashtable scoresY = GetScores(full.Search(queryY));
			System.Collections.Hashtable scoresA = GetScores(full.Search(queryA));
			
			// we'll test searching locally, remote and multi
			// note: the multi test depends on each separate index containing
			// the same documents as our local index, so the computed normalization
			// will be the same.  so we make a multi searcher over two equal document
			// sets - not realistic, but necessary for testing.
			MultiSearcher remote = new MultiSearcher(new Lucene.Net.Search.Searchable[]{Remote});
			MultiSearcher multi = new MultiSearcher(new Lucene.Net.Search.Searchable[]{full, full});
			
			// change sorting and make sure relevancy stays the same
			
			sort = new Sort();
			AssertSameValues(scoresX, GetScores(full.Search(queryX, sort)));
			AssertSameValues(scoresX, GetScores(remote.Search(queryX, sort)));
			AssertSameValues(scoresX, GetScores(multi.Search(queryX, sort)));
			AssertSameValues(scoresY, GetScores(full.Search(queryY, sort)));
			AssertSameValues(scoresY, GetScores(remote.Search(queryY, sort)));
			AssertSameValues(scoresY, GetScores(multi.Search(queryY, sort)));
			AssertSameValues(scoresA, GetScores(full.Search(queryA, sort)));
			AssertSameValues(scoresA, GetScores(remote.Search(queryA, sort)));
			AssertSameValues(scoresA, GetScores(multi.Search(queryA, sort)));
			
			sort.SetSort(SortField.FIELD_DOC);
			AssertSameValues(scoresX, GetScores(full.Search(queryX, sort)));
			AssertSameValues(scoresX, GetScores(remote.Search(queryX, sort)));
			AssertSameValues(scoresX, GetScores(multi.Search(queryX, sort)));
			AssertSameValues(scoresY, GetScores(full.Search(queryY, sort)));
			AssertSameValues(scoresY, GetScores(remote.Search(queryY, sort)));
			AssertSameValues(scoresY, GetScores(multi.Search(queryY, sort)));
			AssertSameValues(scoresA, GetScores(full.Search(queryA, sort)));
			AssertSameValues(scoresA, GetScores(remote.Search(queryA, sort)));
			AssertSameValues(scoresA, GetScores(multi.Search(queryA, sort)));
			
			sort.SetSort("int");
			AssertSameValues(scoresX, GetScores(full.Search(queryX, sort)));
			AssertSameValues(scoresX, GetScores(remote.Search(queryX, sort)));
			AssertSameValues(scoresX, GetScores(multi.Search(queryX, sort)));
			AssertSameValues(scoresY, GetScores(full.Search(queryY, sort)));
			AssertSameValues(scoresY, GetScores(remote.Search(queryY, sort)));
			AssertSameValues(scoresY, GetScores(multi.Search(queryY, sort)));
			AssertSameValues(scoresA, GetScores(full.Search(queryA, sort)));
			AssertSameValues(scoresA, GetScores(remote.Search(queryA, sort)));
			AssertSameValues(scoresA, GetScores(multi.Search(queryA, sort)));
			
			sort.SetSort("float");
			AssertSameValues(scoresX, GetScores(full.Search(queryX, sort)));
			AssertSameValues(scoresX, GetScores(remote.Search(queryX, sort)));
			AssertSameValues(scoresX, GetScores(multi.Search(queryX, sort)));
			AssertSameValues(scoresY, GetScores(full.Search(queryY, sort)));
			AssertSameValues(scoresY, GetScores(remote.Search(queryY, sort)));
			AssertSameValues(scoresY, GetScores(multi.Search(queryY, sort)));
			AssertSameValues(scoresA, GetScores(full.Search(queryA, sort)));
			AssertSameValues(scoresA, GetScores(remote.Search(queryA, sort)));
			AssertSameValues(scoresA, GetScores(multi.Search(queryA, sort)));
			
			sort.SetSort("string");
			AssertSameValues(scoresX, GetScores(full.Search(queryX, sort)));
			AssertSameValues(scoresX, GetScores(remote.Search(queryX, sort)));
			AssertSameValues(scoresX, GetScores(multi.Search(queryX, sort)));
			AssertSameValues(scoresY, GetScores(full.Search(queryY, sort)));
			AssertSameValues(scoresY, GetScores(remote.Search(queryY, sort)));
			AssertSameValues(scoresY, GetScores(multi.Search(queryY, sort)));
			AssertSameValues(scoresA, GetScores(full.Search(queryA, sort)));
			AssertSameValues(scoresA, GetScores(remote.Search(queryA, sort)));
			AssertSameValues(scoresA, GetScores(multi.Search(queryA, sort)));
			
			sort.SetSort(new System.String[]{"int", "float"});
			AssertSameValues(scoresX, GetScores(full.Search(queryX, sort)));
			AssertSameValues(scoresX, GetScores(remote.Search(queryX, sort)));
			AssertSameValues(scoresX, GetScores(multi.Search(queryX, sort)));
			AssertSameValues(scoresY, GetScores(full.Search(queryY, sort)));
			AssertSameValues(scoresY, GetScores(remote.Search(queryY, sort)));
			AssertSameValues(scoresY, GetScores(multi.Search(queryY, sort)));
			AssertSameValues(scoresA, GetScores(full.Search(queryA, sort)));
			AssertSameValues(scoresA, GetScores(remote.Search(queryA, sort)));
			AssertSameValues(scoresA, GetScores(multi.Search(queryA, sort)));
			
			sort.SetSort(new SortField[]{new SortField("int", true), new SortField(null, SortField.DOC, true)});
			AssertSameValues(scoresX, GetScores(full.Search(queryX, sort)));
			AssertSameValues(scoresX, GetScores(remote.Search(queryX, sort)));
			AssertSameValues(scoresX, GetScores(multi.Search(queryX, sort)));
			AssertSameValues(scoresY, GetScores(full.Search(queryY, sort)));
			AssertSameValues(scoresY, GetScores(remote.Search(queryY, sort)));
			AssertSameValues(scoresY, GetScores(multi.Search(queryY, sort)));
			AssertSameValues(scoresA, GetScores(full.Search(queryA, sort)));
			AssertSameValues(scoresA, GetScores(remote.Search(queryA, sort)));
			AssertSameValues(scoresA, GetScores(multi.Search(queryA, sort)));
			
			sort.SetSort(new System.String[]{"float", "string"});
			AssertSameValues(scoresX, GetScores(full.Search(queryX, sort)));
			AssertSameValues(scoresX, GetScores(remote.Search(queryX, sort)));
			AssertSameValues(scoresX, GetScores(multi.Search(queryX, sort)));
			AssertSameValues(scoresY, GetScores(full.Search(queryY, sort)));
			AssertSameValues(scoresY, GetScores(remote.Search(queryY, sort)));
			AssertSameValues(scoresY, GetScores(multi.Search(queryY, sort)));
			AssertSameValues(scoresA, GetScores(full.Search(queryA, sort)));
			AssertSameValues(scoresA, GetScores(remote.Search(queryA, sort)));
			AssertSameValues(scoresA, GetScores(multi.Search(queryA, sort)));
		}
Example #7
0
		public virtual void  TestEmptyIndex()
		{
			Searcher empty = EmptyIndex;
			
			sort = new Sort();
			AssertMatches(empty, queryX, sort, "");
			
			sort.SetSort(SortField.FIELD_DOC);
			AssertMatches(empty, queryX, sort, "");
			
			sort.SetSort(new SortField[]{new SortField("int", SortField.INT), SortField.FIELD_DOC});
			AssertMatches(empty, queryX, sort, "");
			
			sort.SetSort(new SortField[]{new SortField("string", SortField.STRING, true), SortField.FIELD_DOC});
			AssertMatches(empty, queryX, sort, "");
			
			sort.SetSort(new SortField[]{new SortField("float", SortField.FLOAT), new SortField("string", SortField.STRING)});
			AssertMatches(empty, queryX, sort, "");
		}
Example #8
0
		public virtual void  TestBuiltInSorts()
		{
			sort = new Sort();
			AssertMatches(full, queryX, sort, "ACEGI");
			AssertMatches(full, queryY, sort, "BDFHJ");
			
			sort.SetSort(SortField.FIELD_DOC);
			AssertMatches(full, queryX, sort, "ACEGI");
			AssertMatches(full, queryY, sort, "BDFHJ");
		}
Example #9
0
		public virtual void  TestNormalizedScores()
		{
			
			// capture relevancy scores
			System.Collections.Hashtable scoresX = GetScores(full.Search(queryX, null, 1000).ScoreDocs, full);
			System.Collections.Hashtable scoresY = GetScores(full.Search(queryY, null, 1000).ScoreDocs, full);
			System.Collections.Hashtable scoresA = GetScores(full.Search(queryA, null, 1000).ScoreDocs, full);
			
			// we'll test searching locally, remote and multi
			
			MultiSearcher multi = new MultiSearcher(new Searchable[]{searchX, searchY});
			
			// change sorting and make sure relevancy stays the same
			
			sort = new Sort();
			AssertSameValues(scoresX, GetScores(full.Search(queryX, null, 1000, sort).ScoreDocs, full));
			AssertSameValues(scoresX, GetScores(multi.Search(queryX, null, 1000, sort).ScoreDocs, multi));
			AssertSameValues(scoresY, GetScores(full.Search(queryY, null, 1000, sort).ScoreDocs, full));
			AssertSameValues(scoresY, GetScores(multi.Search(queryY, null, 1000, sort).ScoreDocs, multi));
			AssertSameValues(scoresA, GetScores(full.Search(queryA, null, 1000, sort).ScoreDocs, full));
			AssertSameValues(scoresA, GetScores(multi.Search(queryA, null, 1000, sort).ScoreDocs, multi));
			
			sort.SetSort(SortField.FIELD_DOC);
			AssertSameValues(scoresX, GetScores(full.Search(queryX, null, 1000, sort).ScoreDocs, full));
			AssertSameValues(scoresX, GetScores(multi.Search(queryX, null, 1000, sort).ScoreDocs, multi));
			AssertSameValues(scoresY, GetScores(full.Search(queryY, null, 1000, sort).ScoreDocs, full));
			AssertSameValues(scoresY, GetScores(multi.Search(queryY, null, 1000, sort).ScoreDocs, multi));
			AssertSameValues(scoresA, GetScores(full.Search(queryA, null, 1000, sort).ScoreDocs, full));
			AssertSameValues(scoresA, GetScores(multi.Search(queryA, null, 1000, sort).ScoreDocs, multi));
			
			sort.SetSort("int");
			AssertSameValues(scoresX, GetScores(full.Search(queryX, null, 1000, sort).ScoreDocs, full));
			AssertSameValues(scoresX, GetScores(multi.Search(queryX, null, 1000, sort).ScoreDocs, multi));
			AssertSameValues(scoresY, GetScores(full.Search(queryY, null, 1000, sort).ScoreDocs, full));
			AssertSameValues(scoresY, GetScores(multi.Search(queryY, null, 1000, sort).ScoreDocs, multi));
			AssertSameValues(scoresA, GetScores(full.Search(queryA, null, 1000, sort).ScoreDocs, full));
			AssertSameValues(scoresA, GetScores(multi.Search(queryA, null, 1000, sort).ScoreDocs, multi));
			
			sort.SetSort("float");
			AssertSameValues(scoresX, GetScores(full.Search(queryX, null, 1000, sort).ScoreDocs, full));
			AssertSameValues(scoresX, GetScores(multi.Search(queryX, null, 1000, sort).ScoreDocs, multi));
			AssertSameValues(scoresY, GetScores(full.Search(queryY, null, 1000, sort).ScoreDocs, full));
			AssertSameValues(scoresY, GetScores(multi.Search(queryY, null, 1000, sort).ScoreDocs, multi));
			AssertSameValues(scoresA, GetScores(full.Search(queryA, null, 1000, sort).ScoreDocs, full));
			AssertSameValues(scoresA, GetScores(multi.Search(queryA, null, 1000, sort).ScoreDocs, multi));
			
			sort.SetSort("string");
			AssertSameValues(scoresX, GetScores(full.Search(queryX, null, 1000, sort).ScoreDocs, full));
			AssertSameValues(scoresX, GetScores(multi.Search(queryX, null, 1000, sort).ScoreDocs, multi));
			AssertSameValues(scoresY, GetScores(full.Search(queryY, null, 1000, sort).ScoreDocs, full));
			AssertSameValues(scoresY, GetScores(multi.Search(queryY, null, 1000, sort).ScoreDocs, multi));
			AssertSameValues(scoresA, GetScores(full.Search(queryA, null, 1000, sort).ScoreDocs, full));
			AssertSameValues(scoresA, GetScores(multi.Search(queryA, null, 1000, sort).ScoreDocs, multi));
			
			sort.SetSort(new System.String[]{"int", "float"});
			AssertSameValues(scoresX, GetScores(full.Search(queryX, null, 1000, sort).ScoreDocs, full));
			AssertSameValues(scoresX, GetScores(multi.Search(queryX, null, 1000, sort).ScoreDocs, multi));
			AssertSameValues(scoresY, GetScores(full.Search(queryY, null, 1000, sort).ScoreDocs, full));
			AssertSameValues(scoresY, GetScores(multi.Search(queryY, null, 1000, sort).ScoreDocs, multi));
			AssertSameValues(scoresA, GetScores(full.Search(queryA, null, 1000, sort).ScoreDocs, full));
			AssertSameValues(scoresA, GetScores(multi.Search(queryA, null, 1000, sort).ScoreDocs, multi));
			
			sort.SetSort(new SortField[]{new SortField("int", true), new SortField(null, SortField.DOC, true)});
			AssertSameValues(scoresX, GetScores(full.Search(queryX, null, 1000, sort).ScoreDocs, full));
			AssertSameValues(scoresX, GetScores(multi.Search(queryX, null, 1000, sort).ScoreDocs, multi));
			AssertSameValues(scoresY, GetScores(full.Search(queryY, null, 1000, sort).ScoreDocs, full));
			AssertSameValues(scoresY, GetScores(multi.Search(queryY, null, 1000, sort).ScoreDocs, multi));
			AssertSameValues(scoresA, GetScores(full.Search(queryA, null, 1000, sort).ScoreDocs, full));
			AssertSameValues(scoresA, GetScores(multi.Search(queryA, null, 1000, sort).ScoreDocs, multi));
			
			sort.SetSort(new System.String[]{"float", "string"});
			AssertSameValues(scoresX, GetScores(full.Search(queryX, null, 1000, sort).ScoreDocs, full));
			AssertSameValues(scoresX, GetScores(multi.Search(queryX, null, 1000, sort).ScoreDocs, multi));
			AssertSameValues(scoresY, GetScores(full.Search(queryY, null, 1000, sort).ScoreDocs, full));
			AssertSameValues(scoresY, GetScores(multi.Search(queryY, null, 1000, sort).ScoreDocs, multi));
			AssertSameValues(scoresA, GetScores(full.Search(queryA, null, 1000, sort).ScoreDocs, full));
			AssertSameValues(scoresA, GetScores(multi.Search(queryA, null, 1000, sort).ScoreDocs, multi));
		}
Example #10
0
		public virtual void  TestSort()
		{
			this.r = NewRandom();
			
			// reverse & not
			// all types
			// restrictive & non restrictive searches (on contents)
			
			Create();
			
			Sort[] sorts = new Sort[50];
			int sortCount = 0;
			
			for (int r = 0; r < 2; r++)
			{
				Sort sort;
				bool reverse = 1 == r;
				
				sorts[sortCount++] = sort = new Sort();
				sort.SetSort(new SortField[]{new SortField("byte", SortField.BYTE, reverse)});
				
				sorts[sortCount++] = sort = new Sort();
				sort.SetSort(new SortField[]{new SortField("short", SortField.SHORT, reverse)});
				
				sorts[sortCount++] = sort = new Sort();
				sort.SetSort(new SortField[]{new SortField("int", SortField.INT, reverse)});
				
				sorts[sortCount++] = sort = new Sort();
				sort.SetSort(new SortField[]{new SortField("long", SortField.LONG, reverse)});
				
				sorts[sortCount++] = sort = new Sort();
				sort.SetSort(new SortField[]{new SortField("float", SortField.FLOAT, reverse)});
				
				sorts[sortCount++] = sort = new Sort();
				sort.SetSort(new SortField[]{new SortField("double", SortField.DOUBLE, reverse)});
				
				sorts[sortCount++] = sort = new Sort();
				sort.SetSort(new SortField[]{new SortField("string", SortField.STRING_VAL, reverse)});
				
				sorts[sortCount++] = sort = new Sort();
				sort.SetSort(new SortField[]{new SortField("stringIdx", SortField.STRING, reverse)});
				
				//sorts[sortCount++] = sort = new Sort();
				//sort.setSort(new SortField[] {new SortField("string", SortField.STRING_ORD, reverse)});
				
				//sorts[sortCount++] = sort = new Sort();
				//sort.setSort(new SortField[] {new SortField("string", SortField.STRING_ORD_VAL, reverse)});
				
				//sorts[sortCount++] = sort = new Sort();
				//sort.setSort(new SortField[] {new SortField("string", SortField.STRING_ORD_VAL_DEM, reverse)});
				
				//sorts[sortCount++] = sort = new Sort();
				//sort.setSort(new SortField[] {new SortField("string", SortField.STRING_ORD_VAL_DEM2, reverse)});
				
				sorts[sortCount++] = sort = new Sort();
				sort.SetSort(new SortField[]{new SortField(null, SortField.SCORE, reverse)});
				
				sorts[sortCount++] = sort = new Sort();
				sort.SetSort(new SortField[]{new SortField(null, SortField.DOC, reverse)});
			}
			
			Query[] queries = new Query[4];
			queries[0] = new MatchAllDocsQuery();
			queries[1] = new TermQuery(new Term("contents", "x")); // matches every 10th doc
			queries[2] = new TermQuery(new Term("contents", "y")); // matches every 100th doc
			queries[3] = new TermQuery(new Term("contents", "z")); // matches every 1000th doc
			
			for (int sx = 0; sx < 3; sx++)
			{
				IndexSearcher searcher;
				if (sx == 0)
				{
					searcher = searcherSingleSegment;
				}
				else if (sx == 1)
				{
					searcher = searcherFewSegment;
				}
				else
				{
					searcher = searcherMultiSegment;
				}
				
				for (int qx = 0; qx < queries.Length; qx++)
				{
					Query query = queries[qx];
					
					for (int q = 0; q < 3; q++)
					{
						
						int queueSize;
						if (q == 0)
						{
							queueSize = 10;
						}
						else if (q == 1)
						{
							queueSize = 100;
						}
						else
						{
							queueSize = 1000;
						}
						
						for (int s = 0; s < sortCount; s++)
						{
							Sort sort1 = sorts[s];
							
							for (int s2 = - 1; s2 < sortCount; s2++)
							{
								Sort sort;
								if (s2 == - 1)
								{
									// Single field sort
									sort = sort1;
								}
								else
								{
									sort = new Sort(new SortField[]{sort1.GetSort()[0], sorts[s2].GetSort()[0]});
								}
								
								// Old
								Sort oldSort = GetOldSort(sort);
								
								if (VERBOSE)
								{
									System.Console.Out.WriteLine("query=" + query);
									if (sx == 0)
									{
										System.Console.Out.WriteLine("  single-segment index");
									}
									else if (sx == 1)
									{
										System.Console.Out.WriteLine("  few-segment index");
									}
									else
									{
										System.Console.Out.WriteLine("  many-segment index");
									}
									System.Console.Out.WriteLine("  numHit=" + queueSize);
									System.Console.Out.WriteLine("  old=" + oldSort);
									System.Console.Out.WriteLine("  new=" + sort);
								}
								
								TopDocs newHits = searcher.Search(query, null, queueSize, sort);
								TopDocs oldHits = searcher.Search(query, null, queueSize, oldSort);
								
								Compare(oldHits, newHits);
							}
						}
					}
				}
			}
			
			// we explicitly test the old sort method and
			// compare with the new, so we expect to see SUBREADER
			// sanity checks fail.
			Insanity[] insanity = FieldCacheSanityChecker.CheckSanity(Lucene.Net.Search.FieldCache_Fields.DEFAULT);
			try
			{
				int ignored = 0;
				for (int i = 0; i < insanity.Length; i++)
				{
					if (insanity[i].GetType() == InsanityType.SUBREADER)
					{
						insanity[i] = new Insanity(InsanityType.EXPECTED, insanity[i].GetMsg(), insanity[i].GetCacheEntries());
						ignored++;
					}
				}
				Assert.AreEqual(ignored, insanity.Length, "Not all insane field cache usage was expected");
				
				insanity = null;
			}
			finally
			{
				// report this in the event of any exception/failure
				// if no failure, then insanity will be null
				if (null != insanity)
				{
					System.IO.StreamWriter temp_writer;
					temp_writer = new System.IO.StreamWriter(System.Console.OpenStandardError(), System.Console.Error.Encoding);
					temp_writer.AutoFlush = true;
					DumpArray(GetTestLabel() + ": Insane FieldCache usage(s)", insanity, temp_writer);
				}
			}
			// we've already checked FieldCache, purge so tearDown doesn't complain
			PurgeFieldCache(Lucene.Net.Search.FieldCache_Fields.DEFAULT); // so
			
			Close();
		}
Example #11
0
        public virtual void  TestSort()
        {
            this.r = NewRandom();

            // reverse & not
            // all types
            // restrictive & non restrictive searches (on contents)

            Create();

            Sort[] sorts     = new Sort[50];
            int    sortCount = 0;

            for (int r = 0; r < 2; r++)
            {
                Sort sort;
                bool reverse = 1 == r;

                sorts[sortCount++] = sort = new Sort();
                sort.SetSort(new SortField[] { new SortField("byte", SortField.BYTE, reverse) });

                sorts[sortCount++] = sort = new Sort();
                sort.SetSort(new SortField[] { new SortField("short", SortField.SHORT, reverse) });

                sorts[sortCount++] = sort = new Sort();
                sort.SetSort(new SortField[] { new SortField("int", SortField.INT, reverse) });

                sorts[sortCount++] = sort = new Sort();
                sort.SetSort(new SortField[] { new SortField("long", SortField.LONG, reverse) });

                sorts[sortCount++] = sort = new Sort();
                sort.SetSort(new SortField[] { new SortField("float", SortField.FLOAT, reverse) });

                sorts[sortCount++] = sort = new Sort();
                sort.SetSort(new SortField[] { new SortField("double", SortField.DOUBLE, reverse) });

                sorts[sortCount++] = sort = new Sort();
                sort.SetSort(new SortField[] { new SortField("string", SortField.STRING_VAL, reverse) });

                sorts[sortCount++] = sort = new Sort();
                sort.SetSort(new SortField[] { new SortField("stringIdx", SortField.STRING, reverse) });

                //sorts[sortCount++] = sort = new Sort();
                //sort.setSort(new SortField[] {new SortField("string", SortField.STRING_ORD, reverse)});

                //sorts[sortCount++] = sort = new Sort();
                //sort.setSort(new SortField[] {new SortField("string", SortField.STRING_ORD_VAL, reverse)});

                //sorts[sortCount++] = sort = new Sort();
                //sort.setSort(new SortField[] {new SortField("string", SortField.STRING_ORD_VAL_DEM, reverse)});

                //sorts[sortCount++] = sort = new Sort();
                //sort.setSort(new SortField[] {new SortField("string", SortField.STRING_ORD_VAL_DEM2, reverse)});

                sorts[sortCount++] = sort = new Sort();
                sort.SetSort(new SortField[] { new SortField(null, SortField.SCORE, reverse) });

                sorts[sortCount++] = sort = new Sort();
                sort.SetSort(new SortField[] { new SortField(null, SortField.DOC, reverse) });
            }

            Query[] queries = new Query[4];
            queries[0] = new MatchAllDocsQuery();
            queries[1] = new TermQuery(new Term("contents", "x"));             // matches every 10th doc
            queries[2] = new TermQuery(new Term("contents", "y"));             // matches every 100th doc
            queries[3] = new TermQuery(new Term("contents", "z"));             // matches every 1000th doc

            for (int sx = 0; sx < 3; sx++)
            {
                IndexSearcher searcher;
                if (sx == 0)
                {
                    searcher = searcherSingleSegment;
                }
                else if (sx == 1)
                {
                    searcher = searcherFewSegment;
                }
                else
                {
                    searcher = searcherMultiSegment;
                }

                for (int qx = 0; qx < queries.Length; qx++)
                {
                    Query query = queries[qx];

                    for (int q = 0; q < 3; q++)
                    {
                        int queueSize;
                        if (q == 0)
                        {
                            queueSize = 10;
                        }
                        else if (q == 1)
                        {
                            queueSize = 100;
                        }
                        else
                        {
                            queueSize = 1000;
                        }

                        for (int s = 0; s < sortCount; s++)
                        {
                            Sort sort1 = sorts[s];

                            for (int s2 = -1; s2 < sortCount; s2++)
                            {
                                Sort sort;
                                if (s2 == -1)
                                {
                                    // Single field sort
                                    sort = sort1;
                                }
                                else
                                {
                                    sort = new Sort(new SortField[] { sort1.GetSort()[0], sorts[s2].GetSort()[0] });
                                }

                                // Old
                                Sort oldSort = GetOldSort(sort);

                                if (VERBOSE)
                                {
                                    System.Console.Out.WriteLine("query=" + query);
                                    if (sx == 0)
                                    {
                                        System.Console.Out.WriteLine("  single-segment index");
                                    }
                                    else if (sx == 1)
                                    {
                                        System.Console.Out.WriteLine("  few-segment index");
                                    }
                                    else
                                    {
                                        System.Console.Out.WriteLine("  many-segment index");
                                    }
                                    System.Console.Out.WriteLine("  numHit=" + queueSize);
                                    System.Console.Out.WriteLine("  old=" + oldSort);
                                    System.Console.Out.WriteLine("  new=" + sort);
                                }

                                TopDocs newHits = searcher.Search(query, null, queueSize, sort);
                                TopDocs oldHits = searcher.Search(query, null, queueSize, oldSort);

                                Compare(oldHits, newHits);
                            }
                        }
                    }
                }
            }

            // we explicitly test the old sort method and
            // compare with the new, so we expect to see SUBREADER
            // sanity checks fail.
            Insanity[] insanity = FieldCacheSanityChecker.CheckSanity(Lucene.Net.Search.FieldCache_Fields.DEFAULT);
            try
            {
                int ignored = 0;
                for (int i = 0; i < insanity.Length; i++)
                {
                    if (insanity[i].GetType() == InsanityType.SUBREADER)
                    {
                        insanity[i] = new Insanity(InsanityType.EXPECTED, insanity[i].GetMsg(), insanity[i].GetCacheEntries());
                        ignored++;
                    }
                }
                Assert.AreEqual(ignored, insanity.Length, "Not all insane field cache usage was expected");

                insanity = null;
            }
            finally
            {
                // report this in the event of any exception/failure
                // if no failure, then insanity will be null
                if (null != insanity)
                {
                    System.IO.StreamWriter temp_writer;
                    temp_writer           = new System.IO.StreamWriter(System.Console.OpenStandardError(), System.Console.Error.Encoding);
                    temp_writer.AutoFlush = true;
                    DumpArray(GetTestLabel() + ": Insane FieldCache usage(s)", insanity, temp_writer);
                }
            }
            // we've already checked FieldCache, purge so tearDown doesn't complain
            PurgeFieldCache(Lucene.Net.Search.FieldCache_Fields.DEFAULT);             // so

            Close();
        }
Example #12
0
        public virtual void TestEmptyIndex()
        {
            IndexSearcher empty = NewSearcher(new MultiReader());
            Query query = new TermQuery(new Term("contents", "foo"));

            Sort sort = new Sort();
            TopDocs td = empty.Search(query, null, 10, sort, true, true);
            Assert.AreEqual(0, td.TotalHits);

            sort.SetSort(SortField.FIELD_DOC);
            td = empty.Search(query, null, 10, sort, true, true);
            Assert.AreEqual(0, td.TotalHits);

            sort.SetSort(new SortField("int", SortField.Type_e.INT), SortField.FIELD_DOC);
            td = empty.Search(query, null, 10, sort, true, true);
            Assert.AreEqual(0, td.TotalHits);

            sort.SetSort(new SortField("string", SortField.Type_e.STRING, true), SortField.FIELD_DOC);
            td = empty.Search(query, null, 10, sort, true, true);
            Assert.AreEqual(0, td.TotalHits);

            sort.SetSort(new SortField("string_val", SortField.Type_e.STRING_VAL, true), SortField.FIELD_DOC);
            td = empty.Search(query, null, 10, sort, true, true);
            Assert.AreEqual(0, td.TotalHits);

            sort.SetSort(new SortField("float", SortField.Type_e.FLOAT), new SortField("string", SortField.Type_e.STRING));
            td = empty.Search(query, null, 10, sort, true, true);
            Assert.AreEqual(0, td.TotalHits);
        }
        public void TestEmptyIndex()
        {
            IndexSearcher empty = NewSearcher(new MultiReader());
            Query query = new TermQuery(new Term("contents", "foo"));

            Sort sort = new Sort();
            sort.SetSort(new SortedSetSortField("sortedset", false));
            TopDocs td = empty.Search(query, null, 10, sort, true, true);
            assertEquals(0, td.TotalHits);

            // for an empty index, any selector should work
            foreach (Selector v in Enum.GetValues(typeof(Selector)))
            {
                sort.SetSort(new SortedSetSortField("sortedset", false, v));
                td = empty.Search(query, null, 10, sort, true, true);
                assertEquals(0, td.TotalHits);
            }
        }
        void LUCENENET_100_ClientSearch()
        {
            try
            {
                Lucene.Net.Search.Searchable s = (Lucene.Net.Search.Searchable)Activator.GetObject(typeof(Lucene.Net.Search.Searchable), @"tcp://localhost:" + ANYPORT  + "/Searcher");
                Lucene.Net.Search.MultiSearcher searcher = new Lucene.Net.Search.MultiSearcher(new Lucene.Net.Search.Searchable[] { s });

                Lucene.Net.Search.Query q = new Lucene.Net.Search.TermQuery(new Lucene.Net.Index.Term("field1", "moon"));

                Lucene.Net.Search.Sort sort = new Lucene.Net.Search.Sort();
                sort.SetSort(new Lucene.Net.Search.SortField("field2", Lucene.Net.Search.SortField.INT));

                Lucene.Net.Search.TopDocs h = searcher.Search(q, null,100, sort);
                if (h.ScoreDocs.Length != 2) LUCENENET_100_Exception = new Exception("Test_Search_FieldDoc Error. ");
            }
            catch (Exception ex)
            {
                LUCENENET_100_Exception = ex;
            }
            finally
            {
                LUCENENET_100_testFinished = true;
            }
        }
            void TestIndexAndSearch(string tempDir)
            {
                Lucene.Net.Store.Directory dir = Lucene.Net.Store.FSDirectory.Open(new System.IO.DirectoryInfo(tempDir));

                Lucene.Net.Index.IndexWriter w = new Lucene.Net.Index.IndexWriter(dir, new Lucene.Net.Analysis.Standard.StandardAnalyzer(), true);
                Lucene.Net.Documents.Field f1 = new Lucene.Net.Documents.Field("field1", "dark side of the moon", Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.TOKENIZED);
                Lucene.Net.Documents.Field f2 = new Lucene.Net.Documents.Field("field2", "123", Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.UN_TOKENIZED);
                Lucene.Net.Documents.Document d = new Lucene.Net.Documents.Document();
                d.Add(f1);
                d.Add(f2);
                w.AddDocument(d);

                f1 = new Lucene.Net.Documents.Field("field1", "Fly me to the moon", Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.TOKENIZED);
                f2 = new Lucene.Net.Documents.Field("field2", "456", Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.UN_TOKENIZED);
                d = new Lucene.Net.Documents.Document();
                d.Add(f1);
                d.Add(f2);
                w.AddDocument(d);

                w.Close();

                Lucene.Net.Search.IndexSearcher searcher = new IndexSearcher(dir, true);

                Lucene.Net.Search.Sort sort = new Lucene.Net.Search.Sort();
                sort.SetSort(new Lucene.Net.Search.SortField("field2", Lucene.Net.Search.SortField.STRING));
                Lucene.Net.Search.Query q = new Lucene.Net.QueryParsers.QueryParser("field1", new Lucene.Net.Analysis.Standard.StandardAnalyzer()).Parse("moon");
                TopDocs td = searcher.Search(q, null, 100, sort);
                int resCount = td.ScoreDocs.Length;

                searcher.Close();
            }
Example #16
0
        public virtual void  TestNormalizedScores()
        {
            // capture relevancy scores
            System.Collections.Hashtable scoresX = GetScores(full.Search(queryX));
            System.Collections.Hashtable scoresY = GetScores(full.Search(queryY));
            System.Collections.Hashtable scoresA = GetScores(full.Search(queryA));

            // we'll test searching locally, remote and multi
            // note: the multi test depends on each separate index containing
            // the same documents as our local index, so the computed normalization
            // will be the same.  so we make a multi searcher over two equal document
            // sets - not realistic, but necessary for testing.
            MultiSearcher remote = new MultiSearcher(new Lucene.Net.Search.Searchable[] { Remote });
            MultiSearcher multi  = new MultiSearcher(new Lucene.Net.Search.Searchable[] { full, full });

            // change sorting and make sure relevancy stays the same

            sort = new Sort();
            AssertSameValues(scoresX, GetScores(full.Search(queryX, sort)));
            AssertSameValues(scoresX, GetScores(remote.Search(queryX, sort)));
            AssertSameValues(scoresX, GetScores(multi.Search(queryX, sort)));
            AssertSameValues(scoresY, GetScores(full.Search(queryY, sort)));
            AssertSameValues(scoresY, GetScores(remote.Search(queryY, sort)));
            AssertSameValues(scoresY, GetScores(multi.Search(queryY, sort)));
            AssertSameValues(scoresA, GetScores(full.Search(queryA, sort)));
            AssertSameValues(scoresA, GetScores(remote.Search(queryA, sort)));
            AssertSameValues(scoresA, GetScores(multi.Search(queryA, sort)));

            sort.SetSort(SortField.FIELD_DOC);
            AssertSameValues(scoresX, GetScores(full.Search(queryX, sort)));
            AssertSameValues(scoresX, GetScores(remote.Search(queryX, sort)));
            AssertSameValues(scoresX, GetScores(multi.Search(queryX, sort)));
            AssertSameValues(scoresY, GetScores(full.Search(queryY, sort)));
            AssertSameValues(scoresY, GetScores(remote.Search(queryY, sort)));
            AssertSameValues(scoresY, GetScores(multi.Search(queryY, sort)));
            AssertSameValues(scoresA, GetScores(full.Search(queryA, sort)));
            AssertSameValues(scoresA, GetScores(remote.Search(queryA, sort)));
            AssertSameValues(scoresA, GetScores(multi.Search(queryA, sort)));

            sort.SetSort("int");
            AssertSameValues(scoresX, GetScores(full.Search(queryX, sort)));
            AssertSameValues(scoresX, GetScores(remote.Search(queryX, sort)));
            AssertSameValues(scoresX, GetScores(multi.Search(queryX, sort)));
            AssertSameValues(scoresY, GetScores(full.Search(queryY, sort)));
            AssertSameValues(scoresY, GetScores(remote.Search(queryY, sort)));
            AssertSameValues(scoresY, GetScores(multi.Search(queryY, sort)));
            AssertSameValues(scoresA, GetScores(full.Search(queryA, sort)));
            AssertSameValues(scoresA, GetScores(remote.Search(queryA, sort)));
            AssertSameValues(scoresA, GetScores(multi.Search(queryA, sort)));

            sort.SetSort("float");
            AssertSameValues(scoresX, GetScores(full.Search(queryX, sort)));
            AssertSameValues(scoresX, GetScores(remote.Search(queryX, sort)));
            AssertSameValues(scoresX, GetScores(multi.Search(queryX, sort)));
            AssertSameValues(scoresY, GetScores(full.Search(queryY, sort)));
            AssertSameValues(scoresY, GetScores(remote.Search(queryY, sort)));
            AssertSameValues(scoresY, GetScores(multi.Search(queryY, sort)));
            AssertSameValues(scoresA, GetScores(full.Search(queryA, sort)));
            AssertSameValues(scoresA, GetScores(remote.Search(queryA, sort)));
            AssertSameValues(scoresA, GetScores(multi.Search(queryA, sort)));

            sort.SetSort("string");
            AssertSameValues(scoresX, GetScores(full.Search(queryX, sort)));
            AssertSameValues(scoresX, GetScores(remote.Search(queryX, sort)));
            AssertSameValues(scoresX, GetScores(multi.Search(queryX, sort)));
            AssertSameValues(scoresY, GetScores(full.Search(queryY, sort)));
            AssertSameValues(scoresY, GetScores(remote.Search(queryY, sort)));
            AssertSameValues(scoresY, GetScores(multi.Search(queryY, sort)));
            AssertSameValues(scoresA, GetScores(full.Search(queryA, sort)));
            AssertSameValues(scoresA, GetScores(remote.Search(queryA, sort)));
            AssertSameValues(scoresA, GetScores(multi.Search(queryA, sort)));

            sort.SetSort(new System.String[] { "int", "float" });
            AssertSameValues(scoresX, GetScores(full.Search(queryX, sort)));
            AssertSameValues(scoresX, GetScores(remote.Search(queryX, sort)));
            AssertSameValues(scoresX, GetScores(multi.Search(queryX, sort)));
            AssertSameValues(scoresY, GetScores(full.Search(queryY, sort)));
            AssertSameValues(scoresY, GetScores(remote.Search(queryY, sort)));
            AssertSameValues(scoresY, GetScores(multi.Search(queryY, sort)));
            AssertSameValues(scoresA, GetScores(full.Search(queryA, sort)));
            AssertSameValues(scoresA, GetScores(remote.Search(queryA, sort)));
            AssertSameValues(scoresA, GetScores(multi.Search(queryA, sort)));

            sort.SetSort(new SortField[] { new SortField("int", true), new SortField(null, SortField.DOC, true) });
            AssertSameValues(scoresX, GetScores(full.Search(queryX, sort)));
            AssertSameValues(scoresX, GetScores(remote.Search(queryX, sort)));
            AssertSameValues(scoresX, GetScores(multi.Search(queryX, sort)));
            AssertSameValues(scoresY, GetScores(full.Search(queryY, sort)));
            AssertSameValues(scoresY, GetScores(remote.Search(queryY, sort)));
            AssertSameValues(scoresY, GetScores(multi.Search(queryY, sort)));
            AssertSameValues(scoresA, GetScores(full.Search(queryA, sort)));
            AssertSameValues(scoresA, GetScores(remote.Search(queryA, sort)));
            AssertSameValues(scoresA, GetScores(multi.Search(queryA, sort)));

            sort.SetSort(new System.String[] { "float", "string" });
            AssertSameValues(scoresX, GetScores(full.Search(queryX, sort)));
            AssertSameValues(scoresX, GetScores(remote.Search(queryX, sort)));
            AssertSameValues(scoresX, GetScores(multi.Search(queryX, sort)));
            AssertSameValues(scoresY, GetScores(full.Search(queryY, sort)));
            AssertSameValues(scoresY, GetScores(remote.Search(queryY, sort)));
            AssertSameValues(scoresY, GetScores(multi.Search(queryY, sort)));
            AssertSameValues(scoresA, GetScores(full.Search(queryA, sort)));
            AssertSameValues(scoresA, GetScores(remote.Search(queryA, sort)));
            AssertSameValues(scoresA, GetScores(multi.Search(queryA, sort)));
        }
Example #17
0
        public virtual void  TestTypedSort()
        {
            sort.SetSort(new SortField[] { new SortField("int", SortField.INT), SortField.FIELD_DOC });
            AssertMatches(full, queryX, sort, "IGAEC");
            AssertMatches(full, queryY, sort, "DHFJB");

            sort.SetSort(new SortField[] { new SortField("float", SortField.FLOAT), SortField.FIELD_DOC });
            AssertMatches(full, queryX, sort, "GCIEA");
            AssertMatches(full, queryY, sort, "DHJFB");

            sort.SetSort(new SortField[] { new SortField("string", SortField.STRING), SortField.FIELD_DOC });
            AssertMatches(full, queryX, sort, "AIGEC");
            AssertMatches(full, queryY, sort, "DJHFB");
        }