The constructor instantiate IndexWriter object that is used to create index. Public Indexer(String indexerDirectoryPath) throws Exception ĭocument.add(new TextField("title", title, Store.YES)) So lets first create an index of some data: ![]() Then we run the search operation on that index. To search something using Apache Lucene, we need to create an index of data. ![]() In this article we want to achieve same functioanlity using Lucene search engine library. In result Google has highlighted these terms in URL and description. Import .Notice, there are three query terms: java, inheritance and bitspedia. Our index has been created and now we can search it! LuceneDemo.java package avajava After the documents have been added to the index, the index is then optimized and then closed. If you'd like to store the contents in the index, you need to use a String rather than a Reader.Įach Document object is added to the index via the IndexWriter's addDocument() method. This is because we used a Reader as an argument to the Field constructor. The contents get tokenized and indexed, but they do not get stored in the index. The second field represents the contents of the file. This is so that the path stays whole and doesn't get chopped up by the Analyzer in the index. We also specify to not let the Analyzer tokenize the path via the _TOKENIZED. We specify to store it in the index via the argument. The first field is used to store the canonical path to each text file in the index. We create two fields and add them to the document. For each file, we create a Lucene Document object, which is a collection of fields that can represent the content, metadata, and other data related to a document. Next, we go through the files in the "filesToIndex" directory. The third argument is a boolean parameter set to true, which tells the IndexWriter to rebuild the index from scratch if it already exists. An analyzer represents the rules for extracting index terms from text. The second argument is a StandardAnalyzer object. The first argument is the directory location in the file system where the index files should be located. I used a constructor that takes three arguments. An IndexWriter object is used to create and update the index. The first thing it does is to create an index via its createIndex() method. The second text file, nicole-foods.txt, lists some foods that Nicole likes. The first one, deron-foods.txt, lists some foods that I like. Two text files in the "filesToIndex" directory will be indexed. Since Lucene is a fairly involved API, it can be a good idea to reference the Lucene source code and javadocs in your project build path, as shown here. The project utilizes that lucene-core jar W file. We have a directory called "indexDirectory". We have a directory called "filesToIndex" that contains text files that we are going to index. The demonstration project's structure is shown here. ![]() These are conceptually two different tasks. This example will both create an index and perform searches against the index. Likewise, when we create an index based on documents, we can query the index to find out what documents match our search terms. What is an index? An index is similar to an index at the back of the book, where you can look up search terms and find their corresponding pages in a book. In this tutorial, I'll create an index based on text files in a directory, and then I'll perform several searches on that index for various search terms. One good way to start becoming familiar with Lucene is to begin with a simple application. In fact, Eclipse S W uses Lucene for its great search capabilities. If you'd like to add customized search capabilities to an application, Lucene can be a great choice.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |