我在Lucene 3.5中运行代码。但是,如何在Lucene 4.5中运行代码,

        public class CustomScoreQuerySample {
            private Log logger = LogFactory.getLog(CustomScoreQuerySample.class);

            public static void main(String[] args) throws Exception {
                CustomScoreQuerySample sample = new CustomScoreQuerySample();
                sample.makeIndex();
                sample.execute("??");
            }

            private static final String[][] DATAS = {
                {"??","981"},
                {"??","672"},
                {"??","521"},
                {"??","124"},
                {"??","908"},
                {"??","652"},
                {"??","872"},
                {"??","278"},
                {"??","485"},
                {"??","372"}};

            private RAMDirectory dir = new RAMDirectory();
            private Analyzer analyzer = new SimpleAnalyzer();

            public void makeIndex() throws Exception{
                IndexWriter writer = new IndexWriter( dir, analyzer , true, MaxFieldLength.UNLIMITED);

                try{
                    for(int i=0;i<DATAS.length;i++){
                        Document doc = new Document();
                        doc.add( new Field( "value", DATAS[i][0], Store.YES, Index.NOT_ANALYZED ) );
                        doc.add( new Field( "count", DATAS[i][1], Store.YES, Index.NOT_ANALYZED ) );
                        writer.addDocument( doc );
                    }
                }finally{
                    writer.close();
                }
            }

            public void execute(String value) throws Exception{
                IndexSearcher searcher = new IndexSearcher( dir );
                try{
                    search(searcher,value);
                }finally{
                    searcher.close();
                }
            }

            private void search(Searcher searcher,String value) throws Exception{

                Query tq = new TermQuery(new Term("value",value));

                FloatFieldSource source = new FloatFieldSource("count");
                ValueSourceQuery vq = new ValueSourceQuery(source);

                //FieldScoreQuery vq = new FieldScoreQuery("count",FieldScoreQuery.Type.INT);

                CustomScoreQuery query = new CustomScoreQuery(tq,vq) {
                    @Override
                    public float customScore(int doc, float subQueryScore, float valSrcScore) {
                        float result = subQueryScore * valSrcScore;
                        System.out.println("doc:"+doc+" / subQueryScore:"+subQueryScore+" / valSrcScore:"+valSrcScore + " / result:"+result);
                        return valSrcScore;
                    }
                };

                query.setStrict(true);

                TopScoreDocCollector collector = null;
                collector = TopScoreDocCollector.create(5 * 5, false);

                searcher.search(query, collector);
                ScoreDoc[] hits = collector.topDocs().scoreDocs;

                int count = hits.length;
                for( int i = 0; i < count; i++ ){
                    Document doc = searcher.doc(hits[i].doc);
                    float score = hits[i].score;
                    System.out.println( score + " : " + doc.get( "value" )+" / "+ doc.get( "count" ));
                    //Explanation exp = searcher.explain( query, hits[i].doc );
                    //System.out.println( exp.toString() );
                }
            }
        }


如何在Lucene 4.5中使用此“ CustomScoreQuery”?我尝试了以下代码,但无法获得任何答案,

CustomScoreQuery cstquery =new CustomScoreQuery(tq);


但是在这里我看不到valueScore。我在API中找不到。任何人都可以,请帮助我解决此问题。

最佳答案

从3.X到4.X,大量更改了,但是基本上您需要使用FunctionQuery代替ValueSourceQuery。对此进行了一些简化,并针对4.4进行了编写,但对于4.5则应相同。

public class CustomScoreQuerySample {

public static void main(String[] args) throws Exception {
    CustomScoreQuerySample sample = new CustomScoreQuerySample();
    sample.makeIndex();
    sample.search("XX");
}

private static final String[][] DATAS = { { "XX", "981" }, { "XX", "672" },
        { "XX", "521" }, { "XX", "124" }, { "XX", "908" }, { "XX", "652" },
        { "XX", "872" }, { "XX", "278" }, { "XX", "485" }, { "XX", "372" } };

private RAMDirectory dir = new RAMDirectory();
private Analyzer analyzer = new SimpleAnalyzer(Version.LUCENE_44);

public void makeIndex() throws Exception {
    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_44, analyzer);
    IndexWriter writer = new IndexWriter(dir, config);

    try {
        for (int i = 0; i < DATAS.length; i++) {
            Document doc = new Document();
            doc.add(new TextField("value", DATAS[i][0], Field.Store.YES ));
            doc.add(new FloatField("count", Float.parseFloat(DATAS[i][1]), Field.Store.YES ));
            writer.addDocument(doc);
        }
    } finally {
        writer.close();
    }
}
private void search(String value) throws Exception {

    DirectoryReader reader = DirectoryReader.open(dir);
    try {
        QueryParser parser = new QueryParser(Version.LUCENE_44, "value", analyzer);
        Query tq = parser.parse("XX");

        FloatFieldSource source = new FloatFieldSource("count");
        FunctionQuery fq = new FunctionQuery(source);

        CustomScoreQuery query = new CustomScoreQuery(tq, fq);

        TopScoreDocCollector collector = TopScoreDocCollector.create(5 * 5, false);

        IndexSearcher searcher = new IndexSearcher(reader);
        searcher.search(query,collector);

        ScoreDoc[] hits = collector.topDocs().scoreDocs;

        int count = hits.length;
        for (int i = 0; i < count; i++) {
            Document doc = searcher.doc(hits[i].doc);
            float score = hits[i].score;
            System.out.println(score + " : " + doc.get("value") + " / "
                    + doc.get("count"));
        }
    } finally {
        reader.close();
    }
}
}

关于java - 如何使CustomScoreQuery在Lucene 4.5版中运行。,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20680883/

10-10 06:23