示例#1
0
 public virtual Query GetQueryDOA(System.String query, Analyzer a)
 {
     if (a == null)
     {
         a = new SimpleAnalyzer();
     }
     QueryParsers.QueryParser qp = new QueryParsers.QueryParser("Field", a);
     qp.SetOperator(QueryParsers.QueryParser.DEFAULT_OPERATOR_AND);
     return(qp.Parse(query));
 }
示例#2
0
 public virtual QueryParsers.QueryParser GetParser(Analyzer a)
 {
     if (a == null)
     {
         a = new SimpleAnalyzer();
     }
     QueryParsers.QueryParser qp = new QueryParsers.QueryParser("Field", a);
     qp.SetOperator(QueryParsers.QueryParser.DEFAULT_OPERATOR_OR);
     return(qp);
 }
示例#3
0
        public virtual void  AssertWildcardQueryEquals(System.String query, bool lowercase, System.String result)
        {
            QueryParsers.QueryParser qp = GetParser(null);
            qp.SetLowercaseWildcardTerms(lowercase);
            Query q = qp.Parse(query);

            System.String s = q.ToString("Field");
            if (!s.Equals(result))
            {
                Assert.Fail("WildcardQuery /" + query + "/ yielded /" + s + "/, expecting /" + result + "/");
            }
        }
示例#4
0
        public static void  Main(System.String[] args)
        {
            try
            {
                Directory   directory = new RAMDirectory();
                Analyzer    analyzer  = new SimpleAnalyzer();
                IndexWriter writer    = new IndexWriter(directory, analyzer, true);

                int MAX_DOCS = 225;

                for (int j = 0; j < MAX_DOCS; j++)
                {
                    Document d = new Document();
                    d.Add(Field.Text(PRIORITY_FIELD, HIGH_PRIORITY));
                    d.Add(Field.Text(ID_FIELD, System.Convert.ToString(j)));
                    writer.AddDocument(d);
                }
                writer.Close();

                // try a search without OR
                Searcher searcher = new IndexSearcher(directory);
                Hits     hits     = null;

                QueryParsers.QueryParser parser = new QueryParsers.QueryParser(PRIORITY_FIELD, analyzer);

                Query query = parser.Parse(HIGH_PRIORITY);
                System.Console.Out.WriteLine("Query: " + query.ToString(PRIORITY_FIELD));

                hits = searcher.Search(query);
                PrintHits(hits);

                searcher.Close();

                // try a new search with OR
                searcher = new IndexSearcher(directory);
                hits     = null;

                parser = new QueryParsers.QueryParser(PRIORITY_FIELD, analyzer);

                query = parser.Parse(HIGH_PRIORITY + " OR " + MED_PRIORITY);
                System.Console.Out.WriteLine("Query: " + query.ToString(PRIORITY_FIELD));

                hits = searcher.Search(query);
                PrintHits(hits);

                searcher.Close();
            }
            catch (System.Exception e)
            {
                System.Console.Out.WriteLine(" caught a " + e.GetType() + "\n with message: " + e.Message);
            }
        }
示例#5
0
文件: SearchTest.cs 项目: yonder/mono
        public static void  Main(System.String[] args)
        {
            try
            {
                Directory   directory = new RAMDirectory();
                Analyzer    analyzer  = new SimpleAnalyzer();
                IndexWriter writer    = new IndexWriter(directory, analyzer, true);

                System.String[] docs = new System.String[] { "a b c d e", "a b c d e a b c d e", "a b c d e f g h i j", "a c e", "e c a", "a c e a c e", "a c e a b c" };
                for (int j = 0; j < docs.Length; j++)
                {
                    Document d = new Document();
                    d.Add(Field.Text("contents", docs[j]));
                    writer.AddDocument(d);
                }
                writer.Close();

                Searcher searcher = new IndexSearcher(directory);

                System.String[] queries = new System.String[] { "\"a c e\"" };
                Hits            hits    = null;

                QueryParsers.QueryParser parser = new QueryParsers.QueryParser("contents", analyzer);
                parser.SetPhraseSlop(4);
                for (int j = 0; j < queries.Length; j++)
                {
                    Query query = parser.Parse(queries[j]);
                    System.Console.Out.WriteLine("Query: " + query.ToString("contents"));

                    //DateFilter filter =
                    //  new DateFilter("modified", Time(1997,0,1), Time(1998,0,1));
                    //DateFilter filter = DateFilter.Before("modified", Time(1997,00,01));
                    //System.out.println(filter);

                    hits = searcher.Search(query);

                    System.Console.Out.WriteLine(hits.Length() + " total results");
                    for (int i = 0; i < hits.Length() && i < 10; i++)
                    {
                        Document d = hits.Doc(i);
                        System.Console.Out.WriteLine(i + " " + hits.Score(i) + " " + d.Get("contents"));
                    }
                }
                searcher.Close();
            }
            catch (System.Exception e)
            {
                System.Console.Out.WriteLine(" caught a " + e.GetType() + "\n with message: " + e.Message);
            }
        }
示例#6
0
        private void  DoTest(System.IO.StringWriter out_Renamed, bool useCompoundFiles)
        {
            Directory   directory = new RAMDirectory();
            Analyzer    analyzer  = new SimpleAnalyzer();
            IndexWriter writer    = new IndexWriter(directory, analyzer, true);

            writer.SetUseCompoundFile(useCompoundFiles);

            int MAX_DOCS = 225;

            for (int j = 0; j < MAX_DOCS; j++)
            {
                Document d = new Document();
                d.Add(Field.Text(PRIORITY_FIELD, HIGH_PRIORITY));
                d.Add(Field.Text(ID_FIELD, System.Convert.ToString(j)));
                writer.AddDocument(d);
            }
            writer.Close();

            // try a search without OR
            Searcher searcher = new IndexSearcher(directory);
            Hits     hits     = null;

            QueryParsers.QueryParser parser = new QueryParsers.QueryParser(PRIORITY_FIELD, analyzer);

            Query query = parser.Parse(HIGH_PRIORITY);

            out_Renamed.WriteLine("Query: " + query.ToString(PRIORITY_FIELD));

            hits = searcher.Search(query);
            PrintHits(out_Renamed, hits);
            CheckHits(hits, MAX_DOCS);

            searcher.Close();

            // try a new search with OR
            searcher = new IndexSearcher(directory);
            hits     = null;

            parser = new QueryParsers.QueryParser(PRIORITY_FIELD, analyzer);

            query = parser.Parse(HIGH_PRIORITY + " OR " + MED_PRIORITY);
            out_Renamed.WriteLine("Query: " + query.ToString(PRIORITY_FIELD));

            hits = searcher.Search(query);
            PrintHits(out_Renamed, hits);
            CheckHits(hits, MAX_DOCS);

            searcher.Close();
        }
示例#7
0
        private void  DoTestSearch(System.IO.StringWriter out_Renamed, bool useCompoundFile)
        {
            Directory   directory = new RAMDirectory();
            Analyzer    analyzer  = new SimpleAnalyzer();
            IndexWriter writer    = new IndexWriter(directory, analyzer, true);

            writer.SetUseCompoundFile(useCompoundFile);

            System.String[] docs = new System.String[] { "a b c d e", "a b c d e a b c d e", "a b c d e f g h i j", "a c e", "e c a", "a c e a c e", "a c e a b c" };
            for (int j = 0; j < docs.Length; j++)
            {
                Document d = new Document();
                d.Add(Field.Text("contents", docs[j]));
                writer.AddDocument(d);
            }
            writer.Close();

            Searcher searcher = new IndexSearcher(directory);

            System.String[] queries = new System.String[] { "a b", "\"a b\"", "\"a b c\"", "a c", "\"a c\"", "\"a c e\"" };
            Hits            hits    = null;

            QueryParsers.QueryParser parser = new QueryParsers.QueryParser("contents", analyzer);
            parser.SetPhraseSlop(4);
            for (int j = 0; j < queries.Length; j++)
            {
                Query query = parser.Parse(queries[j]);
                out_Renamed.WriteLine("Query: " + query.ToString("contents"));

                //DateFilter filter =
                //  new DateFilter("modified", Time(1997,0,1), Time(1998,0,1));
                //DateFilter filter = DateFilter.Before("modified", Time(1997,00,01));
                //System.out.println(filter);

                hits = searcher.Search(query);

                out_Renamed.WriteLine(hits.Length() + " total results");
                for (int i = 0; i < hits.Length() && i < 10; i++)
                {
                    Document d = hits.Doc(i);
                    out_Renamed.WriteLine(i + " " + hits.Score(i) + " " + d.Get("contents"));
                }
            }
            searcher.Close();
        }
示例#8
0
        public virtual void  TestBoost()
        {
            StandardAnalyzer oneStopAnalyzer = new StandardAnalyzer(new System.String[] { "on" });

            QueryParsers.QueryParser qp = new QueryParsers.QueryParser("Field", oneStopAnalyzer);
            Query q = qp.Parse("on^1.0");

            Assert.IsNotNull(q);
            q = qp.Parse("\"hello\"^2.0");
            Assert.IsNotNull(q);
            Assert.AreEqual(q.GetBoost(), (float)2.0, (float)0.5);
            q = qp.Parse("hello^2.0");
            Assert.IsNotNull(q);
            Assert.AreEqual(q.GetBoost(), (float)2.0, (float)0.5);
            q = qp.Parse("\"on\"^1.0");
            Assert.IsNotNull(q);

            q = QueryParsers.QueryParser.Parse("the^3", "Field", new StandardAnalyzer());
            Assert.IsNotNull(q);
        }
示例#9
0
		public virtual void  TestBoost()
		{
			StandardAnalyzer oneStopAnalyzer = new StandardAnalyzer(new System.String[]{"on"});
			QueryParsers.QueryParser qp = new QueryParsers.QueryParser("Field", oneStopAnalyzer);
			Query q = qp.Parse("on^1.0");
			Assert.IsNotNull(q);
			q = qp.Parse("\"hello\"^2.0");
			Assert.IsNotNull(q);
			Assert.AreEqual(q.GetBoost(), (float) 2.0, (float) 0.5);
			q = qp.Parse("hello^2.0");
			Assert.IsNotNull(q);
			Assert.AreEqual(q.GetBoost(), (float) 2.0, (float) 0.5);
			q = qp.Parse("\"on\"^1.0");
			Assert.IsNotNull(q);
			
			q = QueryParsers.QueryParser.Parse("the^3", "Field", new StandardAnalyzer());
			Assert.IsNotNull(q);
		}
示例#10
0
		public virtual Query GetQueryDOA(System.String query, Analyzer a)
		{
			if (a == null)
				a = new SimpleAnalyzer();
			QueryParsers.QueryParser qp = new QueryParsers.QueryParser("Field", a);
			qp.SetOperator(QueryParsers.QueryParser.DEFAULT_OPERATOR_AND);
			return qp.Parse(query);
		}
示例#11
0
		public virtual QueryParsers.QueryParser GetParser(Analyzer a)
		{
			if (a == null)
				a = new SimpleAnalyzer();
			QueryParsers.QueryParser qp = new QueryParsers.QueryParser("Field", a);
			qp.SetOperator(QueryParsers.QueryParser.DEFAULT_OPERATOR_OR);
			return qp;
		}