Micha Kops avatar Micha Kops committed 0b01c00

initial import

Comments (0)

Files changed (4)

+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" output="target/classes" path="src/main/java"/>
+	<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
+	<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
+	<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
+	<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
+	<classpathentry kind="output" path="target/classes"/>
+</classpath>
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>lucene-samples</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>org.eclipse.m2e.core.maven2Builder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+		<nature>org.eclipse.m2e.core.maven2Nature</nature>
+	</natures>
+</projectDescription>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>com.hascode.tutorial</groupId>
+  <artifactId>lucene-samples</artifactId>
+  <version>0.0.1-SNAPSHOT</version>
+  <dependencies>
+  	<dependency>
+  		<groupId>org.apache.lucene</groupId>
+  		<artifactId>lucene-core</artifactId>
+  		<version>3.5.0</version>
+  	</dependency>
+  </dependencies>
+</project>

src/main/java/com/hascode/tutorial/IndexUpdateByPrimaryKey.java

+package com.hascode.tutorial;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+import org.apache.lucene.index.CorruptIndexException;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.FSDirectory;
+import org.apache.lucene.store.LockObtainFailedException;
+import org.apache.lucene.util.Version;
+
+public class IndexUpdateByPrimaryKey {
+	public static void main(final String[] args) throws CorruptIndexException,
+			LockObtainFailedException, IOException {
+		Directory index = FSDirectory.open(new File("/tmp/index"));
+		IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_35,
+				new StandardAnalyzer(Version.LUCENE_35));
+
+		IndexWriter w = new IndexWriter(index, config);
+		addDoc(w, "1", "test1");
+		addDoc(w, "2", "test2");
+		addDoc(w, "3", "test3");
+		addDoc(w, "1", "test4");
+		addDoc(w, "1", "test5");
+		w.forceMergeDeletes();
+		w.close();
+
+		IndexReader reader = IndexReader.open(index);
+		for (int i = 0; i < reader.maxDoc(); i++) {
+			Document d = reader.document(i);
+			System.out.println("doc from index - id: " + d.get("id")
+					+ ", name: " + d.get("name"));
+		}
+	}
+
+	public static void addDoc(final IndexWriter w, final String id,
+			final String name) throws IOException {
+		Document doc = new Document();
+		doc.add(new Field("id", id, Field.Store.YES, Field.Index.NOT_ANALYZED));
+		doc.add(new Field("name", name, Field.Store.YES, Field.Index.ANALYZED));
+		w.updateDocument(new Term("id", id), doc);
+	}
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.