Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use Lucene IndexReader to read index in version 4.4?
    primarykey
    data
    text
    <p>For the just the sake of learning I've created an index from 1 file and wanted to search it. I am using Lucene Version 4.4. I know that indexing part is true. </p> <p>tempFileName is the name of file which contains tokens and this file has the following words : </p> <p>"odd plus odd is even ## even plus even is even ## odd plus even is odd ##"</p> <p>However when I provide a query it returns nothing. I can't see what would be the problem. Any help is greatly appreciated.</p> <p>Indexing part : </p> <pre><code>public void startIndexingDocument(String indexPath) throws IOException { Analyzer analyzer = new WhitespaceAnalyzer(Version.LUCENE_44); SimpleFSDirectory directory = new SimpleFSDirectory(new File(indexPath)); IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_44, analyzer); IndexWriter writer = new IndexWriter(directory, config); indexDocs(writer); writer.close(); } private void indexDocs(IndexWriter w) throws IOException { Document doc = new Document(); File file = new File(tempFileName); BufferedReader br = new BufferedReader(new FileReader(tempFileName)); Field field = new StringField(fieldName, br.readLine().toString(), Field.Store.YES); doc.add(field); w.addDocument(doc); } </code></pre> <p>Searching part :</p> <pre><code>public void readFromIndex(String indexPath) throws IOException, ParseException { Analyzer anal = new WhitespaceAnalyzer(Version.LUCENE_44); QueryParser parser = new QueryParser(Version.LUCENE_44, fieldName, anal); Query query = parser.parse("odd"); IndexReader reader = IndexReader.open(NIOFSDirectory.open(new File( indexPath))); IndexSearcher searcher = new IndexSearcher(reader); TopScoreDocCollector collector = TopScoreDocCollector.create(10, true); searcher.search(query, collector); ScoreDoc[] hits = collector.topDocs().scoreDocs; // display System.out.println("fieldName =" + fieldName); System.out.println("Found : " + hits.length + " hits."); for (int i = 0; i &lt; hits.length; i++) { int docId = hits[i].doc; Document d = searcher.doc(docId); System.out.println((i + 1) + ". " + d.get(fieldName)); } reader.close(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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