SynonymIndexSearcher { public TopDocs searchDocumentBySynonym(String stringToSearch) throws IOException { TopDocs topDocs; Query query; switch (searchMode) { case MATCH_ANY: query = createCombinedQueryFor(stringToSearch, false, false); break; case MATCH_PARTIAL: query = createCombinedQueryForPartialMatch(stringToSearch); break; case MATCH_ALL: query = createCombinedQueryFor(stringToSearch, false, true); break; case MATCH_EXACT: query = createCombinedQueryForExactMatch(stringToSearch); break; case MATCH_ANY_FUZZY: query = createCombinedQueryFor(stringToSearch, true, false); break; case MATCH_ALL_FUZZY: query = createCombinedQueryFor(stringToSearch, true, true); break; default: query = createCombinedQueryFor(stringToSearch, false, false); break; } final IndexSearcher searcher = mgr.acquire(); topDocs = searcher.search(query, topDocLimit); mgr.release(searcher); return topDocs; } @Deprecated  SynonymIndexSearcher();  SynonymIndexSearcher(String indexPath);   SynonymIndexSearcher(Directory indexDir); void openIndexInFS(String path); TopDocs searchDocumentByWord(String word); TopDocs searchDocumentBySynonym(String stringToSearch); int getSynonymCount(String word); Document getDocument(int docNum); String getWordByDocNumber(int docNo); String[] getSynonymsByDocNumber(int docNo); int getNumDocs(); int getMaxDoc(); int getTopDocLimit(); void setTopDocLimit(int topDocLimit); int getSlop(); void setSlop(int slop); void setAnalyzer(Analyzer analyzer); Analyzer getAnalyzer(); void close(); SynonymSearchMode getSearchMode(); void setSearchMode(SynonymSearchMode searchMode); void setMaxEdits(int maxEdits); float getMatchingThreshold(); void setMatchingThreshold(float matchingThreshold); void setMatchingThreshold(double matchingThreshold); static List<String> getTokensFromAnalyzer(String input); static final String F_WORD; static final String F_SYN; static final String F_WORDTERM; static final String F_SYNTERM; }
@Test public void testSearchDocumentBySynonym() throws IOException { printLineToConsole("\n-----------Test searchDocumentBySynonym----------"); SynonymIndexSearcher searcher = getSearcher(); searcher.setAnalyzer(new StandardAnalyzer(CharArraySet.EMPTY_SET)); TopDocs docs = searcher.searchDocumentBySynonym("ibm"); printLineToConsole(docs.totalHits + " documents found."); doAssertEquals("unexpected totalHits size!", 3, docs.totalHits.value); doAssertEquals(true, searcher.getTopDocLimit() >= docs.scoreDocs.length); for (int i = 0; i < docs.scoreDocs.length; i++) { int docNumber = docs.scoreDocs[i].doc; printToConsole("\ndoc=" + docNumber + "\tscore=" + docs.scoreDocs[i].score); printLineToConsole("\tword: " + searcher.getWordByDocNumber(docNumber)); printLineToConsole("\tsynonyms: " + Arrays.toString(searcher.getSynonymsByDocNumber(docNumber))); } String[] bigblue = { "Big Blue", "big blue", "Blue", "big" }; for (int i = 0; i < bigblue.length; i++) { String toFind = bigblue[i]; TopDocs doc = searcher.searchDocumentBySynonym(toFind); doAssertEquals("we should have found at least one document, check the list of synonyms or the code", false, doc.totalHits.value == 0); } searcher.close(); }