/** * Rebuild a suggestion index from the document index. * * This method iterates through the entire document index and makes sure that only unique titles * are indexed. * * @param indexRoot The parent directory inside which both the document index and the suggestion * index lives. * @throws IOException */ public static void rebuild(String indexRoot) throws IOException { Path indexRootPath = Paths.get(indexRoot); Path suggestionPath = getSuggestionIndexPath(indexRootPath); // Delete the suggestion index if it exists. if (Files.exists(suggestionPath)) { Util.deletePath(suggestionPath); } // Create the suggestion index. Analyzer analyzer = Indexer.getAnalyzer(); Directory suggestionDir = FSDirectory.open(getSuggestionIndexPath(indexRootPath)); AnalyzingInfixSuggester suggester = new AnalyzingInfixSuggester(suggestionDir, analyzer); // Open the document index. Directory indexDir = FSDirectory.open(Indexer.getMainIndexPath(indexRootPath)); IndexReader reader = DirectoryReader.open(indexDir); // Get a document iterator. DocumentDictionary docDict = new DocumentDictionary(reader, Indexer.TITLE_FIELD_NAME, null); InputIterator iterator = docDict.getEntryIterator(); Set<BytesRef> titleSet = new HashSet<>(); BytesRef next; while ((next = iterator.next()) != null) { if (titleSet.contains(next)) { continue; } titleSet.add(next); suggester.add(next, null, 0, null); } reader.close(); suggester.commit(); suggester.close(); }
@Override public Dictionary create(SolrCore core, SolrIndexSearcher searcher) { if(params == null) { // should not happen; implies setParams was not called throw new IllegalStateException("Value of params not set"); } String field = (String) params.get(FIELD); String weightField = (String) params.get(WEIGHT_FIELD); String payloadField = (String) params.get(PAYLOAD_FIELD); if (field == null) { throw new IllegalArgumentException(FIELD + " is a mandatory parameter"); } if (weightField == null) { throw new IllegalArgumentException(WEIGHT_FIELD + " is a mandatory parameter"); } return new DocumentDictionary(searcher.getIndexReader(), field, weightField, payloadField); }
private void buildSuggesterIndex() throws IOException { try (DirectoryReader reader = DirectoryReader.open(indexDir)) { final Dictionary dictionary = new DocumentDictionary(reader, "content", null, null, "username"); suggester.build(dictionary); suggester.refresh(); } }