Java 类org.apache.lucene.search.similarities.BasicStats 实例源码

项目:lire    文件:LocalitySensitiveHashingTest.java   
public double singleSearch(int docNum) throws IOException, InstantiationException, IllegalAccessException {
        IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(indexPath)));

        // -----------

        String query = reader.document(docNum).getValues("hash")[0];
        CEDD ceddQuery = new CEDD();
        ceddQuery.setByteArrayRepresentation(reader.document(docNum).getField(DocumentBuilder.FIELD_NAME_CEDD).binaryValue().bytes, reader.document(docNum).getField(DocumentBuilder.FIELD_NAME_CEDD).binaryValue().offset, reader.document(docNum).getField(DocumentBuilder.FIELD_NAME_CEDD).binaryValue().length);

        // -----------

        HashSet<String> gold = new HashSet<String>(numImagesEval);
        ImageSearcher cis = ImageSearcherFactory.createCEDDImageSearcher(100);
        ImageSearchHits hits = cis.search(reader.document(docNum), reader);
        for (int i = 0; i < 10; i++) {
            gold.add(hits.doc(i).getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0]);
        }

        // ------------

        IndexSearcher searcher = new IndexSearcher(reader);
        searcher.setSimilarity(new SimilarityBase() {
            @Override
            protected float score(BasicStats basicStats, float freq, float v2) {
                return 1;
            }

            @Override
            public String toString() {
                return null;
            }
        });
        TopDocs topDocs = searcher.search(createQuery(query), 500);
        topDocs = rerank(topDocs, ceddQuery, reader);
//        System.out.println("topDocs.scoreDocs.length = " + topDocs.scoreDocs.length);
        double numMatches = 0;
        for (int i = 0; i < topDocs.scoreDocs.length; i++) {
            ScoreDoc scoreDoc = topDocs.scoreDocs[i];
//            System.out.print(scoreDoc.score + ": ");
            String file = reader.document(scoreDoc.doc).getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0];
//            System.out.println(file.substring(file.lastIndexOf('/') + 1) + (gold.contains(file)?" x":" o"));
            if (gold.contains(file)) numMatches++;
        }
        return numMatches;
    }