private static float searchPhrase ( File indexDir , String A , String B ) throws Exception { Directory fsDir = FSDirectory . getDirectory ( indexDir ) ; IndexSearcher searcher = new IndexSearcher ( fsDir ) ; long start = new Date ( ) . getTime ( ) ; QueryParser parser = new QueryParser ( "contents" , new StandardAnalyzer ( ) ) ; //System.err.println("searching for: '\"" + A + " " + B + "\"~"+MAX_PHRASE+"'"); parser . setPhraseSlop ( MAX_PHRASE ) ; String my_phrase = "\"" + A + " " + B + "\"" ; Query query = parser . parse ( my_phrase ) ; //System.err.println("total hits: " + results.totalHits); //set similarity to use only the frequencies //score is based on frequency of phrase only searcher . setSimilarity ( new Similarity ( ) { public static final long serialVersionUID = 1L ; public float coord ( int overlap , int maxOverlap ) { return 1 ; } public float queryNorm ( float sumOfSquaredWeights ) { return 1 ; } public float tf ( float freq ) { return freq ; } public float idf ( int docFreq , int numDocs ) { return 1 ; } public float lengthNorm ( String fieldName , int numTokens ) { return 1 ; } public float sloppyFreq ( int distance ) { return 1 ; } } ) ; TopDocs results = searcher . search ( query , 10 ) ; ScoreDoc [ ] hits = results . scoreDocs ; float total_score = 0 ; //add up the scores for ( ScoreDoc hit : hits ) { Document doc = searcher . doc ( hit . doc ) ; //System.err.printf("%5.3f %sn\n", // hit.score, doc.get("contents")); total_score += hit . score ; } long end = new Date ( ) . getTime ( ) ; searcher . close ( ) ; return total_score ; }
method that actually does the searching