Example usage for org.apache.lucene.index IndexWriter prepareCommit

List of usage examples for org.apache.lucene.index IndexWriter prepareCommit

Introduction

In this page you can find the example usage for org.apache.lucene.index IndexWriter prepareCommit.

Prototype

@Override
public final long prepareCommit() throws IOException 

Source Link

Document

Expert: prepare for commit.

Usage

From source file:Util.Index_Handler.java

/**
 * creates an index for the abstracts.//from  w  w  w. j av a2  s .c o m
 * @return status of operation
 */
public static String createAbstract_Index() {
    try {
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_46);
        Directory dir = FSDirectory.open(new File("Abstract_Index"));
        IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_46, analyzer);
        conf.setSimilarity(new CustomSimilarity());
        conf.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
        IndexWriter writer = new IndexWriter(dir, conf);
        Scanner scan = new Scanner(Paths.get("abstract_clean.txt"));
        String[] line;
        Document doc;
        while (scan.hasNextLine()) {
            doc = new Document();
            line = scan.nextLine().split("\\|");
            doc.add(new Field("entity", line[0], TextField.TYPE_STORED));
            doc.add(new Field("abstract", line[1], TextField.TYPE_STORED));
            writer.addDocument(doc);

        }
        writer.prepareCommit();
        writer.commit();
        writer.close();
        scan.close();
    } catch (IOException ex) {
        System.out.println("Failed to creat Index for Abstracts:" + ex.getMessage());
        return "Failed to creat Index for Abstracts:" + ex.getMessage();
    }
    return "done";
}

From source file:Util.Index_Handler.java

/**
 * Creates a block index . Is necessary for blockjoinquerys.
 * @return status of operation/*ww w .  j  a  v  a2s  .  com*/
 */
public static String createBlockIndex() {
    try {
        Scanner scan = new Scanner(Paths.get("combined.txt"));
        Scanner scan2 = new Scanner(Paths.get("entity_anchors.txt"));
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_46);
        Directory dir = FSDirectory.open(new File("Entity_Index"));
        IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_46, analyzer);
        conf.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
        conf.setSimilarity(new CustomSimilarity());
        IndexWriter writer = new IndexWriter(dir, conf);
        String[] line;//=scan.nextLine().split(" ");
        ArrayList<Document> group = new ArrayList<>();
        Map<String, String> anchorMap = new HashMap<>();
        Document doc;

        String[] anchors;
        //load anchors into ram
        while (scan2.hasNext()) {
            line = scan2.nextLine().split("\\|");
            anchorMap.put(line[0], line[1]);
        }
        line = scan.nextLine().split("\\|");
        String previous = line[0];
        anchors = line[2].split(";");
        for (String s : anchors) {
            doc = new Document();
            doc.add(new Field("anchorN", s, TextField.TYPE_STORED));
            group.add(doc);
        }
        while (scan.hasNext()) {
            line = scan.nextLine().split("\\|");
            if (line.length == 0) {
                System.out.println("found end");
                doc = new Document();
                doc.add(new Field("entity", line[0], TextField.TYPE_STORED));
                doc.add(new Field("anchor", anchorDeli(anchorMap.get(line[0])), TextField.TYPE_STORED));
                doc.add(new Field("titel", delimeterString(getEntity(line[0])), TextField.TYPE_STORED));
                //this is the marker for the parent field.
                // it needs to be a stringfield so only an exact match will hit
                // and does not need to be in the index since you dont search this field.
                doc.add(new Field("type", "Parent", StringField.TYPE_NOT_STORED));
                group.add(doc);
                writer.addDocuments(group);
                group.clear();
                break;
            }
            if (!line[0].equals(previous)) {
                doc = new Document();
                doc.add(new Field("entity", line[0], TextField.TYPE_STORED));
                doc.add(new Field("anchor", anchorDeli(anchorMap.get(line[0])), TextField.TYPE_STORED));
                doc.add(new Field("title", delimeterString(getEntity(line[0])), TextField.TYPE_STORED));
                //this is the marker for the parent field.
                // it needs to be a stringfield so only an exact match will hit
                // and does not need to be in the index since you dont search this field.
                doc.add(new Field("type", "Parent", StringField.TYPE_NOT_STORED));
                group.add(doc);
                writer.addDocuments(group);
                group.clear();
                previous = line[0];
            }
            anchors = line[2].split(";");
            for (String s : anchors) {
                doc = new Document();
                doc.add(new Field("anchorN", s, TextField.TYPE_STORED));
                group.add(doc);
            }
        }
        writer.prepareCommit();
        writer.commit();
        writer.close();
    } catch (IOException ex) {
        System.out.println("Creating Blockindex failed :" + ex.getMessage());
        return "Creating Blockindex failed :" + ex.getMessage();
    }
    return "done";
}