Krishnendu Ghosh
Krishnendu Ghosh

Reputation: 334

How to rank documents using tfidf similairty in lucene

In a basic code to create index and search queries, I want to use TFIDFsimilarity to rank retrieved documents. But I am getting an error "Cannot instantiate the type TFIDFSimilarity". My code is given below:

public class TFIDF_T {

private static Document createDocument(String id, String tb) {
    Document doc = new Document();
    doc.add(new Field("id", id, TextField.TYPE_STORED));
    doc.add(new Field("tb", tb, TextField.TYPE_STORED));
    return doc;
}

private static void search(IndexSearcher searcher, String queryString, Analyzer analyzer) throws IOException, ParseException {
    QueryParser parser = new QueryParser("tb", analyzer);
    Query query = parser.parse(queryString);
    ScoreDoc[] hits = searcher.search(query, 20).scoreDocs;
    int hitCount = hits.length;
    for (int i = 0; i < hitCount; i++) {
        Document doc = searcher.doc(hits[i].doc);
        System.out.println(doc.get("id"));
    }
}

public static void run(String path) throws IOException, ParseException {
    int ctr = 0;
    Analyzer analyzer = new StandardAnalyzer();
    RAMDirectory directory = new RAMDirectory();
    IndexWriterConfig config = new IndexWriterConfig(analyzer);
    IndexWriter iwriter = new IndexWriter(directory, config);
        String id = "101";
        String tb = "How to rank documents using tfidfsimilairty";
        iwriter.addDocument(createDocument(id, tb));
    }
    iwriter.close();
    DirectoryReader ireader = DirectoryReader.open(directory);
    IndexSearcher isearcher = new IndexSearcher(ireader);
    isearcher.setSimilarity(new **TFIDFSimilarity**());
    String tb1 = "How to rank documents using tfidfsimilairty";
    search(isearcher, tb1, analyzer);
    }
    ireader.close();
    directory.close();
  }
}

Upvotes: 3

Views: 1290

Answers (1)

femtoRgon
femtoRgon

Reputation: 33341

TFIDFSimilarity is an abstract class. You can't instantiate it.

ClassicSimilarity is an implementation of it (Assuming you are using 5.4 or later, otherwise DefaultSimilarity). Use that instead.

Upvotes: 2

Related Questions