0
votes

Using Lucene 4.3.0

New to Lucene. I want to get more documents like the currently selected document. From my research, older versions of Lucene had a MoreLikeThis (which is similar behavior to what I want).

I put together some toy code to test options. I have indexing completed and include TermVector in the indexing.

Code Exceprt

QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, "body", this.analyzer);
Query query = null ;
try {
    query = parser.parse(searchterm);
    ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs;
    simpleresult = simpleresult + "HITS = " + hits.length + "\n";
    IndexReader ir = isearcher.getIndexReader() ; //2013-06-09 testing
    simpleresult = simpleresult + "Total Indexed Num Docs = " + ir.numDocs() + "\n" ;

    //Loop through results and construct simple string representation
    for (int i = 0; i < hits.length; i++) {
        Document hitdoc = isearcher.doc(hits[i].doc);
        float docscore = hits[i].score ;

        simpleresult = simpleresult + "=======" + (i+1) + "=======\n" ;
        simpleresult = simpleresult + "DOCDBID: " + hitdoc.get("dbid") + "\n" ;
        simpleresult = simpleresult + "Score: " + docscore + "\n" ;

        simpleresult = simpleresult + "File: " + hitdoc.get("filename") + "\n" ;
        simpleresult = simpleresult + hitdoc.get("body") ;
        simpleresult = simpleresult + "\n--------META--------\n" ;
        simpleresult = simpleresult + hitdoc.get("meta") ;
        simpleresult = simpleresult + "==============\n" ;

        //TESTING 2013-06-09
        //Trying to mimic similar documents
        //Feed the text contents of the current document back into nother query?????
        query = parser.parse(hitdoc.get("body"));
        ScoreDoc[] simhits = isearcher.search(query, null, 1000).scoreDocs;
        TopDocs top = isearcher.search(query, 10);
        simpleresult = simpleresult + "Similar Hits = " + simhits.length + "\n";
        simpleresult = simpleresult + "Top Hits MaxScore= " + top.getMaxScore() + "\n"; //why does this score differ from the above scores???????
        simpleresult = simpleresult + "Top Hits = " + top.totalHits + "\n";

      }
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

this.close() ;

Again, this is an excerpt from a toy example so I can better learn Lucene. It essentially just performs a simple query, displays each result (in a GUI), and then tries to re-query using each document to see any similar documents to mimic MoreLikeThis. What I am trying to do is get documents similar to the document. I

Is the ty example the proper way to do this in Lucene 4+?

1

1 Answers

1
votes

MoreLikeThis is still around. It's in the lucene-queries jar. I think it should be simple enough to use:

MoreLikeThis mlt = MoreLikeThis(ir);
Query likeQuery = mlt.like(hits[i].doc);
TopDocs results = isearcher.search(likeQuery);
//etc