从最简单的开始,show me the code,,,(在lucene自带的文档中).
这个自带的例子很清晰,索引文档,查询等,继续学习
java 代码
- Analyzer analyzer = new StandardAnalyzer();
- // Store the index in memory:
- Directory directory = new RAMDirectory();
- // To store an index on disk, use this instead:
- //Directory directory = FSDirectory.getDirectory("/tmp/testindex");
- IndexWriter iwriter = new IndexWriter(directory, analyzer, true);
- iwriter.setMaxFieldLength(25000);
- Document doc = new Document();
- String text = "This is the text to be indexed.";
- doc.add(new Field("fieldname", text, Field.Store.YES,
- Field.Index.TOKENIZED));
- iwriter.addDocument(doc);
- iwriter.optimize();
- iwriter.close();
- // Now search the index:
- IndexSearcher isearcher = new IndexSearcher(directory);
- // Parse a simple query that searches for "text":
- QueryParser parser = new QueryParser("fieldname", analyzer);
- Query query = parser.parse("text");
- Hits hits = isearcher.search(query);
- assertEquals(1, hits.length());
- // Iterate through the results:
- for (int i = 0; i < hits.length(); i++) {
- Document hitDoc = hits.doc(i);
- assertEquals("This is the text to be indexed.", hitDoc.get("fieldname"));
- }
- isearcher.close();
- directory.close();
这个自带的例子很清晰,索引文档,查询等,继续学习