Java 类org.apache.lucene.search.function.CustomScoreQuery 实例源码

项目:t4f-data    文件:FunctionQueryTest.java   
public void testCustomScoreQuery() throws Throwable {
  Query q = new QueryParser(Version.LUCENE_41,
                            "content",
                            new StandardAnalyzer(
                              Version.LUCENE_41))
               .parse("the green hat");
  FieldScoreQuery qf = new FieldScoreQuery("score",
                                           FieldScoreQuery.Type.BYTE);
  CustomScoreQuery customQ = new CustomScoreQuery(q, qf) {
    public CustomScoreProvider getCustomScoreProvider(IndexReader r) {
      return new CustomScoreProvider(r) {
        public float customScore(int doc,
                                 float subQueryScore,
                                 float valSrcScore) {
          return (float) (Math.sqrt(subQueryScore) * valSrcScore);
        }
      };
    }
  };

  TopDocs hits = s.search(customQ, 10);
  assertEquals(2, hits.scoreDocs.length);

  assertEquals(1, hits.scoreDocs[0].doc);           //
  assertEquals(0, hits.scoreDocs[1].doc);
}