예제 #1
0
        public virtual void  TestFilteredQuery_()
        {
            Query filteredquery = new FilteredQuery(query, filter);
            Hits  hits          = searcher.Search(filteredquery);

            Assert.AreEqual(1, hits.Length());
            Assert.AreEqual(1, hits.Id(0));

            hits = searcher.Search(filteredquery, new Sort("sorter"));
            Assert.AreEqual(1, hits.Length());
            Assert.AreEqual(1, hits.Id(0));

            filteredquery = new FilteredQuery(new TermQuery(new Term("Field", "one")), filter);
            hits          = searcher.Search(filteredquery);
            Assert.AreEqual(2, hits.Length());

            filteredquery = new FilteredQuery(new TermQuery(new Term("Field", "x")), filter);
            hits          = searcher.Search(filteredquery);
            Assert.AreEqual(1, hits.Length());
            Assert.AreEqual(3, hits.Id(0));

            filteredquery = new FilteredQuery(new TermQuery(new Term("Field", "y")), filter);
            hits          = searcher.Search(filteredquery);
            Assert.AreEqual(0, hits.Length());
        }
예제 #2
0
        public virtual void  TestInclusiveLowerNull()
        {
            //http://issues.apache.org/jira/browse/LUCENE-38
            Analyzer analyzer = new SingleCharAnalyzer();
            Query    query    = new TermRangeQuery("content", null, "C", true, true);

            InitializeIndex(new System.String[] { "A", "B", "", "C", "D" }, analyzer);
            IndexSearcher searcher = new IndexSearcher(dir);
            Hits          hits     = searcher.Search(query);

            // When Lucene-38 is fixed, use the assert on the next line:
            Assert.AreEqual(4, hits.Length(), "A,B,<empty string>,C,D => A,B,<empty string>,C in range");
            // until Lucene-38 is fixed, use this assert
            //Assert.AreEqual(3, hits.length(), "A,B,<empty string>,C,D => A,B,<empty string>,C in range");
            searcher.Close();
            InitializeIndex(new System.String[] { "A", "B", "", "D" }, analyzer);
            searcher = new IndexSearcher(dir);
            hits     = searcher.Search(query);
            // When Lucene-38 is fixed, use the assert on the next line:
            Assert.AreEqual(3, hits.Length(), "A,B,<empty string>,D - A, B and <empty string> in range");
            // until Lucene-38 is fixed, use this assert
            //Assert.AreEqual(2, hits.length(), "A,B,<empty string>,D => A, B and <empty string> in range");
            searcher.Close();
            AddDoc("C");
            searcher = new IndexSearcher(dir);
            hits     = searcher.Search(query);
            // When Lucene-38 is fixed, use the assert on the next line:
            Assert.AreEqual(4, hits.Length(), "C added => A,B,<empty string>,C in range");
            // until Lucene-38 is fixed, use this assert
            //Assert.AreEqual(3, hits.length(), "C added => A,B,<empty string>,C in range");
            searcher.Close();
        }
예제 #3
0
        public virtual void  TestPhraseQueryWithStopAnalyzer()
        {
            RAMDirectory directory    = new RAMDirectory();
            StopAnalyzer stopAnalyzer = new StopAnalyzer();
            IndexWriter  writer       = new IndexWriter(directory, stopAnalyzer, true);
            Document     doc          = new Document();

            doc.Add(Field.Text("Field", "the stop words are here"));
            writer.AddDocument(doc);
            writer.Close();

            IndexSearcher searcher = new IndexSearcher(directory);

            // valid exact phrase query
            PhraseQuery query = new PhraseQuery();

            query.Add(new Term("Field", "stop"));
            query.Add(new Term("Field", "words"));
            Hits hits = searcher.Search(query);

            Assert.AreEqual(1, hits.Length());

            // currently StopAnalyzer does not leave "holes", so this matches.
            query = new PhraseQuery();
            query.Add(new Term("Field", "words"));
            query.Add(new Term("Field", "here"));
            hits = searcher.Search(query);
            Assert.AreEqual(1, hits.Length());

            searcher.Close();
        }
예제 #4
0
        public virtual void  TestFarsi()
        {
            /* build an index */
            RAMDirectory farsiIndex = new RAMDirectory();
            IndexWriter  writer     = new IndexWriter(farsiIndex, new SimpleAnalyzer(), T, IndexWriter.MaxFieldLength.LIMITED);
            Document     doc        = new Document();

            doc.Add(new Field("content", "\u0633\u0627\u0628", Field.Store.YES, Field.Index.UN_TOKENIZED));
            doc.Add(new Field("body", "body", Field.Store.YES, Field.Index.UN_TOKENIZED));
            writer.AddDocument(doc);

            writer.Optimize();
            writer.Close();

            IndexReader   reader = IndexReader.Open(farsiIndex);
            IndexSearcher search = new IndexSearcher(reader);
            Query         q      = new TermQuery(new Term("body", "body"));

            // Neither Java 1.4.2 nor 1.5.0 has Farsi Locale collation available in
            // RuleBasedCollator.  However, the Arabic Locale seems to order the Farsi
            // characters properly.
            System.Globalization.CompareInfo collator = new System.Globalization.CultureInfo("ar").CompareInfo;

            // Unicode order would include U+0633 in [ U+062F - U+0698 ], but Farsi
            // orders the U+0698 character before the U+0633 character, so the single
            // index Term below should NOT be returned by a TermRangeFilter with a Farsi
            // Collator (or an Arabic one for the case when Farsi is not supported).
            Hits result = search.Search(q, new TermRangeFilter("content", "\u062F", "\u0698", T, T, collator));

            Assert.AreEqual(0, result.Length(), "The index Term should not be included.");

            result = search.Search(q, new TermRangeFilter("content", "\u0633", "\u0638", T, T, collator));
            Assert.AreEqual(1, result.Length(), "The index Term should be included.");
            search.Close();
        }
예제 #5
0
파일: CheckHits.cs 프로젝트: raj581/Marvin
        public static void  CheckHits_(Query query, System.String defaultFieldName, Searcher searcher, int[] results, TestCase testCase)
        {
            Hits hits = searcher.Search(query);

            System.Collections.Hashtable correct = new System.Collections.Hashtable();
            for (int i = 0; i < results.Length; i++)
            {
                correct.Add((System.Int32)results[i], null);
            }

            System.Collections.Hashtable actual = new System.Collections.Hashtable();
            for (int i = 0; i < hits.Length(); i++)
            {
                actual.Add((System.Int32)hits.Id(i), null);
            }

            //Assert.AreEqual(correct, actual, query.ToString(defaultFieldName));
            if (correct.Count != 0)
            {
                System.Collections.IDictionaryEnumerator iter = correct.GetEnumerator();
                bool status = false;
                while (iter.MoveNext())
                {
                    status = actual.ContainsKey(iter.Key);
                    if (status == false)
                    {
                        break;
                    }
                }
                Assert.IsTrue(status, query.ToString(defaultFieldName));
            }
        }
예제 #6
0
        public virtual void  TestTermVectors_()
        {
            Query query = new TermQuery(new Term("Field", "seventy"));

            try
            {
                Hits hits = searcher.Search(query);
                Assert.AreEqual(100, hits.Length());

                for (int i = 0; i < hits.Length(); i++)
                {
                    TermFreqVector[] vector = searcher.reader.GetTermFreqVectors(hits.Id(i));
                    Assert.IsTrue(vector != null);
                    Assert.IsTrue(vector.Length == 1);
                    //Assert.IsTrue();
                }
                TermFreqVector[] vector2 = searcher.reader.GetTermFreqVectors(hits.Id(50));
                //System.out.println("Explain: " + searcher.explain(query, hits.id(50)));
                //System.out.println("Vector: " + vector[0].toString());
            }
            catch (System.IO.IOException e)
            {
                Assert.IsTrue(false);
            }
        }
예제 #7
0
        public virtual void  TestPrefixQuery()
        {
            RAMDirectory directory = new RAMDirectory();

            System.String[] categories = new System.String[] { "/Computers", "/Computers/Mac", "/Computers/Windows" };
            IndexWriter     writer     = new IndexWriter(directory, new WhitespaceAnalyzer(), true);

            for (int i = 0; i < categories.Length; i++)
            {
                Document doc = new Document();
                doc.Add(Field.Keyword("category", categories[i]));
                writer.AddDocument(doc);
            }
            writer.Close();

            PrefixQuery   query    = new PrefixQuery(new Term("category", "/Computers"));
            IndexSearcher searcher = new IndexSearcher(directory);
            Hits          hits     = searcher.Search(query);

            Assert.AreEqual(3, hits.Length(), "All documents in /Computers category and below");

            query = new PrefixQuery(new Term("category", "/Computers/Mac"));
            hits  = searcher.Search(query);
            Assert.AreEqual(1, hits.Length(), "One in /Computers/Mac");
        }
예제 #8
0
        public List <IndexPageResult> GetDocumentPagesWithQuery(string query)
        {
            List <IndexPageResult> results = new List <IndexPageResult>();
            Dictionary <string, IndexPageResult> fingerprints_already_seen = new Dictionary <string, IndexPageResult>();

            try
            {
                using (IndexReader index_reader = IndexReader.Open(LIBRARY_INDEX_BASE_PATH, true))
                {
                    using (IndexSearcher index_searcher = new IndexSearcher(index_reader))
                    {
                        QueryParser query_parser = new QueryParser(Version.LUCENE_29, "content", analyzer);

                        Lucene.Net.Search.Query query_object = query_parser.Parse(query);
                        Lucene.Net.Search.Hits  hits         = index_searcher.Search(query_object);

                        var i = hits.Iterator();
                        while (i.MoveNext())
                        {
                            Lucene.Net.Search.Hit hit = (Lucene.Net.Search.Hit)i.Current;
                            string fingerprint        = hit.Get("fingerprint");
                            int    page  = Convert.ToInt32(hit.Get("page"));
                            double score = hit.GetScore();

                            // If this is the first time we have seen this fingerprint, make the top-level record
                            if (!fingerprints_already_seen.ContainsKey(fingerprint))
                            {
                                IndexPageResult result = new IndexPageResult();
                                result.fingerprint = fingerprint;
                                result.score       = score;

                                // Add to our structures
                                results.Add(result);
                                fingerprints_already_seen[fingerprint] = result;
                            }

                            // And add the page record
                            {
                                IndexPageResult result = fingerprints_already_seen[fingerprint];
                                result.page_results.Add(new PageResult {
                                    page = page, score = score
                                });
                            }
                        }

                        // Close the index
                        index_searcher.Close();
                    }
                    index_reader.Close();
                }
            }
            catch (Exception ex)
            {
                Logging.Warn(ex, $"GetDocumentPagesWithQuery: There was a problem opening the index file for searching (path: '{LIBRARY_INDEX_BASE_PATH}', query: '{query}')");
            }

            return(results);
        }
예제 #9
0
        public virtual void  TestRangeQuery()
        {
            RangeQuery rq = new RangeQuery(new Term("sorter", "b"), new Term("sorter", "d"), true);

            Query filteredquery = new FilteredQuery(rq, filter);
            Hits  hits          = searcher.Search(filteredquery);

            Assert.AreEqual(2, hits.Length());
        }
예제 #10
0
        public virtual void  TestBarelyCloseEnough()
        {
            query.SetSlop(3);
            query.Add(new Term("Field", "one"));
            query.Add(new Term("Field", "five"));
            Hits hits = searcher.Search(query);

            Assert.AreEqual(1, hits.Length());
        }
예제 #11
0
        public virtual void  TestQueryFilter()
        {
            // try to search the published index
            Lucene.Net.Search.Searchable[] searchables = new Lucene.Net.Search.Searchable[] { Remote };
            Searcher searcher = new MultiSearcher(searchables);
            Hits     hits     = searcher.Search(new TermQuery(new Term("test", "text")), new QueryFilter(new TermQuery(new Term("test", "test"))));
            Hits     nohits   = searcher.Search(new TermQuery(new Term("test", "text")), new QueryFilter(new TermQuery(new Term("test", "non-existent-term"))));

            Assert.AreEqual(0, nohits.Length());
        }
예제 #12
0
        private static void  Search(Query query)
        {
            // try to search the published index
            Lucene.Net.Search.Searchable[] searchables = new Lucene.Net.Search.Searchable[] { Remote };
            Searcher searcher = new MultiSearcher(searchables);
            Hits     result   = searcher.Search(query);

            Assert.AreEqual(1, result.Length());
            Assert.AreEqual("test text", result.Doc(0).Get("test"));
        }
예제 #13
0
 public void AddResult(Hits hits, int maxMatches)
 {
     if (docList == null)
         docList = new List<ExDocument>();
     if (hits == null)
         return;
     for (int i = 0; i < maxMatches && i < hits.Length(); i++)
     {
         docList.Add(new ExDocument(hits.Doc(i), hits.Score(i)));
     }
 }
예제 #14
0
		public static void  PrintDocNrs(Hits hits)
		{
			System.Console.Out.Write("new int[] {");
			for (int i = 0; i < hits.Length(); i++)
			{
				System.Console.Out.Write(hits.Id(i));
				if (i != hits.Length() - 1)
					System.Console.Out.Write(", ");
			}
			System.Console.Out.WriteLine("}");
		}
        private void  DoTestSearchHitsDeleteEvery(int k, bool deleteInFront)
        {
            bool intermittent = k < 0;

            Log("Test search hits with " + (intermittent?"intermittent deletions.":"deletions of every " + k + " hit."));
            IndexSearcher searcher = new IndexSearcher(directory);
            IndexReader   reader   = searcher.GetIndexReader();
            Query         q        = new TermQuery(new Term(TEXT_FIELD, "text")); // matching all docs
            Hits          hits     = searcher.Search(q);

            Log("Got " + hits.Length() + " results");
            Assert.AreEqual(N, hits.Length(), "must match all " + N + " docs, not only " + hits.Length() + " docs!");
            if (deleteInFront)
            {
                Log("deleting hits that was not yet retrieved!");
                reader.DeleteDocument(reader.MaxDoc() - 1);
                reader.DeleteDocument(reader.MaxDoc() - 2);
                reader.DeleteDocument(reader.MaxDoc() - 3);
            }
            try
            {
                for (int i = 0; i < hits.Length(); i++)
                {
                    int id = hits.Id(i);
                    Assert.AreEqual(i, hits.Id(i), "Hit " + i + " has doc id " + hits.Id(i) + " instead of " + i);
                    if ((intermittent && (i == 50 || i == 250 || i == 950)) || (!intermittent && (k < 2 || (i > 0 && i % k == 0))))
                    {
                        Document doc = hits.Doc(id);
                        Log("Deleting hit " + i + " - doc " + doc + " with id " + id);
                        reader.DeleteDocument(id);
                    }
                    if (intermittent)
                    {
                        // check internal behavior of Hits (go 50 ahead of getMoreDocs points because the deletions cause to use more of the available hits)
                        if (i == 150 || i == 450 || i == 1650)
                        {
                            Assert.IsTrue(hits.debugCheckedForDeletions, "Hit " + i + ": hits should have checked for deletions in last call to getMoreDocs()");
                        }
                        else if (i == 50 || i == 250 || i == 850)
                        {
                            Assert.IsFalse(hits.debugCheckedForDeletions, "Hit " + i + ": hits should have NOT checked for deletions in last call to getMoreDocs()");
                        }
                    }
                }
            }
            catch (System.Exception e)
            {
                // this is the only valid exception, and only when deletng in front.
                Assert.IsTrue(deleteInFront, e.Message + " not expected unless deleting hits that were not yet seen!");
            }
            searcher.Close();
        }
예제 #16
0
파일: CheckHits.cs 프로젝트: raj581/Marvin
 public static void  PrintDocNrs(Hits hits)
 {
     System.Console.Out.Write("new int[] {");
     for (int i = 0; i < hits.Length(); i++)
     {
         System.Console.Out.Write(hits.Id(i));
         if (i != hits.Length() - 1)
         {
             System.Console.Out.Write(", ");
         }
     }
     System.Console.Out.WriteLine("}");
 }
예제 #17
0
 public void TestDefaultFilter()
 {
     DuplicateFilter df = new DuplicateFilter(KEY_FIELD);
     Hashtable results = new Hashtable();
     Hits h = searcher.Search(tq, df);
     for (int i = 0; i < h.Length(); i++)
     {
         Document d = h.Doc(i);
         String url = d.Get(KEY_FIELD);
         Assert.IsFalse(results.Contains(url), "No duplicate urls should be returned");
         results.Add(url,url);
     }
 }
예제 #18
0
파일: TestSort.cs 프로젝트: raj581/Marvin
        private System.Collections.Hashtable GetScores(Hits hits)
        {
            System.Collections.Hashtable scoreMap = new System.Collections.Hashtable();
            int n = hits.Length();

            for (int i = 0; i < n; ++i)
            {
                Document        doc = hits.Doc(i);
                System.String[] v   = doc.GetValues("tracer");
                Assert.AreEqual(v.Length, 1);
                scoreMap[v[0]] = (float)hits.Score(i);
            }
            return(scoreMap);
        }
예제 #19
0
        public virtual void  TestExact()
        {
            // slop is zero by default
            query.Add(new Term("Field", "four"));
            query.Add(new Term("Field", "five"));
            Hits hits = searcher.Search(query);

            Assert.AreEqual(1, hits.Length(), "exact match");

            query = new PhraseQuery();
            query.Add(new Term("Field", "two"));
            query.Add(new Term("Field", "one"));
            hits = searcher.Search(query);
            Assert.AreEqual(0, hits.Length(), "reverse not exact");
        }
        public EntityInfo Extract(Hits hits, int index)
        {
            Document doc = hits.Doc(index);
            //TODO if we are lonly looking for score (unlikely), avoid accessing doc (lazy load)
            EntityInfo entityInfo = Extract(doc);
            object[] eip = entityInfo.Projection;

            if (eip != null && eip.Length > 0)
            {
                for (int x = 0; x < projection.Length; x++)
                {
                    switch (projection[x])
                    {
                        case ProjectionConstants.SCORE:
                            eip[x] = hits.Score(index);
                            break;

                        case ProjectionConstants.ID:
                            eip[x] = entityInfo.Id;
                            break;

                        case ProjectionConstants.DOCUMENT:
                            eip[x] = doc;
                            break;

                        case ProjectionConstants.DOCUMENT_ID:
                            eip[x] = hits.Id(index);
                            break;

                        case ProjectionConstants.BOOST:
                            eip[x] = doc.GetBoost();
                            break;

                        case ProjectionConstants.THIS:
                            //THIS could be projected more than once
                            //THIS loading delayed to the Loader phase
                            if (entityInfo.IndexesOfThis == null)
                            {
                                entityInfo.IndexesOfThis = new List<int>(1);
                            }
                            entityInfo.IndexesOfThis.Add(x);
                            break;
                    }
                }
            }

            return entityInfo;
        }
예제 #21
0
 public void TestNoFilter()
 {
     Hashtable results = new Hashtable();
     Hits h = searcher.Search(tq);
     Assert.IsTrue(h.Length() > 0, "Default searching should have found some matches");
     bool dupsFound = false;
     for (int i = 0; i < h.Length(); i++)
     {
         Document d = h.Doc(i);
         String url = d.Get(KEY_FIELD);
         if (!dupsFound)
             dupsFound = results.Contains(url);
         results[url]=url;
     }
     Assert.IsTrue(dupsFound, "Default searching should have found duplicate urls");
 }
예제 #22
0
 public void TestFastFilter()
 {
     DuplicateFilter df = new DuplicateFilter(KEY_FIELD);
     df.SetProcessingMode(DuplicateFilter.PM_FAST_INVALIDATION);
     Hashtable results = new Hashtable();
     Hits h = searcher.Search(tq, df);
     Assert.IsTrue(h.Length() > 0, "Filtered searching should have found some matches");
     for (int i = 0; i < h.Length(); i++)
     {
         Document d = h.Doc(i);
         String url = d.Get(KEY_FIELD);
         Assert.IsFalse(results.Contains(url), "No duplicate urls should be returned");
         results.Add(url,url);
     }
     Assert.AreEqual(2, results.Count, "Two urls found");
 }
예제 #23
0
        /***
         * Understands the lucene query syntax
         */
        public List <Utilities.Language.TextIndexing.IndexResult> GetDocumentsWithQuery(string query)
        {
            List <Utilities.Language.TextIndexing.IndexResult> fingerprints = new List <Utilities.Language.TextIndexing.IndexResult>();
            HashSet <string> fingerprints_already_seen = new HashSet <string>();

            try
            {
                using (Lucene.Net.Index.IndexReader index_reader = Lucene.Net.Index.IndexReader.Open(LIBRARY_INDEX_BASE_PATH, true))
                {
                    using (Lucene.Net.Search.IndexSearcher index_searcher = new Lucene.Net.Search.IndexSearcher(index_reader))
                    {
                        Lucene.Net.QueryParsers.QueryParser query_parser = new Lucene.Net.QueryParsers.QueryParser(Version.LUCENE_29, "content", analyzer);

                        Lucene.Net.Search.Query query_object = query_parser.Parse(query);
                        Lucene.Net.Search.Hits  hits         = index_searcher.Search(query_object);

                        var i = hits.Iterator();
                        while (i.MoveNext())
                        {
                            Lucene.Net.Search.Hit hit = (Lucene.Net.Search.Hit)i.Current;
                            string fingerprint        = hit.Get("fingerprint");
                            string page = hit.Get("page");

                            if (!fingerprints_already_seen.Contains(fingerprint))
                            {
                                fingerprints_already_seen.Add(fingerprint);

                                IndexResult index_result = new IndexResult {
                                    fingerprint = fingerprint, score = hit.GetScore()
                                };
                                fingerprints.Add(index_result);
                            }
                        }

                        // Close the index
                        index_searcher.Close();
                    }
                    index_reader.Close();
                }
            }
            catch (Exception ex)
            {
                Logging.Warn(ex, "GetDocumentsWithQuery: There was a problem opening the index file for searching.");
            }

            return(fingerprints);
        }
예제 #24
0
        public HashSet <string> GetDocumentsWithWord(string keyword)
        {
            HashSet <string> fingerprints = new HashSet <string>();

            try
            {
                keyword = ReasonableWord.MakeReasonableWord(keyword);
                if (null != keyword)
                {
                    ////Do a quick check for whether there are actually any segments files, otherwise we throw many exceptions in the IndexReader.Open in a very tight loop.
                    ////Added by Nik to cope with some exception...will uncomment this when i know what the problem is...
                    //var segments_files = Directory.GetFiles(LIBRARY_INDEX_BASE_PATH, "segments*", SearchOption.AllDirectories);
                    //if (segments_files.Length <= 0)
                    //{
                    //    Logging.Debug("No index segments files found");
                    //    return fingerprints;
                    //}

                    using (IndexReader index_reader = IndexReader.Open(LIBRARY_INDEX_BASE_PATH, true))
                    {
                        using (IndexSearcher index_searcher = new IndexSearcher(index_reader))
                        {
                            Lucene.Net.Search.TermQuery term_query = new Lucene.Net.Search.TermQuery(new Term("content", keyword));
                            Lucene.Net.Search.Hits      hits       = index_searcher.Search(term_query);

                            var i = hits.Iterator();
                            while (i.MoveNext())
                            {
                                Lucene.Net.Search.Hit hit = (Lucene.Net.Search.Hit)i.Current;
                                string fingerprint        = hit.Get("fingerprint");
                                fingerprints.Add(fingerprint);
                            }

                            // Close the index
                            index_searcher.Close();
                        }
                        index_reader.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.Warn(ex, $"GetDocumentsWithWord: There was a problem opening the index file for searching (path: '{LIBRARY_INDEX_BASE_PATH}', keyword: '{keyword}')");
            }

            return(fingerprints);
        }
예제 #25
0
 public void TestKeepsFirstFilter()
 {
     DuplicateFilter df = new DuplicateFilter(KEY_FIELD);
     df.SetKeepMode(DuplicateFilter.KM_USE_FIRST_OCCURRENCE);
     Hits h = searcher.Search(tq, df);
     Assert.IsTrue(h.Length() > 0, "Filtered searching should have found some matches");
     for (int i = 0; i < h.Length(); i++)
     {
         Document d = h.Doc(i);
         String url = d.Get(KEY_FIELD);
         TermDocs td = reader.TermDocs(new Term(KEY_FIELD, url));
         int lastDoc = 0;
         td.Next();
         lastDoc = td.Doc();
         Assert.AreEqual(lastDoc, h.Id((i)), "Duplicate urls should return first doc");
     }
 }
예제 #26
0
        public virtual void  TestOrderDoesntMatter()
        {
            SetUp();

            query.SetSlop(2);             // must be at least two for reverse order match
            query.Add(new Term("Field", "two"));
            query.Add(new Term("Field", "one"));
            Hits hits = searcher.Search(query);

            Assert.AreEqual(1, hits.Length(), "just sloppy enough");

            query = new PhraseQuery();
            query.SetSlop(2);
            query.Add(new Term("Field", "three"));
            query.Add(new Term("Field", "one"));
            hits = searcher.Search(query);
            Assert.AreEqual(0, hits.Length(), "not sloppy enough");
        }
예제 #27
0
파일: TestSort.cs 프로젝트: raj581/Marvin
        // make sure the documents returned by the search match the expected list
        private void  AssertMatches(Searcher searcher, Query query, Sort sort, System.String expectedResult)
        {
            Hits result = searcher.Search(query, sort);

            System.Text.StringBuilder buff = new System.Text.StringBuilder(10);
            int n = result.Length();

            for (int i = 0; i < n; ++i)
            {
                Document        doc = result.Doc(i);
                System.String[] v   = doc.GetValues("tracer");
                for (int j = 0; j < v.Length; ++j)
                {
                    buff.Append(v[j]);
                }
            }
            Assert.AreEqual(expectedResult, buff.ToString());
        }
예제 #28
0
파일: TestNot.cs 프로젝트: yonder/mono
        public virtual void  TestNot_()
        {
            RAMDirectory store  = new RAMDirectory();
            IndexWriter  writer = new IndexWriter(store, new SimpleAnalyzer(), true);

            Document d1 = new Document();

            d1.Add(Field.Text("Field", "a b"));

            writer.AddDocument(d1);
            writer.Optimize();
            writer.Close();

            Searcher searcher = new IndexSearcher(store);
            Query    query    = Lucene.Net.QueryParsers.QueryParser.Parse("a NOT b", "Field", new SimpleAnalyzer());
            //System.out.println(query);
            Hits hits = searcher.Search(query);

            Assert.AreEqual(0, hits.Length());
        }
예제 #29
0
파일: TestSort.cs 프로젝트: raj581/Marvin
        // make sure the documents returned by the search match the expected list pattern
        private void  AssertMatchesPattern(Searcher searcher, Query query, Sort sort, System.String pattern)
        {
            Hits result = searcher.Search(query, sort);

            System.Text.StringBuilder buff = new System.Text.StringBuilder(10);
            int n = result.Length();

            for (int i = 0; i < n; ++i)
            {
                Document        doc = result.Doc(i);
                System.String[] v   = doc.GetValues("tracer");
                for (int j = 0; j < v.Length; ++j)
                {
                    buff.Append(v[j]);
                }
            }
            // System.out.println ("matching \""+buff+"\" against pattern \""+pattern+"\"");
            System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(pattern);
            Assert.IsTrue(regex.IsMatch(buff.ToString()));
        }
예제 #30
0
        public virtual void  TestSlop1()
        {
            SetUp();

            // Ensures slop of 1 works with terms in order.
            query.SetSlop(1);
            query.Add(new Term("Field", "one"));
            query.Add(new Term("Field", "two"));
            Hits hits = searcher.Search(query);

            Assert.AreEqual(1, hits.Length(), "in order");

            // Ensures slop of 1 does not work for phrases out of order;
            // must be at least 2.
            query = new PhraseQuery();
            query.SetSlop(1);
            query.Add(new Term("Field", "two"));
            query.Add(new Term("Field", "one"));
            hits = searcher.Search(query);
            Assert.AreEqual(0, hits.Length(), "reversed, slop not 2 or more");
        }
예제 #31
0
        public virtual void  TestTermPositionVectors()
        {
            Query query = new TermQuery(new Term("Field", "fifty"));

            try
            {
                Hits hits = searcher.Search(query);
                Assert.AreEqual(100, hits.Length());

                for (int i = 0; i < hits.Length(); i++)
                {
                    TermFreqVector[] vector = searcher.reader.GetTermFreqVectors(hits.Id(i));
                    Assert.IsTrue(vector != null);
                    Assert.IsTrue(vector.Length == 1);
                    //Assert.IsTrue();
                }
            }
            catch (System.IO.IOException e)
            {
                Assert.IsTrue(false);
            }
        }
예제 #32
0
파일: Hit.cs 프로젝트: mrkurt/mubble-old
        internal static Hit FromRaw(Hits raw, int index)
        {
            var doc = raw.Doc(index);
            var fields = new FieldCollection();

            foreach (var f in doc.GetFields())
            {
                var rf = (Lucene.Net.Documents.Field)f;
                fields.Add(new Field
                {
                    Name = rf.Name(),
                    Values = { rf.StringValue() }
                });
            }

            return new Hit
            {
                Score = raw.Score(index),
                SchemaName = doc.GetValues(IndexHelper.SchemaNameField).Join(""),
                SchemaVersion = doc.GetValues(IndexHelper.SchemaVersionField).Join(""),
                Fields = fields
            };
        }
예제 #33
0
        public virtual void  TestInclusive()
        {
            Query query = new RangeQuery(new Term("content", "A"), new Term("content", "C"), true);

            InitializeIndex(new System.String[] { "A", "B", "C", "D" });
            IndexSearcher searcher = new IndexSearcher(dir);
            Hits          hits     = searcher.Search(query);

            Assert.AreEqual(3, hits.Length(), "A,B,C,D - A,B,C in range");
            searcher.Close();

            InitializeIndex(new System.String[] { "A", "B", "D" });
            searcher = new IndexSearcher(dir);
            hits     = searcher.Search(query);
            Assert.AreEqual(2, hits.Length(), "A,B,D - A and B in range");
            searcher.Close();

            AddDoc("C");
            searcher = new IndexSearcher(dir);
            hits     = searcher.Search(query);
            Assert.AreEqual(3, hits.Length(), "C added - A, B, C in range");
            searcher.Close();
        }
예제 #34
0
        public virtual void  TestMulipleTerms()
        {
            SetUp();

            query.SetSlop(2);
            query.Add(new Term("Field", "one"));
            query.Add(new Term("Field", "three"));
            query.Add(new Term("Field", "five"));
            Hits hits = searcher.Search(query);

            Assert.AreEqual(1, hits.Length(), "two total moves");

            query = new PhraseQuery();
            query.SetSlop(5);             // it takes six moves to match this phrase
            query.Add(new Term("Field", "five"));
            query.Add(new Term("Field", "three"));
            query.Add(new Term("Field", "one"));
            hits = searcher.Search(query);
            Assert.AreEqual(0, hits.Length(), "slop of 5 not close enough");

            query.SetSlop(6);
            hits = searcher.Search(query);
            Assert.AreEqual(1, hits.Length(), "slop of 6 just right");
        }
예제 #35
0
 //----------------------------------------------------------------------------------
 /// <summary>
 /// Initializes a new instance of the <see cref="SearchResultCollection"/> class.
 /// </summary>
 /// <param name="hits">The hits.</param>
 /// <param name="timeElapsed">The time elapsed.</param>
 /// <remark>Created 02/02/2009 15:15 by jm</remark>
 //----------------------------------------------------------------------------------
 public SearchResultCollection(Hits hits, double timeElapsed)
 {
     SearchResultHits    = hits;
     TimeElapsed         = timeElapsed;
 }
        public Hits Search(Query query, int maxDocPerGroup = DefaultMaxDocPerGroup)
        {
            List<HitsPerFacet> hitsPerGroup = new List<HitsPerFacet>();

            DocIdSet queryDocidSet = new CachingWrapperFilter(new QueryWrapperFilter(query)).GetDocIdSet(_Reader);
            Action[] actions = new Action[_Groups.Count];           
            for (int i = 0; i < _Groups.Count; i++)
            {
                HitsPerFacet h = new HitsPerFacet(new FacetName(_Groups[i].Key.ToArray()), _Reader, queryDocidSet, _Groups[i].Value, maxDocPerGroup);
                hitsPerGroup.Add(h);
                actions[i] = () => h.Calculate();
            }
            
            Parallel.Invoke(actions);
            
            Hits hits = new Hits();
            hits.HitsPerFacet = hitsPerGroup.ToArray();

            return hits;
        }
예제 #37
0
 public PoiResult(Hits hits)
 {
     this.hits = hits;
 }
예제 #38
0
 public void AddResult(SearchInfo info, Hits hits, int maxMatches)
 {
     if ( docs == null)
         docs = new Dictionary<SearchInfo, List<ExDocument>>();
     if (hits == null)
         return;
     if (info == null)
         return;
     List<ExDocument> exdl = new List<ExDocument>();
     for (int i = 0; i < maxMatches && i < hits.Length(); i++)
     {
         exdl.Add(new ExDocument(hits.Doc(i),hits.Score(i)));
     }
     if ( exdl.Count > 0)
        docs.Add(info, exdl);
 }
예제 #39
0
파일: Searcher.cs 프로젝트: usmanghani/Misc
        private SearchResult[] _prepareSearchResultsFromHits(string query, Hits hits)
        {
            List<SearchResult> results = new List<SearchResult>();
            for (int i = 0; i < hits.Length(); i++)
            {
                SearchResult sr = new SearchResult(hits.Doc(i), _analyzer, query, _fragmentSize);
                sr.QueryParser = _parser;
                results.Add(sr);

            }
            return results.ToArray();
        }
예제 #40
0
파일: Form1.cs 프로젝트: usmanghani/Misc
        private void btnSearch_Click(object sender, EventArgs e)
        {
            lstResults.Items.Clear();
            searcher = new IndexSearcher(new RAMDirectory(_indexTarget));
            PerFieldAnalyzerWrapper analyzer = new PerFieldAnalyzerWrapper(new StandardAnalyzer());
            analyzer.AddAnalyzer("ayat_arabic", new DiacriticAnalyzer(FilterData.stopWords));
            //MyQueryParser parser = new MyQueryParser(new string[] { "ayat_desc", "ayat_urdu", "ayat_arabic" }, analyzer);
            //parser.SetDefaultOperator(QueryParser.Operator.AND);
            //Query q = parser.Parse(txtSearch.Text);
            //Query q = new TermQuery(new Term("ayatno", NumberTools.LongToString(long.Parse(txtSearch.Text))));
            BooleanQuery q = new BooleanQuery();
            long l1 = 1; long l2 = 500; long l3 = 1; long l4 = 1;
            //RangeQuery rq = new RangeQuery(new Term("ayatno", l1.ToString("00000")), new Term("ayatno", l2.ToString("00000")), true);
            //q.Add(rq, true, false);
            q.Add(new TermQuery(new Term("sid", l3.ToString("00000"))), true, false);
            q.Add(new TermQuery(new Term("ayatno", l4.ToString("00000"))), true, false);
            MessageBox.Show(q.ToString());
            Sort sort = new Sort(new string[] { "pid", "sid", "ayatno" });
            hits = searcher.Search(q, sort);
            lblHits.Text = hits.Length() + " hit(s).";
            Application.DoEvents();

            for (int i = 0; i < hits.Length(); i++)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("Para: ").Append(hits.Doc(i).Get("pid"));
                sb.Append(", Surat: ").Append(hits.Doc(i).Get("sid"));
                sb.Append(", Verse: ").Append(hits.Doc(i).Get("ayatno"));
                lstResults.Items.Add(sb.ToString());

            }
        }
예제 #41
0
파일: MainForm.cs 프로젝트: usmanghani/Misc
        private void button2_Click(object sender, EventArgs e)
        {
            if (_indexTarget == string.Empty)
                return;

            if (textBox4.Text == string.Empty)
            {
                return;
            }
            listBox4.Items.Clear();
            if (IndexReader.IndexExists(_indexTarget))
            {
                searcher = new IndexSearcher(_indexTarget);
                Query q = QueryParser.Parse(textBox4.Text, "contents", analyzer);
                hits = searcher.Search(q);

                if (hits.Length() == 0)
                {
                    label8.Text = "No Hits :P";
                    return;
                }
                label8.Text = hits.Length() + " hit(s).";
                for (int i = 0; i < hits.Length(); i++)
                {
                    Document d = hits.Doc(i);
                    listBox4.Items.Add(d.Get("filename"));

                }

            }
        }
예제 #42
0
		/// <summary>Tests that a Hits has an expected order of documents </summary>
		public static void  CheckDocIds(System.String mes, int[] results, Hits hits)
		{
			Assert.AreEqual(results.Length, hits.Length(), mes + " nr of hits");
			for (int i = 0; i < results.Length; i++)
			{
				Assert.AreEqual(results[i], hits.Id(i), mes + " doc nrs for hit " + i);
			}
		}
예제 #43
0
		/// <summary> Constructed from {@link HitIterator}</summary>
		/// <param name="hits">Hits returned from a search
		/// </param>
		/// <param name="hitNumber">Hit index in Hits
		/// </param>
		internal Hit(Hits hits, int hitNumber)
		{
			this.hits = hits;
			this.hitNumber = hitNumber;
		}
예제 #44
0
		public static System.String Hits2str(Hits hits1, Hits hits2, int start, int end)
		{
			System.Text.StringBuilder sb = new System.Text.StringBuilder();
			int len1 = hits1 == null?0:hits1.Length();
			int len2 = hits2 == null?0:hits2.Length();
			if (end <= 0)
			{
				end = System.Math.Max(len1, len2);
			}
			
			sb.Append("Hits length1=").Append(len1).Append("\tlength2=").Append(len2);
			
			sb.Append('\n');
			for (int i = start; i < end; i++)
			{
				sb.Append("hit=").Append(i).Append(':');
				if (i < len1)
				{
					sb.Append(" doc").Append(hits1.Id(i)).Append('=').Append(hits1.Score(i));
				}
				else
				{
					sb.Append("               ");
				}
				sb.Append(",\t");
				if (i < len2)
				{
					sb.Append(" doc").Append(hits2.Id(i)).Append('=').Append(hits2.Score(i));
				}
				sb.Append('\n');
			}
			return sb.ToString();
		}
		/// <summary> Check the hits for duplicates.</summary>
		/// <param name="hits">
		/// </param>
		private void  CheckHits(Hits hits, System.String prefix)
		{
			if (hits != null)
			{
				System.Collections.IDictionary idMap = new System.Collections.SortedList();
				for (int docnum = 0; docnum < hits.Length(); ++docnum)
				{
					System.Int32 luceneId;
					try
					{
						luceneId = (System.Int32) hits.Id(docnum);
						if (idMap.Contains(luceneId))
						{
							System.Text.StringBuilder message = new System.Text.StringBuilder(prefix);
							message.Append("Duplicate key for hit index = ");
							message.Append(docnum);
							message.Append(", previous index = ");
							message.Append(((System.Int32) idMap[luceneId]).ToString());
							message.Append(", Lucene ID = ");
							message.Append(luceneId);
							Log(message.ToString());
						}
						else
						{
							idMap[luceneId] = (System.Int32) docnum;
						}
					}
					catch (System.IO.IOException ioe)
					{
						System.Text.StringBuilder message = new System.Text.StringBuilder(prefix);
						message.Append("Error occurred for hit index = ");
						message.Append(docnum);
						message.Append(" (");
						message.Append(ioe.Message);
						message.Append(")");
						Log(message.ToString());
					}
				}
			}
		}
예제 #46
0
파일: Form1.cs 프로젝트: usmanghani/Misc
        private void button2_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            searcher = new IndexSearcher(new RAMDirectory(indexpath));
            Query q = MultiFieldQueryParser.Parse(textBox1.Text, new string[] { "ayat_desc", "ayat_urdu", "ayat_arabic" }, new DiacriticAnalyzer(FilterData.stopWords));
            //Query q = QueryParser.Parse(textBox1.Text, "contents", new DiacriticAnalyzer(FilterData.stopWords));
            //Query q = QueryParser.Parse(textBox1.Text, "ayat_desc", new DiacriticAnalyzer(FilterData.stopWords));
            hits = searcher.Search(q);
            label2.Text = string.Format("{0} hit(s).", hits.Length().ToString());
            Application.DoEvents();

            //for (int i = 0; i < hits.Length(); i++)
            //{
            //    foreach (Field f in hits.Doc(i).Fields())
            //    {
            //        listBox1.Items.Add(f.Name());

            //    }
            //}

            for (int i = 0; i < hits.Length(); i++)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("Para: ").Append(hits.Doc(i).Get("pid"));
                sb.Append(", Surat: ").Append(hits.Doc(i).Get("sid"));
                sb.Append(", Verse: ").Append(hits.Doc(i).Get("ayatno"));
                listBox1.Items.Add(sb.ToString());

            }
        }
		private static void  PrintHits(Hits hits)
		{
			System.Console.Out.WriteLine(hits.Length() + " total results\n");
			for (int i = 0; i < hits.Length(); i++)
			{
				if (i < 10 || (i > 94 && i < 105))
				{
					Lucene.Net.Documents.Document d = hits.Doc(i);
					System.Console.Out.WriteLine(i + " " + d.Get(ID_FIELD));
				}
			}
		}
예제 #48
0
        public Hits Search(Query query, int maxDocPerGroup)
        {
            var hitsPerGroup = new List<HitsPerFacet>();

            DocIdSet queryDocidSet = new CachingWrapperFilter(new QueryWrapperFilter(query)).GetDocIdSet(_Reader);
            var actions = new Action[_Groups.Count];           
            for (int i = 0; i < _Groups.Count; i++)
            {
                var h = new HitsPerFacet(new FacetName(_Groups[i].Key.ToArray()), _Reader, queryDocidSet, _Groups[i].Value, maxDocPerGroup);
                hitsPerGroup.Add(h);
                actions[i] = h.Calculate;
            }
            
#if !NET35
            Parallel.Invoke(actions);
#else
            foreach (var action in actions)
                action();
#endif
            
            Hits hits = new Hits {HitsPerFacet = hitsPerGroup.ToArray()};

            return hits;
        }
예제 #49
0
		private void  CheckHits(Hits hits, int expectedCount)
		{
			Assert.AreEqual(expectedCount, hits.Length(), "total results");
			for (int i = 0; i < hits.Length(); i++)
			{
				if (i < 10 || (i > 94 && i < 105))
				{
					Document d = hits.Doc(i);
					Assert.AreEqual(System.Convert.ToString(i), d.Get(ID_FIELD), "check " + i);
				}
			}
		}
예제 #50
0
파일: TestSort.cs 프로젝트: emtees/old-code
		private System.Collections.Hashtable GetScores(Hits hits)
		{
			System.Collections.Hashtable scoreMap = new System.Collections.Hashtable();
			int n = hits.Length();
			for (int i = 0; i < n; ++i)
			{
				Document doc = hits.Doc(i);
				System.String[] v = doc.GetValues("tracer");
				Assert.AreEqual(v.Length, 1);
				scoreMap[v[0]] = (float) hits.Score(i);
			}
			return scoreMap;
		}
예제 #51
0
		private void  PrintHits(System.IO.StringWriter out_Renamed, Hits hits)
		{
			out_Renamed.WriteLine(hits.Length() + " total results\n");
			for (int i = 0; i < hits.Length(); i++)
			{
				if (i < 10 || (i > 94 && i < 105))
				{
					Document d = hits.Doc(i);
					out_Renamed.WriteLine(i + " " + d.Get(ID_FIELD));
				}
			}
		}
예제 #52
0
		/// <summary>Tests that two queries have an expected order of documents,
		/// and that the two queries have the same score values.
		/// </summary>
		public static void  CheckHitsQuery(Query query, Hits hits1, Hits hits2, int[] results)
		{
			
			CheckDocIds("hits1", results, hits1);
			CheckDocIds("hits2", results, hits2);
			CheckEqual(query, hits1, hits2);
		}
		protected internal virtual void  PrintHits(System.String test, Hits h)
		{
			
			System.Console.Error.WriteLine("------- " + test + " -------");
			
			for (int i = 0; i < h.Length(); i++)
			{
				Lucene.Net.Documents.Document d = h.Doc(i);
				float score = h.Score(i);
				System.Console.Error.WriteLine("#" + i + ": {0.000000000}" + score + " - " + d.Get("id"));
			}
		}
예제 #54
0
파일: HitIterator.cs 프로젝트: sinsay/SSE
 /// <summary> Constructed from <see cref="Hits.Iterator()" />.</summary>
 internal HitIterator(Hits hits)
 {
     this.hits = hits;
 }
예제 #55
0
		public static void  CheckEqual(Query query, Hits hits1, Hits hits2)
		{
			float scoreTolerance = 1.0e-6f;
			if (hits1.Length() != hits2.Length())
			{
				Assert.Fail("Unequal lengths: hits1=" + hits1.Length() + ",hits2=" + hits2.Length());
			}
			for (int i = 0; i < hits1.Length(); i++)
			{
				if (hits1.Id(i) != hits2.Id(i))
				{
					Assert.Fail("Hit " + i + " docnumbers don't match\n" + Hits2str(hits1, hits2, 0, 0) + "for query:" + query.ToString());
				}
				
				if ((hits1.Id(i) != hits2.Id(i)) || System.Math.Abs(hits1.Score(i) - hits2.Score(i)) > scoreTolerance)
				{
					Assert.Fail("Hit " + i + ", doc nrs " + hits1.Id(i) + " and " + hits2.Id(i) + "\nunequal       : " + hits1.Score(i) + "\n           and: " + hits2.Score(i) + "\nfor query:" + query.ToString());
				}
			}
		}