List of usage examples for org.apache.lucene.search IndexSearcher doc
public Document doc(int docID) throws IOException
.getIndexReader().document(docID)
From source file:lucandra.NumericRangeTests.java
License:Apache License
@Test public void testLongRangeLessExclusive() throws Exception { // now we'll query from the middle inclusive NumericRangeQuery query = NumericRangeQuery.newLongRange("long", null, mid, true, false); IndexReader reader = new IndexReader(indexName, connection); IndexSearcher searcher = new IndexSearcher(reader); TopDocs docs = searcher.search(query, 1000); assertEquals(1, docs.totalHits);/*from w w w. j av a 2 s .c o m*/ Set<String> results = new HashSet<String>(); for (ScoreDoc doc : docs.scoreDocs) { Document returned = searcher.doc(doc.doc); results.add(returned.get("Id")); } assertTrue(results.contains("first")); }
From source file:lucandra.NumericRangeTests.java
License:Apache License
@Test public void testLongRangeLessInclusive() throws Exception { // now we'll query from the middle inclusive NumericRangeQuery query = NumericRangeQuery.newLongRange("long", null, mid, true, true); IndexReader reader = new IndexReader(indexName, connection); IndexSearcher searcher = new IndexSearcher(reader); TopDocs docs = searcher.search(query, 1000); assertEquals(2, docs.totalHits);//from ww w . j ava 2 s .c om Set<String> results = new HashSet<String>(); for (ScoreDoc doc : docs.scoreDocs) { Document returned = searcher.doc(doc.doc); results.add(returned.get("Id")); } assertTrue(results.contains("first")); assertTrue(results.contains("second")); }
From source file:lucandra.NumericRangeTests.java
License:Apache License
@Test public void testLongRangeMinValueAll() throws Exception { // now we'll query from the middle inclusive NumericRangeQuery query = NumericRangeQuery.newLongRange("long", Long.MIN_VALUE, null, true, true); IndexReader reader = new IndexReader(indexName, connection); IndexSearcher searcher = new IndexSearcher(reader); TopDocs docs = searcher.search(query, 1000); assertEquals(3, docs.totalHits);// w ww.ja va 2s .com Set<String> results = new HashSet<String>(); for (ScoreDoc doc : docs.scoreDocs) { Document returned = searcher.doc(doc.doc); results.add(returned.get("Id")); } assertTrue(results.contains("first")); assertTrue(results.contains("second")); assertTrue(results.contains("third")); }
From source file:lucandra.NumericRangeTests.java
License:Apache License
@Test public void testLongRangeMaxAll() throws Exception { // now we'll query from the middle inclusive NumericRangeQuery query = NumericRangeQuery.newLongRange("long", null, Long.MAX_VALUE, true, true); IndexReader reader = new IndexReader(indexName, connection); IndexSearcher searcher = new IndexSearcher(reader); TopDocs docs = searcher.search(query, 1000); assertEquals(3, docs.totalHits);//from w w w . j av a2 s. co m Set<String> results = new HashSet<String>(); for (ScoreDoc doc : docs.scoreDocs) { Document returned = searcher.doc(doc.doc); results.add(returned.get("Id")); } assertTrue(results.contains("first")); assertTrue(results.contains("second")); assertTrue(results.contains("third")); }
From source file:lucandra.NumericRangeTests.java
License:Apache License
@Test public void testLongRangeZeroAll() throws Exception { // now we'll query from the middle inclusive NumericRangeQuery query = NumericRangeQuery.newLongRange("long", 1L, null, true, true); IndexReader reader = new IndexReader(indexName, connection); IndexSearcher searcher = new IndexSearcher(reader); TopDocs docs = searcher.search(query, 1000); assertEquals(3, docs.totalHits);/*from ww w. j av a 2 s .c om*/ Set<String> results = new HashSet<String>(); for (ScoreDoc doc : docs.scoreDocs) { Document returned = searcher.doc(doc.doc); results.add(returned.get("Id")); } assertTrue(results.contains("first")); assertTrue(results.contains("second")); assertTrue(results.contains("third")); }
From source file:lucene.Access.java
License:Apache License
public static void read(Directory dir, String qs) throws Exception { Analyzer analyzer = new KeywordAnalyzer(); IndexReader indexReader = DirectoryReader.open(dir); IndexSearcher indexSearcher = new IndexSearcher(indexReader); QueryParser queryParser = new QueryParser(Version.LUCENE_46, "uri", analyzer); queryParser.setAllowLeadingWildcard(true); org.apache.lucene.search.Query query = queryParser.parse(qs); // There are 19M transaction records. int limit = 50 * 1000 * 1000; if (limit <= 0) limit = 100000;//from ww w.ja va 2s.c om ScoreDoc[] sDocs = indexSearcher.search(query, limit).scoreDocs; // if ( sDocs.length == 0 ) // log.info("No hits"); for (ScoreDoc sd : sDocs) { Document doc2 = indexSearcher.doc(sd.doc); //String[] values = doc2.getValues("uri") ; log.info(doc2.get("uri") + " " + doc2.get("transactionDate") + " " + doc2.get("pricePaid")); } }
From source file:lucene.demo.search.FileSearcher.java
License:Apache License
private void removeReviews(IndexSearcher searcher, Query query, ScoreDoc[] hits) throws IOException { for (int i = 0; i < hits.length; i++) { Document doc = searcher.doc(hits[i].doc); Integer rating = Integer.parseInt(doc.get("rating")); Integer hotelID = Integer.parseInt(doc.get("hotelID")); Hotel hotel = hotels.getHotel(hotelID); hotel.removeReview(rating);//from w w w . ja va 2s.c om } removeDocs(query); }
From source file:lucene.lab.ReaderClass.java
public List<Album> search(String consulta) throws ParseException, IOException { Path indexPath = Paths.get("C:\\index\\"); System.out.println("Entr! fuck"); //File indexPath = new File("C:\\index\\"); Directory dir = FSDirectory.open(indexPath); IndexReader reader = DirectoryReader.open(dir); IndexSearcher searcher = new IndexSearcher(reader); PerFieldAnalyzerWrapper analyzerWrapper = this.createAnalyzer(); Analyzer analyzer = new WhitespaceAnalyzer(); String[] fields = { "title", "titleMin", "artist", "artistMin", "text", "summary", "tags", "tagsMin" }; //QueryParser parser = new QueryParser(field, analyzerWrapper); MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, analyzerWrapper); Query query = parser.parse(consulta); int hitsPerPage = 100; //IndexReader reader = IndexReader.open(index); //IndexSearcher searcher = new IndexSearcher(reader); TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage); searcher.search(query, collector);/*w w w . jav a 2s. co m*/ ScoreDoc[] hits = collector.topDocs().scoreDocs; int max_cant_comentarios = 0; List<Integer> lista_cant_comentarios = new ArrayList<>(); List<Album> lista_album = new ArrayList<>(); System.out.println("Encontr " + hits.length + " hits."); if (hits.length > 0) { float maxLuceneScore = hits[0].score; for (int i = 0; i < hits.length; ++i) { int docId = hits[i].doc; Document d = searcher.doc(docId); Album album = new Album(); album.d = d; album.luceneScore = hits[i].score; lista_cant_comentarios.add(album.cant_comentarios()); lista_album.add(album); } Collections.sort(lista_cant_comentarios); max_cant_comentarios = lista_cant_comentarios.get(lista_cant_comentarios.size() - 1); for (Album album : lista_album) { album.formulaRanking(maxLuceneScore, max_cant_comentarios); album.title = album.d.get("title"); album.artist = album.d.get("artist"); //System.out.println(album.score); } Collections.sort(lista_album); int i = 0; for (Album album : lista_album) { System.out.println((i + 1) + ". Ttulo: " + album.d.get("title") + "\t" + "Artista: " + album.d.get("artist") + "\t" + "Score" + album.score); i++; } } return lista_album; }
From source file:lucene.searchengine.LuceneSearchEngine.java
public static void main(String[] args) throws IOException { System.out.println(//from w w w .ja v a 2 s. com "Enter the FULL path where the index will be created: (e.g. /Usr/index or c:\\temp\\index)"); String indexLocation = null; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s = br.readLine(); LuceneSearchEngine indexer = null; try { indexLocation = s; indexer = new LuceneSearchEngine(s); } catch (Exception ex) { System.out.println("Cannot create index..." + ex.getMessage()); System.exit(-1); } // =================================================== // read input from user until he enters q for quit // =================================================== while (!s.equalsIgnoreCase("q")) { try { System.out.println( "Enter the FULL path to add into the index (q=quit): (e.g. /home/mydir/docs or c:\\Users\\mydir\\docs)"); System.out.println("[Acceptable file types: .xml, .html, .html, .txt]"); s = br.readLine(); if (s.equalsIgnoreCase("q")) { break; } // try to add file into the index indexer.indexFileOrDirectory(s); } catch (Exception e) { System.out.println("Error indexing " + s + " : " + e.getMessage()); } } // =================================================== // after adding, we always have to call the // closeIndex, otherwise the index is not created // =================================================== indexer.closeIndex(); // ========================================================= // Now search // ========================================================= IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(indexLocation))); //=========================================================== // GET Term frequency //=========================================================== // Creating a output file to store the term,term_frequency pairs. PrintWriter tfwriter = new PrintWriter("..\\term-frequency.csv"); Fields fields = MultiFields.getFields(reader); HashMap<String, Long> tfmap = new HashMap<String, Long>(); Terms terms = fields.terms("contents"); TermsEnum termsEnum = terms.iterator(null); BytesRef bref = null; while ((bref = termsEnum.next()) != null) { String term_name = new String(bref.bytes, bref.offset, bref.length); Term term_instance = new Term("contents", term_name); long termFrequency = reader.totalTermFreq(term_instance); tfmap.put(term_name, termFrequency); } System.out.println(tfmap.size()); for (String key : tfmap.keySet()) { tfwriter.write(key + "," + tfmap.get(key)); tfwriter.write("\n"); } tfwriter.close(); //==================================================================== // Code END to fetch term frequency //==================================================================== IndexSearcher searcher = new IndexSearcher(reader); s = ""; while (!s.equalsIgnoreCase("q")) { TopScoreDocCollector collector = TopScoreDocCollector.create(100, true); try { System.out.println("Enter the search query (q=quit):"); s = br.readLine(); if (s.equalsIgnoreCase("q")) { break; } Query q = new QueryParser(Version.LUCENE_47, "contents", sAnalyzer).parse(s); searcher.search(q, collector); ScoreDoc[] hits = collector.topDocs().scoreDocs; // 4. display results System.out.println("Found " + hits.length + " hits."); for (int i = 0; i < hits.length; ++i) { int docId = hits[i].doc; Document d = searcher.doc(docId); System.out.println((i + 1) + ". " + d.get("filename") + " score=" + hits[i].score); } // 5. term stats --> watch out for which "version" of the term // must be checked here instead! Term termInstance = new Term("contents", s); long termFreq = reader.totalTermFreq(termInstance); long docCount = reader.docFreq(termInstance); System.out.println(s + " Term Frequency " + termFreq + " - Document Frequency " + docCount); } catch (Exception e) { System.out.println("Error searching " + s + " : " + e.getMessage()); break; } } }
From source file:lucene.security.index.SecureAtomicReaderTestBase.java
License:Apache License
@Test public void testQuery() throws IOException, ParseException { SecureAtomicReader secureReader = getSecureReader(); QueryParser parser = new QueryParser(Version.LUCENE_43, "nothing", new KeywordAnalyzer()); Query query = parser.parse("test:test"); IndexSearcher searcher = new IndexSearcher(secureReader); TopDocs topDocs = searcher.search(query, 10); assertEquals(3, topDocs.totalHits);/*from ww w. java 2 s. com*/ { int doc = topDocs.scoreDocs[0].doc; assertEquals(0, doc); Document document = searcher.doc(doc); assertEquals("test", document.get("test")); assertEquals("info", document.get("info")); } { int doc = topDocs.scoreDocs[1].doc; assertEquals(1, doc); Document document = searcher.doc(doc); assertNull(document.get("test")); assertEquals("info", document.get("info")); } { int doc = topDocs.scoreDocs[2].doc; assertEquals(2, doc); Document document = searcher.doc(doc); assertEquals("test", document.get("test")); assertEquals("info", document.get("info")); } secureReader.close(); }