Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is how to make a simple Index for text searches using Lucene 4.6</p> <pre><code>public void indexFilmTitle() { try { Directory dir = FSDirectory.open(new File(AppConstants.INDEX_DIR)); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_46); IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_46, analyzer); iwc.setOpenMode(OpenMode.CREATE); IndexWriter writer = new IndexWriter(dir, iwc); String sql = "SELECT * FROM sakila.film_text"; PreparedStatement ps = con.prepareStatement(sql); ResultSet rs = ps.executeQuery(); int i=0; while (rs.next()) { i++; Document doc = new Document(); Field id = new IntField("id", rs.getInt(1), Field.Store.YES); doc.add(id); Field title = new TextField("title", rs.getString(2), Field.Store.YES); doc.add(title); writer.addDocument(doc); } writer.close(); } catch (IOException ex) { Logger.getLogger(IndexManager.class.getName()).log(Level.SEVERE, null, ex); } catch (SQLException ex) { Logger.getLogger(IndexManager.class.getName()).log(Level.SEVERE, null, ex); } } </code></pre> <p>Note that you have to TextField.</p> <p>And here is how to search:</p> <pre><code>public void searchFromFilm_Text(String keyword) { try { IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(AppConstants.INDEX_DIR))); IndexSearcher searcher = new IndexSearcher(reader); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_46); QueryParser parser = new QueryParser(Version.LUCENE_46, "title", analyzer); Query query = parser.parse(keyword); System.out.println("Searching for: " + query.toString("title")); TopDocs results = searcher.search(query, 100); ScoreDoc[] hits = results.scoreDocs; System.out.println(hits.length); for(ScoreDoc sdoc : hits) { Document doc = searcher.doc(sdoc.doc); System.out.println(doc.get("id")); System.out.println(doc.get("title")); } } catch (IOException ex) { Logger.getLogger(SearchManager.class.getName()).log(Level.SEVERE, null, ex); } catch (ParseException ex) { Logger.getLogger(SearchManager.class.getName()).log(Level.SEVERE, null, ex); } } </code></pre> <p>Hope it helps.....</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload