Java tutorial
/******************************************************************************* * Copyright 2013 Matias Surdi * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express * or implied. See the License for the specific language governing permissions and limitations under * the License. ******************************************************************************/ package io.github.msurdi.redeye.core.lucene; import io.github.msurdi.redeye.api.Entry; import io.github.msurdi.redeye.api.Indexable; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.StringField; import org.apache.lucene.document.TextField; import org.apache.lucene.index.IndexableField; import org.apache.lucene.store.RAMDirectory; import org.apache.lucene.store.SimpleFSDirectory; import java.io.File; import java.io.IOException; /** * Entry indexing implementation */ public class EntryIndex extends AbstractIndex<Entry> { /** * Creates a new in-memory lucene */ public EntryIndex() { super(new RAMDirectory()); } /** * Creates a new filesystem based lucene * * @param path the path to the lucene * @throws IOException if the lucene creation operation fails */ public EntryIndex(File path) throws IOException { super(new SimpleFSDirectory(path)); } @Override protected Entry buildEntity(Document document) { Entry entry = Entry.builder().id(document.get(Indexable.ID_FIELD)).build(); for (IndexableField field : document.getFields()) { if (!field.name().startsWith(Indexable.PRIVATE_PREFIX)) { entry.getValues().set(field.name(), field.stringValue()); } } return entry; } @Override protected Document buildDocument(String id, Entry entry) { Document doc = new Document(); doc.add(new StringField(Indexable.ID_FIELD, id, Field.Store.YES)); doc.add(new TextField(DEFAULT_FIELD, entry.getDefault(), Field.Store.NO)); for (String name : entry.getValues().getKeys()) { doc.add(new TextField(name, entry.getValues().get(name), Field.Store.YES)); } return doc; } }