List of usage examples for org.apache.lucene.index DirectoryReader open
public static DirectoryReader open(final IndexCommit commit) throws IOException
From source file:com.searchcode.app.service.TimeCodeSearcher.java
public List<String> getRepoDocuments(String repoName) { List<String> fileLocations = new ArrayList<>(); try {//from ww w . j a v a2s . c o m IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(this.INDEXPATH))); IndexSearcher searcher = new IndexSearcher(reader); Analyzer analyzer = new CodeAnalyzer(); QueryParser parser = new QueryParser(CODEFIELD, analyzer); Query query = parser.parse(Values.REPONAME + ":" + repoName); TopDocs results = searcher.search(query, Integer.MAX_VALUE); ScoreDoc[] hits = results.scoreDocs; for (int i = 0; i < hits.length; i++) { Document doc = searcher.doc(hits[i].doc); fileLocations.add(doc.get(Values.FILELOCATIONFILENAME)); } reader.close(); } catch (Exception ex) { LOGGER.severe(" caught a " + ex.getClass() + "\n with message: " + ex.getMessage()); } return fileLocations; }
From source file:com.sg.business.vault.index.demo.SearchFiles.java
License:Apache License
/** Simple command-line based search demo. */ public static void main(String[] args) throws Exception { String usage = "Usage:\tjava org.apache.lucene.demo.SearchFiles [-index dir] [-field f] [-repeat n] [-queries file] [-query string] [-raw] [-paging hitsPerPage]\n\nSee http://lucene.apache.org/core/4_1_0/demo/ for details."; //$NON-NLS-1$ if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) { //$NON-NLS-1$ //$NON-NLS-2$ System.out.println(usage); System.exit(0);/* w w w. j ava2 s .c o m*/ } String index = "index"; //$NON-NLS-1$ String field = "contents"; //$NON-NLS-1$ String queries = null; int repeat = 0; boolean raw = false; String queryString = null; int hitsPerPage = 10; for (int i = 0; i < args.length; i++) { if ("-index".equals(args[i])) { //$NON-NLS-1$ index = args[i + 1]; i++; } else if ("-field".equals(args[i])) { //$NON-NLS-1$ field = args[i + 1]; i++; } else if ("-queries".equals(args[i])) { //$NON-NLS-1$ queries = args[i + 1]; i++; } else if ("-query".equals(args[i])) { //$NON-NLS-1$ queryString = args[i + 1]; i++; } else if ("-repeat".equals(args[i])) { //$NON-NLS-1$ repeat = Integer.parseInt(args[i + 1]); i++; } else if ("-raw".equals(args[i])) { //$NON-NLS-1$ raw = true; } else if ("-paging".equals(args[i])) { //$NON-NLS-1$ hitsPerPage = Integer.parseInt(args[i + 1]); if (hitsPerPage <= 0) { System.err.println("There must be at least 1 hit per page."); //$NON-NLS-1$ System.exit(1); } i++; } } IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(index))); IndexSearcher searcher = new IndexSearcher(reader); Analyzer analyzer = new SmartChineseAnalyzer(Version.LUCENE_44); BufferedReader in = null; if (queries != null) { in = new BufferedReader(new InputStreamReader(new FileInputStream(queries), "UTF-8")); //$NON-NLS-1$ } else { in = new BufferedReader(new InputStreamReader(System.in, "UTF-8")); //$NON-NLS-1$ } QueryParser parser = new QueryParser(Version.LUCENE_44, field, analyzer); while (true) { if (queries == null && queryString == null) { // prompt the user System.out.println("Enter query: "); //$NON-NLS-1$ } String line = queryString != null ? queryString : in.readLine(); if (line == null || line.length() == -1) { break; } line = line.trim(); if (line.length() == 0) { break; } Query query = parser.parse(line); System.out.println("Searching for: " + query.toString(field)); //$NON-NLS-1$ if (repeat > 0) { // repeat & time as benchmark Date start = new Date(); for (int i = 0; i < repeat; i++) { searcher.search(query, null, 100); } Date end = new Date(); System.out.println("Time: " + (end.getTime() - start.getTime()) //$NON-NLS-1$ + "ms"); //$NON-NLS-1$ } doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null && queryString == null); if (queryString != null) { break; } } reader.close(); }
From source file:com.shaie.annots.AnnotationSearchExample.java
License:Apache License
public static void main(String[] args) throws Exception { Directory dir = new RAMDirectory(); IndexWriterConfig conf = new IndexWriterConfig(new WhitespaceAnalyzer()); IndexWriter writer = new IndexWriter(dir, conf); // we need to add the annotation as a TokenStream field, therefore cannot use an Analyzer passed in the // IndexWriterConfig. Tokenizer tokenizer = new WhitespaceTokenizer(); tokenizer.setReader(new StringReader("quick brown fox ate the blue red chicken")); TeeSinkTokenFilter textStream = new TeeSinkTokenFilter(tokenizer); TokenStream colorAnnotationStream = new AnnotatingTokenFilter( textStream.newSinkTokenStream(new ColorsSinkFilter()), COLOR_ANNOT_TERM); Document doc = new Document(); doc.add(new TextField("text", textStream)); doc.add(new TextField("annot", colorAnnotationStream)); writer.addDocument(doc);//w w w .j ava2 s . c o m writer.close(); DirectoryReader reader = DirectoryReader.open(dir); LeafReader ar = reader.leaves().get(0).reader(); // we only have one segment printFieldTerms(ar, "text"); System.out.println(); final ByteArrayDataInput in = new ByteArrayDataInput(); PostingsEnum dape = ar.postings(new Term("annot", COLOR_ANNOT_TERM)); int docID = dape.nextDoc(); int freq = dape.freq(); System.out.println("Color annotation spans: doc=" + docID + ", freq=" + freq); for (int i = 0; i < freq; i++) { dape.nextPosition(); BytesRef payload = dape.getPayload(); in.reset(payload.bytes, payload.offset, payload.length); System.out.println(" start=" + in.readVInt() + ", length=" + in.readVInt()); } IndexSearcher searcher = new IndexSearcher(reader); System.out.println("\nsearching for 'red WITHIN color':"); Query q = new SpanWithinQuery(new SpanAnnotationTermQuery(new Term("annot", COLOR_ANNOT_TERM)), new SpanInclusivePositionTermQuery(new Term("text", "red"))); TopDocs td = searcher.search(q, 10); System.out.println(" num results: " + td.scoreDocs.length); System.out.println("\nsearching for 'ate WITHIN color':"); q = new SpanWithinQuery(new SpanAnnotationTermQuery(new Term("annot", COLOR_ANNOT_TERM)), new SpanInclusivePositionTermQuery(new Term("text", "ate"))); td = searcher.search(q, 10); System.out.println(" num results: " + td.scoreDocs.length); reader.close(); dir.close(); }
From source file:com.shaie.annots.example.AnnotatorAnyExample.java
License:Apache License
@SuppressWarnings("resource") public static void main(String[] args) throws Exception { final Directory dir = new RAMDirectory(); final Analyzer analyzer = new WhitespaceAnalyzer(); final IndexWriterConfig conf = new IndexWriterConfig(analyzer); final IndexWriter writer = new IndexWriter(dir, conf); addDocument(writer, "brown fox and a red dog"); addDocument(writer, "only red dog"); addDocument(writer, "no red animals here"); writer.close();/*from w w w . ja v a 2s . c o m*/ final QueryParser qp = new QueryParser(TEXT_FIELD, analyzer); qp.setAllowLeadingWildcard(true); final DirectoryReader reader = DirectoryReader.open(dir); final LeafReader leaf = reader.leaves().get(0).reader(); // We only have one segment IndexUtils.printFieldTerms(leaf, TEXT_FIELD, COLOR_FIELD, ANIMAL_FIELD); IndexUtils.printFieldTermsWithInfo(leaf, COLOR_FIELD, ANIMAL_FIELD); System.out.println(); final IndexSearcher searcher = new IndexSearcher(reader); search(searcher, qp.parse("animal:" + AnyAnnotationTokenFilter.ANY_ANNOTATION_TERM + " AND color:" + AnyAnnotationTokenFilter.ANY_ANNOTATION_TERM)); System.out.println(); search(searcher, qp.parse("animal:" + AnyAnnotationTokenFilter.ANY_ANNOTATION_TERM + " AND color:red")); System.out.println(); searchForRedAnimal(searcher); System.out.println(); reader.close(); }
From source file:com.shaie.annots.example.AnnotatorTeeSinkFilterExample.java
License:Apache License
@SuppressWarnings("resource") public static void main(String[] args) throws Exception { final Directory dir = new RAMDirectory(); final Analyzer analyzer = new WhitespaceAnalyzer(); final IndexWriterConfig conf = new IndexWriterConfig(analyzer); final IndexWriter writer = new IndexWriter(dir, conf); addDocument(writer, "brown fox and a red dog"); addDocument(writer, "only red dog"); addDocument(writer, "no red animals here"); writer.close();//from w w w.j a v a 2 s. c o m final QueryParser qp = new QueryParser(TEXT_FIELD, analyzer); qp.setAllowLeadingWildcard(true); final DirectoryReader reader = DirectoryReader.open(dir); final LeafReader leaf = reader.leaves().get(0).reader(); // We only have one segment IndexUtils.printFieldTerms(leaf, TEXT_FIELD, COLOR_FIELD, ANIMAL_FIELD); IndexUtils.printFieldTermsWithInfo(leaf, COLOR_FIELD, ANIMAL_FIELD); final IndexSearcher searcher = new IndexSearcher(reader); search(searcher, qp.parse("color:red")); System.out.println(); search(searcher, qp.parse("animal:fox")); System.out.println(); searchForBrownFox(searcher); System.out.println(); search(searcher, qp.parse("animal:* AND color:*")); System.out.println(); search(searcher, qp.parse("animal:* AND color:red")); System.out.println(); reader.close(); }
From source file:com.shaie.annots.example.AnnotatorTokenFilterExample.java
License:Apache License
@SuppressWarnings("resource") public static void main(String[] args) throws Exception { final Directory dir = new RAMDirectory(); final Analyzer analyzer = createAnalyzer(); final IndexWriterConfig conf = new IndexWriterConfig(analyzer); final IndexWriter writer = new IndexWriter(dir, conf); addDocument(writer, "brown fox and a red dog"); addDocument(writer, "only red dog"); addDocument(writer, "no red animals here"); writer.close();//from w w w. ja v a2 s .c om final QueryParser qp = new QueryParser(TEXT_FIELD, analyzer); qp.setAllowLeadingWildcard(true); final DirectoryReader reader = DirectoryReader.open(dir); final LeafReader leaf = reader.leaves().get(0).reader(); // We only have one segment IndexUtils.printFieldTerms(leaf, TEXT_FIELD, COLOR_FIELD, ANIMAL_FIELD); IndexUtils.printFieldTermsWithInfo(leaf, COLOR_FIELD, ANIMAL_FIELD); final IndexSearcher searcher = new IndexSearcher(reader); search(searcher, qp.parse("color:red")); System.out.println(); search(searcher, qp.parse("animal:fox")); System.out.println(); searchForBrownFox(searcher); System.out.println(); search(searcher, qp.parse("animal:* AND color:*")); System.out.println(); search(searcher, qp.parse("animal:* AND color:red")); System.out.println(); reader.close(); }
From source file:com.shaie.annots.example.PreAnnotatedTokenFilterExample.java
License:Apache License
@SuppressWarnings("resource") public static void main(String[] args) throws Exception { final Directory dir = new RAMDirectory(); final Analyzer analyzer = new WhitespaceAnalyzer(); final IndexWriterConfig conf = new IndexWriterConfig(analyzer); final IndexWriter writer = new IndexWriter(dir, conf); addDocument(writer, "quick rosy brown fox and a pale violet red dog", 1, 2, 2, 1, 6, 3, 7, 1, 8, 1); addDocument(writer, "only red dog", 1, 1); addDocument(writer, "man with red pale face", 2, 1); writer.close();/*from w w w. j av a 2 s . c o m*/ final QueryParser qp = new QueryParser(TEXT_FIELD, analyzer); qp.setAllowLeadingWildcard(true); final DirectoryReader reader = DirectoryReader.open(dir); final LeafReader leaf = reader.leaves().get(0).reader(); // We only have one segment IndexUtils.printFieldTerms(leaf, TEXT_FIELD, COLOR_FIELD); IndexUtils.printFieldTermsWithInfo(leaf, COLOR_FIELD); System.out.println(); final IndexSearcher searcher = new IndexSearcher(reader); search(searcher, qp.parse("color:" + ANY_ANNOTATION_TERM)); System.out.println(); search(searcher, qp.parse("color:pale")); System.out.println(); searchForColoredFox(searcher); System.out.println(); reader.close(); }
From source file:com.shaie.facet.NotDrillDownExample.java
License:Apache License
public static void main(String[] args) throws Exception { createIndex();//from w w w . ja v a2 s.co m try (DirectoryReader indexReader = DirectoryReader.open(indexDir); TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);) { final IndexSearcher searcher = new IndexSearcher(indexReader); // Find the index field which holds the 'Author' facets final String indexedField = config.getDimConfig(AUTHOR_FACET).indexFieldName; final Query q = new BooleanQuery.Builder() // Here you would usually use a different query .add(new MatchAllDocsQuery(), Occur.MUST) // Exclude results with Author/Lisa .add(new TermQuery(DrillDownQuery.term(indexedField, AUTHOR_FACET, "Lisa")), Occur.MUST_NOT) .build(); final TopDocs topDocs = searcher.search(q, 10); assert topDocs.scoreDocs.length == 1 : "should have found 1 document with Author/Bob"; final Document doc = searcher.doc(topDocs.scoreDocs[0].doc); System.out.println(doc); } }
From source file:com.shaie.PhraseVsSpanQuery.java
License:Apache License
@SuppressWarnings("resource") public static void main(String[] args) throws Exception { final Directory dir = new RAMDirectory(); final IndexWriterConfig conf = new IndexWriterConfig(new WhitespaceAnalyzer()); final IndexWriter writer = new IndexWriter(dir, conf); final Document doc = new Document(); doc.add(new TextField("f", new TokenStream() { final PositionIncrementAttribute pos = addAttribute(PositionIncrementAttribute.class); final CharTermAttribute term = addAttribute(CharTermAttribute.class); boolean first = true, done = false; @Override/*from w w w . j a va2 s . c o m*/ public boolean incrementToken() throws IOException { if (done) { return false; } if (first) { term.setEmpty().append("a"); pos.setPositionIncrement(1); first = false; } else { term.setEmpty().append("b"); pos.setPositionIncrement(0); done = true; } return true; } })); writer.addDocument(doc); writer.close(); final DirectoryReader reader = DirectoryReader.open(dir); final IndexSearcher searcher = new IndexSearcher(reader); final LeafReader ar = reader.leaves().get(0).reader(); final TermsEnum te = ar.terms("f").iterator(); BytesRef scratch = new BytesRef(); while ((scratch = te.next()) != null) { System.out.println(scratch.utf8ToString()); final PostingsEnum dape = ar.postings(new Term("f", scratch.utf8ToString())); System.out.println(" doc=" + dape.nextDoc() + ", pos=" + dape.nextPosition()); } System.out.println(); // try a phrase query with a slop final PhraseQuery pqNoSlop = buildPhraseQuery(0); System.out.println("searching for \"a b\"; num results = " + searcher.search(pqNoSlop, 10).totalHits); final PhraseQuery pqSlop1 = buildPhraseQuery(1); System.out.println("searching for \"a b\"~1; num results = " + searcher.search(pqSlop1, 10).totalHits); final PhraseQuery pqSlop3 = buildPhraseQuery(3); System.out.println("searching for \"a b\"~3; num results = " + searcher.search(pqSlop3, 10).totalHits); final SpanNearQuery snqUnOrdered = new SpanNearQuery( new SpanQuery[] { new SpanTermQuery(new Term("f", "a")), new SpanTermQuery(new Term("f", "b")) }, 1, false); System.out.println("searching for SpanNearUnordered('a', 'b'), slop=1; num results = " + searcher.search(snqUnOrdered, 10).totalHits); final SpanNearQuery snqOrdered = new SpanNearQuery( new SpanQuery[] { new SpanTermQuery(new Term("f", "a")), new SpanTermQuery(new Term("f", "b")) }, 1, true); System.out.println("searching for SpanNearOrdered('a', 'b'), slop=1; num results = " + searcher.search(snqOrdered, 10).totalHits); reader.close(); }
From source file:com.shaie.suggest.ContextSuggestDemo.java
License:Apache License
private void buildSuggesterIndex() throws IOException { try (DirectoryReader reader = DirectoryReader.open(indexDir)) { final Dictionary dictionary = new DocumentDictionary(reader, "content", null, null, "username"); suggester.build(dictionary);/*w w w.ja v a 2 s . c o m*/ suggester.refresh(); } }