예제 #1
0
        /// <summary>
        /// macro for readability </summary>
        public static Query Cswcq(Term wild)
        {
            WildcardQuery query = new WildcardQuery(wild);

            query.SetRewriteMethod(MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE);
            return(query);
        }
예제 #2
0
        public virtual void TestTermWithoutWildcard()
        {
            Directory indexStore = GetIndexStore("field", new string[] { "nowildcard", "nowildcardx" });
            IndexReader reader = DirectoryReader.Open(indexStore);
            IndexSearcher searcher = NewSearcher(reader);

            MultiTermQuery wq = new WildcardQuery(new Term("field", "nowildcard"));
            AssertMatches(searcher, wq, 1);

            wq.SetRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);
            wq.Boost = 0.1F;
            Query q = searcher.Rewrite(wq);
            Assert.IsTrue(q is TermQuery);
            Assert.AreEqual(q.Boost, wq.Boost, 0);

            wq.SetRewriteMethod(MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE);
            wq.Boost = 0.2F;
            q = searcher.Rewrite(wq);
            Assert.IsTrue(q is ConstantScoreQuery);
            Assert.AreEqual(q.Boost, wq.Boost, 0.1);

            wq.SetRewriteMethod(MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT);
            wq.Boost = 0.3F;
            q = searcher.Rewrite(wq);
            Assert.IsTrue(q is ConstantScoreQuery);
            Assert.AreEqual(q.Boost, wq.Boost, 0.1);

            wq.SetRewriteMethod(MultiTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE);
            wq.Boost = 0.4F;
            q = searcher.Rewrite(wq);
            Assert.IsTrue(q is ConstantScoreQuery);
            Assert.AreEqual(q.Boost, wq.Boost, 0.1);
            reader.Dispose();
            indexStore.Dispose();
        }
예제 #3
0
        public virtual void TestDeMorgan()
        {
            Directory         dir1 = NewDirectory();
            RandomIndexWriter iw1  = new RandomIndexWriter(Random(), dir1, Similarity, TimeZone);
            Document          doc1 = new Document();

            doc1.Add(NewTextField("field", "foo bar", Field.Store.NO));
            iw1.AddDocument(doc1);
            IndexReader reader1 = iw1.Reader;

            iw1.Dispose();

            Directory         dir2 = NewDirectory();
            RandomIndexWriter iw2  = new RandomIndexWriter(Random(), dir2, Similarity, TimeZone);
            Document          doc2 = new Document();

            doc2.Add(NewTextField("field", "foo baz", Field.Store.NO));
            iw2.AddDocument(doc2);
            IndexReader reader2 = iw2.Reader;

            iw2.Dispose();

            BooleanQuery query = new BooleanQuery(); // Query: +foo -ba*

            query.Add(new TermQuery(new Term("field", "foo")), BooleanClause.Occur.MUST);
            WildcardQuery wildcardQuery = new WildcardQuery(new Term("field", "ba*"));

            wildcardQuery.SetRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);
            query.Add(wildcardQuery, BooleanClause.Occur.MUST_NOT);

            MultiReader   multireader = new MultiReader(reader1, reader2);
            IndexSearcher searcher    = NewSearcher(multireader);

            Assert.AreEqual(0, searcher.Search(query, 10).TotalHits);


            Task foo = new Task(TestDeMorgan);

            TaskScheduler es = TaskScheduler.Default;

            searcher = new IndexSearcher(multireader, es);
            if (VERBOSE)
            {
                Console.WriteLine("rewritten form: " + searcher.Rewrite(query));
            }
            Assert.AreEqual(0, searcher.Search(query, 10).TotalHits);

            multireader.Dispose();
            reader1.Dispose();
            reader2.Dispose();
            dir1.Dispose();
            dir2.Dispose();
        }
예제 #4
0
        public virtual void TestTermWithoutWildcard()
        {
            Directory     indexStore = GetIndexStore("field", new string[] { "nowildcard", "nowildcardx" });
            IndexReader   reader     = DirectoryReader.Open(indexStore);
            IndexSearcher searcher   = NewSearcher(reader);

            MultiTermQuery wq = new WildcardQuery(new Term("field", "nowildcard"));

            AssertMatches(searcher, wq, 1);

            wq.SetRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);
            wq.Boost = 0.1F;
            Query q = searcher.Rewrite(wq);

            Assert.IsTrue(q is TermQuery);
            Assert.AreEqual(q.Boost, wq.Boost, 0);

            wq.SetRewriteMethod(MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE);
            wq.Boost = 0.2F;
            q        = searcher.Rewrite(wq);
            Assert.IsTrue(q is ConstantScoreQuery);
            Assert.AreEqual(q.Boost, wq.Boost, 0.1);

            wq.SetRewriteMethod(MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT);
            wq.Boost = 0.3F;
            q        = searcher.Rewrite(wq);
            Assert.IsTrue(q is ConstantScoreQuery);
            Assert.AreEqual(q.Boost, wq.Boost, 0.1);

            wq.SetRewriteMethod(MultiTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE);
            wq.Boost = 0.4F;
            q        = searcher.Rewrite(wq);
            Assert.IsTrue(q is ConstantScoreQuery);
            Assert.AreEqual(q.Boost, wq.Boost, 0.1);
            reader.Dispose();
            indexStore.Dispose();
        }
예제 #5
0
        public virtual Query Build(IQueryNode queryNode)
        {
            WildcardQueryNode wildcardNode = (WildcardQueryNode)queryNode;

            WildcardQuery q = new WildcardQuery(new Term(wildcardNode.GetFieldAsString(),
                                                 wildcardNode.GetTextAsString()));

            MultiTermQuery.RewriteMethod method = (MultiTermQuery.RewriteMethod)queryNode.GetTag(MultiTermRewriteMethodProcessor.TAG_ID);
            if (method != null)
            {
                q.SetRewriteMethod(method);
            }

            return q;
        }
예제 #6
0
        public virtual void TestEmptyTerm()
        {
            Directory     indexStore = GetIndexStore("field", new string[] { "nowildcard", "nowildcardx" });
            IndexReader   reader     = DirectoryReader.Open(indexStore);
            IndexSearcher searcher   = NewSearcher(reader);

            MultiTermQuery wq = new WildcardQuery(new Term("field", ""));

            wq.SetRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);
            AssertMatches(searcher, wq, 0);
            Query q = searcher.Rewrite(wq);

            Assert.IsTrue(q is BooleanQuery);
            Assert.AreEqual(0, ((BooleanQuery)q).Clauses.Length);
            reader.Dispose();
            indexStore.Dispose();
        }
예제 #7
0
        public virtual void TestEmptyTerm()
        {
            Directory indexStore = GetIndexStore("field", new string[] { "nowildcard", "nowildcardx" });
            IndexReader reader = DirectoryReader.Open(indexStore);
            IndexSearcher searcher = NewSearcher(reader);

            MultiTermQuery wq = new WildcardQuery(new Term("field", ""));
            wq.SetRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);
            AssertMatches(searcher, wq, 0);
            Query q = searcher.Rewrite(wq);
            Assert.IsTrue(q is BooleanQuery);
            Assert.AreEqual(0, ((BooleanQuery)q).Clauses.Length);
            reader.Dispose();
            indexStore.Dispose();
        }
 /// <summary>
 /// macro for readability </summary>
 public static Query Cswcq(Term wild)
 {
     WildcardQuery query = new WildcardQuery(wild);
     query.SetRewriteMethod(MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE);
     return query;
 }
예제 #9
0
        public virtual void TestDeMorgan()
        {
            Directory dir1 = NewDirectory();
            RandomIndexWriter iw1 = new RandomIndexWriter(Random(), dir1, Similarity, TimeZone);
            Document doc1 = new Document();
            doc1.Add(NewTextField("field", "foo bar", Field.Store.NO));
            iw1.AddDocument(doc1);
            IndexReader reader1 = iw1.Reader;
            iw1.Dispose();

            Directory dir2 = NewDirectory();
            RandomIndexWriter iw2 = new RandomIndexWriter(Random(), dir2, Similarity, TimeZone);
            Document doc2 = new Document();
            doc2.Add(NewTextField("field", "foo baz", Field.Store.NO));
            iw2.AddDocument(doc2);
            IndexReader reader2 = iw2.Reader;
            iw2.Dispose();

            BooleanQuery query = new BooleanQuery(); // Query: +foo -ba*
            query.Add(new TermQuery(new Term("field", "foo")), BooleanClause.Occur.MUST);
            WildcardQuery wildcardQuery = new WildcardQuery(new Term("field", "ba*"));
            wildcardQuery.SetRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);
            query.Add(wildcardQuery, BooleanClause.Occur.MUST_NOT);

            MultiReader multireader = new MultiReader(reader1, reader2);
            IndexSearcher searcher = NewSearcher(multireader);
            Assert.AreEqual(0, searcher.Search(query, 10).TotalHits);


            Task foo = new Task(TestDeMorgan);

            TaskScheduler es = TaskScheduler.Default;
            searcher = new IndexSearcher(multireader, es);
            if (VERBOSE)
            {
                Console.WriteLine("rewritten form: " + searcher.Rewrite(query));
            }
            Assert.AreEqual(0, searcher.Search(query, 10).TotalHits);

            multireader.Dispose();
            reader1.Dispose();
            reader2.Dispose();
            dir1.Dispose();
            dir2.Dispose();
        }
예제 #10
0
 /// <summary>
 /// Builds a new WildcardQuery instance
 /// </summary>
 /// <param name="t">wildcard term</param>
 /// <returns>new WildcardQuery instance</returns>
 protected internal virtual Query NewWildcardQuery(Term t)
 {
     WildcardQuery query = new WildcardQuery(t);
     query.SetRewriteMethod(MultiTermRewriteMethod);
     return query;
 }