Example usage for org.apache.lucene.search.similarities SimilarityBase SimilarityBase

List of usage examples for org.apache.lucene.search.similarities SimilarityBase SimilarityBase

Introduction

In this page you can find the example usage for org.apache.lucene.search.similarities SimilarityBase SimilarityBase.

Prototype

public SimilarityBase() 

Source Link

Document

Sole constructor.

Usage

From source file:de.uni_koeln.spinfo.maalr.lucene.core.DictionaryLoader.java

License:Apache License

private synchronized void loadIndex() throws NoIndexAvailableException {
    if (searcher == null) {
        try {/*from  www .ja  v a 2s.c om*/
            logger.info("Loading index from directory " + environment.getLuceneIndexDir().getAbsolutePath());
            NIOFSDirectory directory = new NIOFSDirectory(environment.getLuceneIndexDir());
            ram = new RAMDirectory(directory, new IOContext());
            reader = DirectoryReader.open(ram);
            searcher = new IndexSearcher(reader);
            searcher.setSimilarity(new SimilarityBase() {

                @Override
                public String toString() {
                    return "Constant Similarity";
                }

                @Override
                protected float score(BasicStats stats, float freq, float docLen) {
                    return stats.getTotalBoost();
                }

            });
            directory.close();
            logger.info("Index loaded.");
        } catch (IOException e) {
            throw new NoIndexAvailableException("Failed to load index", e);
        }
    }
}

From source file:net.semanticmetadata.lire.indexing.LocalitySensitiveHashingTest.java

License:Open Source License

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]);
    }/*from   w w w. j  a  v a2 s  .  com*/

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

    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;
}