shnakkydoodle.searching.provider.Lucene.java Source code

Java tutorial

Introduction

Here is the source code for shnakkydoodle.searching.provider.Lucene.java

Source

/*
 * Author Stephen Booysen
 *
 * Copyright (c) 2015 Stephen Booysen, Inc. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * Stephen Booysen. ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Stephen Booysen.
 *
 * Stephen Booysen MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */
package shnakkydoodle.searching.provider;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import shnakkydoodle.searching.common.FieldData;
import shnakkydoodle.searching.common.FieldType;
import shnakkydoodle.searching.common.ISearchProvider;

/**
 * @author Stephen Booysen
 * 
 *         Our lucene provider
 * 
 */
public class Lucene implements ISearchProvider {

    // region Private Members

    // The index directory defaulted to ./searchindex
    private String indexFilePath = "./searchindex";

    // The standard analyzer
    private StandardAnalyzer analyzer;

    // The directory the index will exist in
    private Directory indexDirectory;

    /**
     * Initialise anything we need
     * 
     * @throws IOException
     */
    private void initialise() throws IOException {

        // Create the directory if it doesnt exist
        if (!new File(this.indexFilePath).exists()) {
            new File(this.indexFilePath).mkdirs();
        }

        // Instantiate the analyzer
        this.analyzer = new StandardAnalyzer();

        // Instantiate the index directory
        this.indexDirectory = FSDirectory.open(new File(indexFilePath).toPath());
    }

    /**
     * Create the document
     * 
     * @param data
     * @return
     */
    private Document createDocument(ArrayList<FieldData> data) {

        // Create the document
        Document doc = new Document();
        for (FieldData fieldData : data) {
            doc.add(new TextField(fieldData.getFieldName(), (String) fieldData.getFieldValue(), Store.YES));
        }

        // return it
        return doc;
    }

    // endregion

    // region Public Members

    /**
     * Constructor
     */
    public Lucene() {
        try {
            initialise();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * Constructor
     */
    public Lucene(String indexFilePath) {
        try {
            this.indexFilePath = indexFilePath;
            initialise();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * Add a document to the index
     * 
     * @param data
     *            : the complex object containing the
     */
    @Override
    public synchronized void addData(ArrayList<FieldData> data) {

        IndexWriter indexWriter = null;

        try {

            // set the index writing config and set our lock timeout
            IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);

            // Create the index writier
            indexWriter = new IndexWriter(this.indexDirectory, indexWriterConfig);
            indexWriter.addDocument(createDocument(data));

            indexWriter.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * Update a document
     */
    @Override
    public synchronized void updateData(String searchQuery, ArrayList<FieldData> data) {

        // delete the object
        deleteData(searchQuery);

        // add the object
        addData(data);

    }

    /**
     * Delete data
     * 
     * @param :
     *            key
     */
    @Override
    public synchronized void deleteData(String searchQuery) {
        IndexWriter indexWriter = null;

        try {
            indexWriter = new IndexWriter(this.indexDirectory, new IndexWriterConfig(analyzer));
            indexWriter.deleteDocuments(new QueryParser("", analyzer).parse(searchQuery));

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                indexWriter.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    @Override
    public ArrayList<ArrayList<FieldData>> retrieveData(String searchQuery) {

        // The data to be returned
        ArrayList<ArrayList<FieldData>> data = new ArrayList<ArrayList<FieldData>>();

        // The query
        Query query;

        try {
            query = new QueryParser("", analyzer).parse(searchQuery);

            int hitsPerPage = 999999;
            IndexReader reader = DirectoryReader.open(indexDirectory);
            IndexSearcher searcher = new IndexSearcher(reader);
            TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage);
            searcher.search(query, collector);
            ScoreDoc[] hits = collector.topDocs().scoreDocs;

            for (int i = 0; i < hits.length; ++i) {

                ArrayList<FieldData> dataItem = new ArrayList<FieldData>();
                int docId = hits[i].doc;
                Document d = searcher.doc(docId);

                for (IndexableField field : d.getFields()) {
                    try {
                        dataItem.add(new FieldData(field.name(), field.stringValue(), FieldType.eTextField));
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                data.add(dataItem);
            }

            reader.close();

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return data;
    }

    public void setindexFilePath(String indexFilePath) {
        this.indexFilePath = indexFilePath;
    }

    public String getindexFilePath() {
        return this.indexFilePath;
    }

    @Override
    public void addDataBatch(ArrayList<ArrayList<FieldData>> data) {
        // TODO Auto-generated method stub

    }

    // endregion

}