Query firstNameQuery = qp. QueryParser qp = new QueryParser("firstName", new StandardAnalyzer()) a Python version of the same algorithms (). Private static TopDocs searchByFirstName(String firstName, IndexSearcher searcher) throws Exception including both the Java source code in the book (using Apache Lucene and. ("Total Results :: " + foundDocs2.totalHits) TopDocs foundDocs2 = searchByFirstName("Brian", searcher) ("Total Results :: " + foundDocs.totalHits) QueryParser qp = new QueryParser("id", new StandardAnalyzer()) Private static TopDocs searchById(Integer id, IndexSearcher searcher) throws Exception TopDocs foundDocs = searchById(1, searcher) IndexSearcher searcher = createSearcher() ach(Query) returns .TopDocs which represents the query result. To search lucene documents, you need to create .Query instance using .classic.QueryParser class. IndexSearcher searcher = new IndexSearcher(reader) IndexReader reader = DirectoryReader.open(dir) Private static IndexSearcher createSearcher() throws IOExceptionĭirectory dir = FSDirectory.open(Paths.get(INDEX_DIR)) It takes one argument Directory, which points to index folder. IndexSearcher is used to search lucene documents from indexes. Lucene Search Example Create IndexSearcher Once you execute above code in your computer, you will see lucene indexes created in configured folder path. Public static void main(String args) throws Exceptionĭocument document1 = createDocument(1, "Lokesh", "Gupta", "") ĭocument document2 = createDocument(2, "Brian", "Schultz", "") Private static final String INDEX_DIR = "c:/temp/lucene6index" To write lucene documents to index, use IndexWriter.addDocuments(documents) method. Private static Document createDocument(Integer id, String firstName, String lastName, String website)ĭocument.add(new StringField("id", id.toString(), )) ĭocument.add(new TextField("firstName", firstName, )) ĭocument.add(new TextField("lastName", lastName, )) ĭocument.add(new TextField("website", website, )) Document class represent Lucene indexed document. IndexWriter writer = new IndexWriter(dir, config) IndexWriterConfig config = new IndexWriterConfig(new StandardAnalyzer()) Private static IndexWriter createWriter() throws IOExceptionįSDirectory dir = FSDirectory.open(Paths.get(INDEX_DIR)) Please note that after the writer is created, the given configuration instance cannot be passed to another writer. It’s constructor takes two arguments: FSDirectory and IndexWriterConfig. IndexWriter class provides functionality to create and manage index. Lucene Write Index Example Create IndexWriter Once you create maven project in eclipse, include following lucene dependencies in pom.xml. Lucene Maven Dependency Lucene Write Index Example Lucene Search Example Download Sourcecode Lucene is used by many different modern search platforms, such as Apache Solr and ElasticSearch, or crawling platforms, such as Apache Nutch for data indexing and searching. Learn to use Apache Lucene 6 to index and search documents.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |