コード例 #1
0
        // Test that FieldScoreQuery returns docs with expected score.
        private void  DoTestCustomScore(System.String field, FieldScoreQuery.Type tp, double dboost)
        {
            float           boost   = (float)dboost;
            IndexSearcher   s       = new IndexSearcher(dir, true);
            FieldScoreQuery qValSrc = new FieldScoreQuery(field, tp); // a query that would score by the field
            QueryParser     qp      = new QueryParser(Util.Version.LUCENE_CURRENT, TEXT_FIELD, anlzr);

            System.String qtxt = "first aid text"; // from the doc texts in FunctionQuerySetup.

            // regular (boolean) query.
            Query q1 = qp.Parse(qtxt);

            Log(q1);

            // custom query, that should score the same as q1.
            CustomScoreQuery q2CustomNeutral = new CustomScoreQuery(q1);

            q2CustomNeutral.Boost = boost;
            Log(q2CustomNeutral);

            // custom query, that should (by default) multiply the scores of q1 by that of the field
            CustomScoreQuery q3CustomMul = new CustomScoreQuery(q1, qValSrc);

            q3CustomMul.SetStrict(true);
            q3CustomMul.Boost = boost;
            Log(q3CustomMul);

            // custom query, that should add the scores of q1 to that of the field
            CustomScoreQuery q4CustomAdd = new CustomAddQuery(q1, qValSrc);

            q4CustomAdd.SetStrict(true);
            q4CustomAdd.Boost = boost;
            Log(q4CustomAdd);

            // custom query, that multiplies and adds the field score to that of q1
            CustomScoreQuery q5CustomMulAdd = new CustomMulAddQuery(q1, qValSrc, qValSrc);

            q5CustomMulAdd.SetStrict(true);
            q5CustomMulAdd.Boost = boost;
            Log(q5CustomMulAdd);

            // do al the searches
            TopDocs td1 = s.Search(q1, null, 1000);
            TopDocs td2CustomNeutral = s.Search(q2CustomNeutral, null, 1000);
            TopDocs td3CustomMul     = s.Search(q3CustomMul, null, 1000);
            TopDocs td4CustomAdd     = s.Search(q4CustomAdd, null, 1000);
            TopDocs td5CustomMulAdd  = s.Search(q5CustomMulAdd, null, 1000);

            // put results in map so we can verify the scores although they have changed
            System.Collections.Hashtable h1 = TopDocsToMap(td1);
            System.Collections.Hashtable h2CustomNeutral = TopDocsToMap(td2CustomNeutral);
            System.Collections.Hashtable h3CustomMul     = TopDocsToMap(td3CustomMul);
            System.Collections.Hashtable h4CustomAdd     = TopDocsToMap(td4CustomAdd);
            System.Collections.Hashtable h5CustomMulAdd  = TopDocsToMap(td5CustomMulAdd);

            VerifyResults(boost, s, h1, h2CustomNeutral, h3CustomMul, h4CustomAdd, h5CustomMulAdd, q1, q2CustomNeutral, q3CustomMul, q4CustomAdd, q5CustomMulAdd);
        }
コード例 #2
0
 public CustomScoreProviderAnonymousInnerClassHelper(CustomMulAddQuery outerInstance, AtomicReaderContext context)
     : base(context)
 {
     this.outerInstance = outerInstance;
 }
コード例 #3
0
        private void DoTestCustomScore(ValueSource valueSource, double dboost)
        {
            float boost = (float)dboost;
            FunctionQuery functionQuery = new FunctionQuery(valueSource);
            IndexReader r = DirectoryReader.Open(dir);
            IndexSearcher s = NewSearcher(r);

            // regular (boolean) query.
            BooleanQuery q1 = new BooleanQuery();
            q1.Add(new TermQuery(new Term(TEXT_FIELD, "first")), BooleanClause.Occur.SHOULD);
            q1.Add(new TermQuery(new Term(TEXT_FIELD, "aid")), BooleanClause.Occur.SHOULD);
            q1.Add(new TermQuery(new Term(TEXT_FIELD, "text")), BooleanClause.Occur.SHOULD);
            Log(q1);

            // custom query, that should score the same as q1.
            BooleanQuery q2CustomNeutral = new BooleanQuery(true);
            Query q2CustomNeutralInner = new CustomScoreQuery(q1);
            q2CustomNeutral.Add(q2CustomNeutralInner, BooleanClause.Occur.SHOULD);
            // a little tricky: we split the boost across an outer BQ and CustomScoreQuery
            // this ensures boosting is correct across all these functions (see LUCENE-4935)
            q2CustomNeutral.Boost = (float)Math.Sqrt(dboost);
            q2CustomNeutralInner.Boost = (float)Math.Sqrt(dboost);
            Log(q2CustomNeutral);

            // custom query, that should (by default) multiply the scores of q1 by that of the field
            CustomScoreQuery q3CustomMul = new CustomScoreQuery(q1, functionQuery);
            q3CustomMul.Strict = true;
            q3CustomMul.Boost = boost;
            Log(q3CustomMul);

            // custom query, that should add the scores of q1 to that of the field
            CustomScoreQuery q4CustomAdd = new CustomAddQuery(q1, functionQuery);
            q4CustomAdd.Strict = true;
            q4CustomAdd.Boost = boost;
            Log(q4CustomAdd);

            // custom query, that multiplies and adds the field score to that of q1
            CustomScoreQuery q5CustomMulAdd = new CustomMulAddQuery(q1, functionQuery, functionQuery);
            q5CustomMulAdd.Strict = true;
            q5CustomMulAdd.Boost = boost;
            Log(q5CustomMulAdd);

            // do al the searches
            TopDocs td1 = s.Search(q1, null, 1000);
            TopDocs td2CustomNeutral = s.Search(q2CustomNeutral, null, 1000);
            TopDocs td3CustomMul = s.Search(q3CustomMul, null, 1000);
            TopDocs td4CustomAdd = s.Search(q4CustomAdd, null, 1000);
            TopDocs td5CustomMulAdd = s.Search(q5CustomMulAdd, null, 1000);

            // put results in map so we can verify the scores although they have changed
            IDictionary<int, float> h1 = TopDocsToMap(td1);
            IDictionary<int, float> h2CustomNeutral = TopDocsToMap(td2CustomNeutral);
            IDictionary<int, float> h3CustomMul = TopDocsToMap(td3CustomMul);
            IDictionary<int, float> h4CustomAdd = TopDocsToMap(td4CustomAdd);
            IDictionary<int, float> h5CustomMulAdd = TopDocsToMap(td5CustomMulAdd);

            VerifyResults(boost, s, h1, h2CustomNeutral, h3CustomMul, h4CustomAdd, h5CustomMulAdd, q1, q2CustomNeutral, q3CustomMul, q4CustomAdd, q5CustomMulAdd);
            r.Dispose();
        }
コード例 #4
0
		// Test that FieldScoreQuery returns docs with expected score.
		private void  DoTestCustomScore(System.String field, FieldScoreQuery.Type tp, double dboost)
		{
			float boost = (float) dboost;
			IndexSearcher s = new IndexSearcher(dir, true);
			FieldScoreQuery qValSrc = new FieldScoreQuery(field, tp); // a query that would score by the field
			QueryParser qp = new QueryParser(Util.Version.LUCENE_CURRENT, TEXT_FIELD, anlzr);
			System.String qtxt = "first aid text"; // from the doc texts in FunctionQuerySetup.
			
			// regular (boolean) query.
			Query q1 = qp.Parse(qtxt);
			Log(q1);
			
			// custom query, that should score the same as q1.
			CustomScoreQuery q2CustomNeutral = new CustomScoreQuery(q1);
			q2CustomNeutral.Boost = boost;
			Log(q2CustomNeutral);
			
			// custom query, that should (by default) multiply the scores of q1 by that of the field
			CustomScoreQuery q3CustomMul = new CustomScoreQuery(q1, qValSrc);
			q3CustomMul.SetStrict(true);
			q3CustomMul.Boost = boost;
			Log(q3CustomMul);
			
			// custom query, that should add the scores of q1 to that of the field
			CustomScoreQuery q4CustomAdd = new CustomAddQuery(q1, qValSrc);
			q4CustomAdd.SetStrict(true);
			q4CustomAdd.Boost = boost;
			Log(q4CustomAdd);
			
			// custom query, that multiplies and adds the field score to that of q1
			CustomScoreQuery q5CustomMulAdd = new CustomMulAddQuery(q1, qValSrc, qValSrc);
			q5CustomMulAdd.SetStrict(true);
			q5CustomMulAdd.Boost = boost;
			Log(q5CustomMulAdd);
			
			// do al the searches 
			TopDocs td1 = s.Search(q1, null, 1000);
			TopDocs td2CustomNeutral = s.Search(q2CustomNeutral, null, 1000);
			TopDocs td3CustomMul = s.Search(q3CustomMul, null, 1000);
			TopDocs td4CustomAdd = s.Search(q4CustomAdd, null, 1000);
			TopDocs td5CustomMulAdd = s.Search(q5CustomMulAdd, null, 1000);
			
			// put results in map so we can verify the scores although they have changed
			System.Collections.Hashtable h1 = TopDocsToMap(td1);
			System.Collections.Hashtable h2CustomNeutral = TopDocsToMap(td2CustomNeutral);
			System.Collections.Hashtable h3CustomMul = TopDocsToMap(td3CustomMul);
			System.Collections.Hashtable h4CustomAdd = TopDocsToMap(td4CustomAdd);
			System.Collections.Hashtable h5CustomMulAdd = TopDocsToMap(td5CustomMulAdd);
			
			VerifyResults(boost, s, h1, h2CustomNeutral, h3CustomMul, h4CustomAdd, h5CustomMulAdd, q1, q2CustomNeutral, q3CustomMul, q4CustomAdd, q5CustomMulAdd);
		}
コード例 #5
0
ファイル: TestCustomScoreQuery.cs プロジェクト: wwb/lucenenet
        private void DoTestCustomScore(ValueSource valueSource, double dboost)
        {
            float         boost         = (float)dboost;
            FunctionQuery functionQuery = new FunctionQuery(valueSource);
            IndexReader   r             = DirectoryReader.Open(dir);
            IndexSearcher s             = NewSearcher(r);

            // regular (boolean) query.
            BooleanQuery q1 = new BooleanQuery();

            q1.Add(new TermQuery(new Term(TEXT_FIELD, "first")), BooleanClause.Occur.SHOULD);
            q1.Add(new TermQuery(new Term(TEXT_FIELD, "aid")), BooleanClause.Occur.SHOULD);
            q1.Add(new TermQuery(new Term(TEXT_FIELD, "text")), BooleanClause.Occur.SHOULD);
            Log(q1);

            // custom query, that should score the same as q1.
            BooleanQuery q2CustomNeutral      = new BooleanQuery(true);
            Query        q2CustomNeutralInner = new CustomScoreQuery(q1);

            q2CustomNeutral.Add(q2CustomNeutralInner, BooleanClause.Occur.SHOULD);
            // a little tricky: we split the boost across an outer BQ and CustomScoreQuery
            // this ensures boosting is correct across all these functions (see LUCENE-4935)
            q2CustomNeutral.Boost      = (float)Math.Sqrt(dboost);
            q2CustomNeutralInner.Boost = (float)Math.Sqrt(dboost);
            Log(q2CustomNeutral);

            // custom query, that should (by default) multiply the scores of q1 by that of the field
            CustomScoreQuery q3CustomMul = new CustomScoreQuery(q1, functionQuery);

            q3CustomMul.Strict = true;
            q3CustomMul.Boost  = boost;
            Log(q3CustomMul);

            // custom query, that should add the scores of q1 to that of the field
            CustomScoreQuery q4CustomAdd = new CustomAddQuery(q1, functionQuery);

            q4CustomAdd.Strict = true;
            q4CustomAdd.Boost  = boost;
            Log(q4CustomAdd);

            // custom query, that multiplies and adds the field score to that of q1
            CustomScoreQuery q5CustomMulAdd = new CustomMulAddQuery(q1, functionQuery, functionQuery);

            q5CustomMulAdd.Strict = true;
            q5CustomMulAdd.Boost  = boost;
            Log(q5CustomMulAdd);

            // do al the searches
            TopDocs td1 = s.Search(q1, null, 1000);
            TopDocs td2CustomNeutral = s.Search(q2CustomNeutral, null, 1000);
            TopDocs td3CustomMul     = s.Search(q3CustomMul, null, 1000);
            TopDocs td4CustomAdd     = s.Search(q4CustomAdd, null, 1000);
            TopDocs td5CustomMulAdd  = s.Search(q5CustomMulAdd, null, 1000);

            // put results in map so we can verify the scores although they have changed
            IDictionary <int, float> h1 = TopDocsToMap(td1);
            IDictionary <int, float> h2CustomNeutral = TopDocsToMap(td2CustomNeutral);
            IDictionary <int, float> h3CustomMul     = TopDocsToMap(td3CustomMul);
            IDictionary <int, float> h4CustomAdd     = TopDocsToMap(td4CustomAdd);
            IDictionary <int, float> h5CustomMulAdd  = TopDocsToMap(td5CustomMulAdd);

            VerifyResults(boost, s, h1, h2CustomNeutral, h3CustomMul, h4CustomAdd, h5CustomMulAdd, q1, q2CustomNeutral, q3CustomMul, q4CustomAdd, q5CustomMulAdd);
            r.Dispose();
        }
コード例 #6
0
ファイル: TestCustomScoreQuery.cs プロジェクト: wwb/lucenenet
 public CustomScoreProviderAnonymousInnerClassHelper(CustomMulAddQuery outerInstance, AtomicReaderContext context) : base(context)
 {
     this.outerInstance = outerInstance;
 }