Пример #1
0
        public virtual void TestNegativeQueryBoost()
        {
            Query q = new TermQuery(new Term("foo", "bar"));
            q.Boost = -42f;
            Assert.AreEqual(-42f, q.Boost, 0.0f);

            Directory directory = newDirectory();
            try
            {
              Analyzer analyzer = new MockAnalyzer(random());
              IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer);

              IndexWriter writer = new IndexWriter(directory, conf);
              try
              {
            Document d = new Document();
            d.add(newTextField("foo", "bar", Field.Store.YES));
            writer.addDocument(d);
              }
              finally
              {
            writer.close();
              }

              IndexReader reader = DirectoryReader.open(directory);
              try
              {
            IndexSearcher searcher = newSearcher(reader);

            ScoreDoc[] hits = searcher.search(q, null, 1000).scoreDocs;
            Assert.AreEqual(1, hits.Length);
            Assert.IsTrue("score is not negative: " + hits[0].score, hits[0].score < 0);

            Explanation explain = searcher.explain(q, hits[0].doc);
            Assert.AreEqual("score doesn't match explanation", hits[0].score, explain.Value, 0.001f);
            Assert.IsTrue("explain doesn't think doc is a match", explain.Match);

              }
              finally
              {
            reader.close();
              }
            }
            finally
            {
              directory.close();
            }
        }
        private void DoTest(Random random, PrintWriter @out, bool useCompoundFiles, int MAX_DOCS)
        {
            Directory directory = newDirectory();
              Analyzer analyzer = new MockAnalyzer(random);
              IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer);
              MergePolicy mp = conf.MergePolicy;
              mp.NoCFSRatio = useCompoundFiles ? 1.0 : 0.0;
              IndexWriter writer = new IndexWriter(directory, conf);
              if (VERBOSE)
              {
            Console.WriteLine("TEST: now build index MAX_DOCS=" + MAX_DOCS);
              }

              for (int j = 0; j < MAX_DOCS; j++)
              {
            Document d = new Document();
            d.Add(newTextField(PRIORITY_FIELD, HIGH_PRIORITY, Field.Store.YES));
            d.Add(newTextField(ID_FIELD, Convert.ToString(j), Field.Store.YES));
            writer.addDocument(d);
              }
              writer.Dispose();

              // try a search without OR
              IndexReader reader = DirectoryReader.Open(directory);
              IndexSearcher searcher = newSearcher(reader);

              Query query = new TermQuery(new Term(PRIORITY_FIELD, HIGH_PRIORITY));
              @out.println("Query: " + query.ToString(PRIORITY_FIELD));
              if (VERBOSE)
              {
            Console.WriteLine("TEST: search query=" + query);
              }

              Sort sort = new Sort(SortField.FIELD_SCORE, new SortField(ID_FIELD, SortField.Type.INT));

              ScoreDoc[] hits = searcher.Search(query, null, MAX_DOCS, sort).scoreDocs;
              PrintHits(@out, hits, searcher);
              CheckHits(hits, MAX_DOCS, searcher);

              // try a new search with OR
              searcher = newSearcher(reader);
              hits = null;

              BooleanQuery booleanQuery = new BooleanQuery();
              booleanQuery.Add(new TermQuery(new Term(PRIORITY_FIELD, HIGH_PRIORITY)), BooleanClause.Occur_e.SHOULD);
              booleanQuery.Add(new TermQuery(new Term(PRIORITY_FIELD, MED_PRIORITY)), BooleanClause.Occur_e.SHOULD);
              @out.println("Query: " + booleanQuery.ToString(PRIORITY_FIELD));

              hits = searcher.search(booleanQuery, null, MAX_DOCS, sort).scoreDocs;
              PrintHits(@out, hits, searcher);
              CheckHits(hits, MAX_DOCS, searcher);

              reader.Close();
              directory.Close();
        }
Пример #3
0
        private void DoTestSearch(Random random, PrintWriter @out, bool useCompoundFile)
        {
            Directory directory = newDirectory();
              Analyzer analyzer = new MockAnalyzer(random);
              IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer);
              MergePolicy mp = conf.MergePolicy;
              mp.NoCFSRatio = useCompoundFile ? 1.0 : 0.0;
              IndexWriter writer = new IndexWriter(directory, conf);

              string[] docs = new 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(newTextField("contents", docs[j], Field.Store.YES));
            d.add(newStringField("id", "" + j, Field.Store.NO));
            writer.addDocument(d);
              }
              writer.close();

              IndexReader reader = DirectoryReader.open(directory);
              IndexSearcher searcher = newSearcher(reader);

              ScoreDoc[] hits = null;

              Sort sort = new Sort(SortField.FIELD_SCORE, new SortField("id", SortField.Type.INT));

              foreach (Query query in BuildQueries())
              {
            @out.println("Query: " + query.ToString("contents"));
            if (VERBOSE)
            {
              Console.WriteLine("TEST: query=" + query);
            }

            hits = searcher.search(query, null, 1000, sort).scoreDocs;

            @out.println(hits.Length + " total results");
            for (int i = 0 ; i < hits.Length && i < 10; i++)
            {
              Document d = searcher.doc(hits[i].doc);
              @out.println(i + " " + hits[i].score + " " + d.get("contents"));
            }
              }
              reader.close();
              directory.close();
        }