List of usage examples for org.apache.lucene.index DirectoryReader open
public static DirectoryReader open(final IndexCommit commit) throws IOException
From source file:com.chimpler.example.FacetLuceneAdvancedSearcher.java
License:Apache License
public static void main(String args[]) throws Exception { if (args.length != 5) { System.err.println(/*ww w . j a va 2 s .c om*/ "Parameters: [index directory] [taxonomy directory] [query] [field drilldown] [value drilldown]"); System.exit(1); } String indexDirectory = args[0]; String taxonomyDirectory = args[1]; String query = args[2]; String fieldDrilldown = args[3]; String valueDrilldown = args[4]; IndexReader indexReader = DirectoryReader.open(FSDirectory.open(new File(indexDirectory))); IndexSearcher indexSearcher = new IndexSearcher(indexReader); TaxonomyReader taxonomyReader = new DirectoryTaxonomyReader(FSDirectory.open(new File(taxonomyDirectory))); CategoryPath drillDownCategoryPath = new CategoryPath(fieldDrilldown + "/" + valueDrilldown, '/'); FacetSearchParams searchParams = new FacetSearchParams(); searchParams.facetRequests.add(new CountFacetRequest(new CategoryPath("author"), 100)); searchParams.facetRequests.add(new CountFacetRequest(new CategoryPath("book_category"), 100)); searchParams.facetRequests.add(new CountFacetRequest(drillDownCategoryPath, 100)); ComplexPhraseQueryParser queryParser = new ComplexPhraseQueryParser(LUCENE_VERSION, "title", new StandardAnalyzer(LUCENE_VERSION)); Query luceneQuery = queryParser.parse(query); //luceneQuery = DrillDownQuery.query(luceneQuery, drillDownCategoryPath); // Collectors to get top results and facets TopScoreDocCollector topScoreDocCollector = TopScoreDocCollector.create(10, true); FacetsCollector facetsCollector = FacetsCollector.create(searchParams, indexReader, taxonomyReader); indexSearcher.search(luceneQuery, MultiCollector.wrap(topScoreDocCollector, facetsCollector)); System.out.println("Found:"); for (ScoreDoc scoreDoc : topScoreDocCollector.topDocs().scoreDocs) { Document document = indexReader.document(scoreDoc.doc); System.out.printf("- book: id=%s, title=%s, book_category=%s, authors=%s, score=%f\n", document.get("id"), document.get("title"), document.get("book_category"), document.get("authors"), scoreDoc.score); } System.out.println("Facets:"); for (FacetResult facetResult : facetsCollector.getFacetResults()) { System.out.println("- " + facetResult.getFacetResultNode().label); for (FacetResultNode facetResultNode : facetResult.getFacetResultNode().subResults) { System.out.printf(" - %s (%f)\n", facetResultNode.label.toString(), facetResultNode.value); for (FacetResultNode subFacetResultNode : facetResultNode.subResults) { System.out.printf(" - %s (%f)\n", subFacetResultNode.label.toString(), subFacetResultNode.value); } } } taxonomyReader.close(); indexReader.close(); }
From source file:com.chimpler.example.FacetLuceneSearcher.java
License:Apache License
public static void main(String args[]) throws Exception { // if (args.length != 3) { // System.err.println("Parameters: [index directory] [taxonomy directory] [query]"); // System.exit(1); // }/*w w w .j av a2 s. c o m*/ String indexDirectory = "index"; String taxonomyDirectory = "taxonomy"; String query = "story"; IndexReader indexReader = DirectoryReader.open(FSDirectory.open(new File(indexDirectory))); IndexSearcher indexSearcher = new IndexSearcher(indexReader); TaxonomyReader taxonomyReader = new DirectoryTaxonomyReader(FSDirectory.open(new File(taxonomyDirectory))); ArrayList<FacetRequest> facetRequests = new ArrayList<FacetRequest>(); facetRequests.add(new CountFacetRequest(new CategoryPath("author"), 100)); facetRequests.add(new CountFacetRequest(new CategoryPath("book_category"), 100)); FacetSearchParams searchParams = new FacetSearchParams(facetRequests); ComplexPhraseQueryParser queryParser = new ComplexPhraseQueryParser(LUCENE_VERSION, "title", new StandardAnalyzer(LUCENE_VERSION)); Query luceneQuery = queryParser.parse(query); // Collectors to get top results and facets TopScoreDocCollector topScoreDocCollector = TopScoreDocCollector.create(10, true); FacetsCollector facetsCollector = FacetsCollector.create(searchParams, indexReader, taxonomyReader); indexSearcher.search(luceneQuery, MultiCollector.wrap(topScoreDocCollector, facetsCollector)); System.out.println("Found:"); for (ScoreDoc scoreDoc : topScoreDocCollector.topDocs().scoreDocs) { Document document = indexReader.document(scoreDoc.doc); System.out.printf("- book: id=%s, title=%s, book_category=%s, authors=%s, score=%f\n", document.get("id"), document.get("title"), document.get("book_category"), document.get("authors"), scoreDoc.score); } System.out.println("Facets:"); for (FacetResult facetResult : facetsCollector.getFacetResults()) { System.out.println("- " + facetResult.getFacetResultNode().label); for (FacetResultNode facetResultNode : facetResult.getFacetResultNode().subResults) { System.out.printf(" - %s (%f)\n", facetResultNode.label.toString(), facetResultNode.value); for (FacetResultNode subFacetResultNode : facetResultNode.subResults) { System.out.printf(" - %s (%f)\n", subFacetResultNode.label.toString(), subFacetResultNode.value); } } } taxonomyReader.close(); indexReader.close(); }
From source file:com.codenvy.test.lucene.DeleteFilesWithSameName.java
License:Open Source License
public static void searchAndPrintResult(Path indexPath) throws Exception { IndexReader reader = DirectoryReader.open(FSDirectory.open(indexPath)); IndexSearcher searcher = new IndexSearcher(reader); Query query = new PrefixQuery(new Term(PATH, filesDirPath + "/File1")); TopDocs topDocs = searcher.search(query, 100); System.out.println();/* w w w . java2 s .c o m*/ System.out.println("=========================== search ================================="); final String[] result = new String[topDocs.scoreDocs.length]; for (int i = 0, length = result.length; i < length; i++) { result[i] = searcher.doc(topDocs.scoreDocs[i].doc).getField(PATH).stringValue(); System.out.println(result[i]); } reader.close(); }
From source file:com.codeReading.core.opengrok.SearchHelper.java
License:Open Source License
/** * Create the searcher to use wrt. to currently set parameters and the given * projects. Does not produce any {@link #redirect} link. It also does * nothing if {@link #redirect} or {@link #errorMsg} have a * none-{@code null} value. <p> Parameters which should be populated/set at * this time: <ul> <li>{@link #builder}</li> <li>{@link #dataRoot}</li> * <li>{@link #order} (falls back to relevance if unset)</li> * <li>{@link #parallel} (default: false)</li> </ul> Populates/sets: <ul> * <li>{@link #query}</li> <li>{@link #searcher}</li> <li>{@link #sort}</li> * <li>{@link #projects}</li> <li>{@link #errorMsg} if an error occurs</li> * </ul>//from w w w. ja v a 2 s.c o m * * @param projects project to use query. If empty, a none-project opengrok * setup is assumed (i.e. DATA_ROOT/index will be used instead of possible * multiple DATA_ROOT/$project/index). * @return this instance */ public SearchHelper prepareExec(SortedSet<String> projects) { if (redirect != null || errorMsg != null) { return this; } // the Query created by the QueryBuilder try { indexDir = new File(dataRoot, "index"); query = builder.build(); if (projects == null) { errorMsg = "No project selected!"; return this; } this.projects = projects; if (projects.isEmpty()) { //no project setup FSDirectory dir = FSDirectory.open(indexDir); searcher = new IndexSearcher(DirectoryReader.open(dir)); } else if (projects.size() == 1) { // just 1 project selected FSDirectory dir = FSDirectory.open(new File(indexDir, projects.first())); searcher = new IndexSearcher(DirectoryReader.open(dir)); } else { //more projects IndexReader[] subreaders = new IndexReader[projects.size()]; int ii = 0; //TODO might need to rewrite to Project instead of // String , need changes in projects.jspf too for (String proj : projects) { FSDirectory dir = FSDirectory.open(new File(indexDir, proj)); subreaders[ii++] = DirectoryReader.open(dir); } MultiReader searchables = new MultiReader(subreaders, true); if (parallel) { int noThreads = 2 + (2 * Runtime.getRuntime().availableProcessors()); //TODO there might be a better way for counting this executor = Executors.newFixedThreadPool(noThreads); } searcher = parallel ? new IndexSearcher(searchables, executor) : new IndexSearcher(searchables); } // TODO check if below is somehow reusing sessions so we don't // requery again and again, I guess 2min timeout sessions could be // usefull, since you click on the next page within 2mins, if not, // then wait ;) switch (order) { case LASTMODIFIED: sort = new Sort(new SortField("date", SortField.Type.STRING, true)); break; case BY_PATH: sort = new Sort(new SortField("fullpath", SortField.Type.STRING)); break; default: sort = Sort.RELEVANCE; break; } checker = new DirectSpellChecker(); } catch (ParseException e) { errorMsg = PARSE_ERROR_MSG + e.getMessage(); } catch (FileNotFoundException e) { // errorMsg = "Index database(s) not found: " + e.getMessage(); errorMsg = "Index database(s) not found."; } catch (Exception e) { errorMsg = e.getMessage(); } return this; }
From source file:com.codeReading.core.opengrok.SearchHelper.java
License:Open Source License
/** * If a search did not return a hit, one may use this method to obtain * suggestions for a new search.//from w w w.ja v a2s . c o m * * <p> Parameters which should be populated/set at this time: <ul> * <li>{@link #projects}</li> <li>{@link #dataRoot}</li> * <li>{@link #builder}</li> </ul> * * @return a possible empty list of suggestions. */ public List<Suggestion> getSuggestions() { if (projects == null) { return new ArrayList<>(0); } String name[]; if (projects.isEmpty()) { name = new String[] { "/" }; } else if (projects.size() == 1) { name = new String[] { projects.first() }; } else { name = new String[projects.size()]; int ii = 0; for (String proj : projects) { name[ii++] = proj; } } List<Suggestion> res = new ArrayList<>(); List<String> dummy = new ArrayList<>(); FSDirectory dir; IndexReader ir = null; Term t; for (int idx = 0; idx < name.length; idx++) { Suggestion s = new Suggestion(name[idx]); try { dir = FSDirectory.open(new File(indexDir, name[idx])); ir = DirectoryReader.open(dir); if (builder.getFreetext() != null && !builder.getFreetext().isEmpty()) { t = new Term(QueryBuilder.FULL, builder.getFreetext()); getSuggestion(t, ir, dummy); s.freetext = dummy.toArray(new String[dummy.size()]); dummy.clear(); } if (builder.getRefs() != null && !builder.getRefs().isEmpty()) { t = new Term(QueryBuilder.REFS, builder.getRefs()); getSuggestion(t, ir, dummy); s.refs = dummy.toArray(new String[dummy.size()]); dummy.clear(); } if (builder.getDefs() != null && !builder.getDefs().isEmpty()) { t = new Term(QueryBuilder.DEFS, builder.getDefs()); getSuggestion(t, ir, dummy); s.defs = dummy.toArray(new String[dummy.size()]); dummy.clear(); } //TODO suggest also for path and history? if ((s.freetext != null && s.freetext.length > 0) || (s.defs != null && s.defs.length > 0) || (s.refs != null && s.refs.length > 0)) { res.add(s); } } catch (IOException e) { log.log(Level.WARNING, "Got exception while getting " + "spelling suggestions: ", e); } finally { if (ir != null) { try { ir.close(); } catch (IOException ex) { log.log(Level.WARNING, "Got exception while " + "getting spelling suggestions: ", ex); } } } } return res; }
From source file:com.cohesionforce.search.EMFIndex.java
License:Open Source License
/** * Searches the index for an attribute with a matching value. * /*from w w w. j ava 2s . c o m*/ * @param feature * - the attribute to match when searching * @param value * - the value to match when searching * @return a list of search results * @throws IllegalArgumentException * if any parameters are null, or if the attribute is not marked * with the search annotation * @throws IOException * if there are issues reading the index */ public List<SearchResult> search(EStructuralFeature feature, String value) throws IllegalArgumentException, IOException { if (feature == null) { throw new IllegalArgumentException("Attribute cannot be null"); } if (value == null) { throw new IllegalArgumentException("Value cannot be null"); } List<SearchResult> rvalue = new ArrayList<SearchResult>(); boolean tokenize = false; EAnnotation annotation = feature.getEAnnotation(EMFIndexUtil.SOURCE); if (annotation != null) { if (annotation.getDetails().containsKey(EMFIndexUtil.TOKENIZE_KEY)) { tokenize = true; } } else { // Bail out early if this feature should not be indexed throw new IllegalArgumentException("Attribute is not annotated to be indexed"); } String key = EMFIndexUtil.getKey(feature); DirectoryReader reader = DirectoryReader.open(fsDir); IndexSearcher searcher = new IndexSearcher(reader); try { Query query = null; if (tokenize) { QueryParser parser = new QueryParser(version, key, analyzer); query = parser.parse(value); } else { Term term = new Term(key, value); query = new TermQuery(term); } ScoreDoc[] hits = searcher.search(query, null, MAX_SEARCH_RESULT).scoreDocs; // Iterate through the results: for (int i = 0; i < hits.length; i++) { Document hitDoc = searcher.doc(hits[i].doc); SearchResult result = new SearchResult(hitDoc); rvalue.add(result); logger.debug(hitDoc.toString()); } } catch (ParseException e) { logger.error(e.getMessage()); } finally { reader.close(); } return rvalue; }
From source file:com.cohesionforce.search.EMFIndex.java
License:Open Source License
/** * Searches the index for an attribute with a matching value and a matching * eclass//w w w .ja v a2 s .c om * * @param eclass * - the EClass to match when searching * @param attr * - the EAttribute to match when searching * @param value * - the value to match when searching * @return a list of search results * @throws IllegalArgumentException * if any parameters are null, or if the attribute is not marked * with the search annotation * @throws IOException * if there are issues reading the index */ public List<SearchResult> search(EClass eclass, EAttribute attr, String value) throws IllegalArgumentException, IOException { if (eclass == null) { throw new IllegalArgumentException("EClass cannot be null"); } if (attr == null) { throw new IllegalArgumentException("Attribute cannot be null"); } if (value == null) { throw new IllegalArgumentException("Value cannot be null"); } EAnnotation annotation = eclass.getEAnnotation(EMFIndexUtil.SOURCE); if (annotation == null) { // Bail out early if this feature should not be indexed throw new IllegalArgumentException("EClass is not annotated to be indexed"); } List<SearchResult> rvalue = new ArrayList<SearchResult>(); boolean tokenize = false; annotation = attr.getEAnnotation(EMFIndexUtil.SOURCE); if (annotation != null) { if (annotation.getDetails().containsKey(EMFIndexUtil.TOKENIZE_KEY)) { tokenize = true; } } else { // Bail out early if this feature should not be indexed throw new IllegalArgumentException("Attribute is not annotated to be indexed"); } String key = EMFIndexUtil.getKey(attr); DirectoryReader reader = DirectoryReader.open(fsDir); IndexSearcher searcher = new IndexSearcher(reader); try { BooleanQuery bquery = new BooleanQuery(); TermQuery classQuery = new TermQuery(new Term(EMFIndexUtil.ETYPE_KEY, eclass.getName())); bquery.add(classQuery, Occur.MUST); Query query = null; if (tokenize) { QueryParser parser = new QueryParser(version, key, analyzer); query = parser.parse(value); } else { Term term = new Term(key, value); query = new TermQuery(term); } bquery.add(query, Occur.MUST); ScoreDoc[] hits = searcher.search(bquery, null, MAX_SEARCH_RESULT).scoreDocs; // Iterate through the results: for (int i = 0; i < hits.length; i++) { Document hitDoc = searcher.doc(hits[i].doc); SearchResult result = new SearchResult(hitDoc); rvalue.add(result); logger.debug(hitDoc.toString()); } } catch (ParseException e) { logger.error(e.getMessage()); } finally { reader.close(); } return rvalue; }
From source file:com.cohesionforce.search.EMFIndex.java
License:Open Source License
/** * Searches the index for a matching eclass * //from w ww.j av a 2 s. co m * @param eclass * - the EClass to match when searching * @return a list of search results * @throws IllegalArgumentException * if the eclass is null or is not annotated for indexing * @throws IOException * if there are issues reading the index */ public List<SearchResult> search(EClass eclass) throws IllegalArgumentException, IOException { if (eclass == null) { throw new IllegalArgumentException("EClass cannot be null"); } EAnnotation annotation = eclass.getEAnnotation(EMFIndexUtil.SOURCE); if (annotation == null) { throw new IllegalArgumentException("EClass is not annotated for indexing"); } List<SearchResult> rvalue = new ArrayList<SearchResult>(); DirectoryReader reader = DirectoryReader.open(fsDir); IndexSearcher searcher = new IndexSearcher(reader); try { TermQuery classQuery = new TermQuery(new Term(EMFIndexUtil.ETYPE_KEY, eclass.getName())); ScoreDoc[] hits = searcher.search(classQuery, null, MAX_SEARCH_RESULT).scoreDocs; // Iterate through the results: for (int i = 0; i < hits.length; i++) { Document hitDoc = searcher.doc(hits[i].doc); SearchResult result = new SearchResult(hitDoc); rvalue.add(result); logger.debug(hitDoc.toString()); } } finally { reader.close(); } return rvalue; }
From source file:com.cohesionforce.search.EMFIndex.java
License:Open Source License
/** * Finds a document matching the object and returns the query used * /*from w w w . j ava2s .co m*/ * @param object * to search for in the index * @return the query that returns the document * @throws IllegalArgumentException * if the object is null * @throws IOException * if there are problems searching the index */ protected Query findDocument(EObject object) throws IllegalArgumentException, IOException { if (object == null) { throw new IllegalArgumentException("EObject cannot be null"); } Query rvalue = null; DirectoryReader reader = DirectoryReader.open(fsDir); IndexSearcher searcher = new IndexSearcher(reader); try { BooleanQuery bquery = new BooleanQuery(); TermQuery classQuery = new TermQuery(new Term(EMFIndexUtil.ETYPE_KEY, object.eClass().getName())); bquery.add(classQuery, Occur.MUST); TermQuery uriQuery = new TermQuery( new Term(EMFIndexUtil.DOCUMENT_URI_KEY, object.eResource().getURI().toString())); bquery.add(uriQuery, Occur.MUST); TermQuery fragmentQuery = new TermQuery( new Term(EMFIndexUtil.FRAGMENT_URI_KEY, object.eResource().getURIFragment(object))); bquery.add(fragmentQuery, Occur.MUST); ScoreDoc[] hits = searcher.search(bquery, null, 1).scoreDocs; // Iterate through the results: if (hits.length > 0) { rvalue = bquery; } } finally { reader.close(); } return rvalue; }
From source file:com.cohesionforce.search.EMFIndex.java
License:Open Source License
/** * Deletes a document matching the EObject * // ww w.ja va 2 s. c om * @param obj * @throws IllegalArgumentException * if EObject is null or the EObject is not contained in a * resource * @throws IOException * if there are issues saving the index */ public void deleteDocument(EObject obj) throws IllegalArgumentException, IOException { if (obj == null) { throw new IllegalArgumentException("EObject cannot be null"); } if (obj.eResource() == null) { throw new IllegalArgumentException("EObject must be contained in a Resource"); } Query query = findDocument(obj); if (query != null) { logger.debug("Deleting existing index for {}:{}", obj.eResource().getURI(), obj.eResource().getURIFragment(obj)); writer.deleteDocuments(query); if (!holdCommits) { writer.commit(); } } DirectoryReader reader = DirectoryReader.open(fsDir); ArrayList<String> names = new ArrayList<String>(); for (AtomicReaderContext context : reader.leaves()) { for (FieldInfo fi : context.reader().getFieldInfos()) { if (!names.contains(fi.name)) { names.add(fi.name); } } } if (names.size() > 0) { MultiFieldQueryParser parser = new MultiFieldQueryParser(version, names.toArray(new String[] {}), analyzer); try { query = parser.parse(obj.eResource().getURIFragment(obj)); IndexSearcher searcher = new IndexSearcher(reader); ScoreDoc[] hits = searcher.search(query, null, MAX_SEARCH_RESULT).scoreDocs; for (ScoreDoc hit : hits) { Document hitDoc = searcher.doc(hit.doc); logger.debug("Hanging reference in: {}", hitDoc.getField(EMFIndexUtil.DOCUMENT_URI_KEY).stringValue()); } } catch (ParseException e) { logger.error(e.getMessage()); } } }