Commits

Micha Kops  committed ee93bc6

Initial import: Index stats example added.

  • Participants

Comments (0)

Files changed (4)

+.project
+.classpath
+target
+.settings
+# Lucene 4.0 Tutorial
+
+---
+Some samples for the Lucene 4.0 beta API.
+
+For the full tutorial please feel free to take a look at my blog at [www.hascode.com].
+
+
+*2012 Micha Kops / hasCode.com*
+
+  [www.hascode.com]:http://www.hascode.com/
+<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-4-tutorial</artifactId>
+	<version>0.0.1</version>
+	
+	<properties>
+		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+		<lucene.version>4.0-SNAPSHOT</lucene.version>
+	</properties>
+	
+	<dependencies>
+		<dependency>
+			<artifactId>lucene-core</artifactId>
+			<groupId>org.apache.lucene</groupId>
+			<version>${lucene.version}</version>
+		</dependency>
+		<dependency>
+			<artifactId>lucene-facet</artifactId>
+			<groupId>org.apache.lucene</groupId>
+			<version>${lucene.version}</version>
+		</dependency>
+	</dependencies>
+
+	<repositories>
+		<repository>
+			<id>lucene-repository</id>
+			<name>Lucene Maven</name>
+			<url>https://repository.apache.org/snapshots/</url>
+			<snapshots>
+				<enabled>true</enabled>
+				<updatePolicy>always</updatePolicy>
+			</snapshots>
+		</repository>
+	</repositories>
+</project>

File src/main/java/com/hascode/tutorial/IndexStatsExample.java

+package com.hascode.tutorial;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.lucene.analysis.Analyzer;
+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.LongField;
+import org.apache.lucene.document.StringField;
+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.IndexWriterConfig.OpenMode;
+import org.apache.lucene.search.CollectionStatistics;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.FSDirectory;
+import org.apache.lucene.util.Version;
+
+public class IndexStatsExample {
+	private static final String INDEX = "target/index_stats";
+
+	public static void main(final String[] args) throws IOException {
+		createRandomData();
+
+		IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(
+				INDEX)));
+		IndexSearcher searcher = new IndexSearcher(reader);
+		CollectionStatistics stats = searcher.collectionStatistics("title");
+		System.out.println("Statistics for the field 'title':");
+		System.out.println("Number of documents with a term for the field: "
+				+ stats.docCount());
+		System.out.println("Total number of documents: " + stats.maxDoc());
+		System.out.println("Total number of postings for the field: "
+				+ stats.sumDocFreq());
+		System.out.println("Total number of tokens for the field: "
+				+ stats.sumTotalTermFreq());
+	}
+
+	public static void createRandomData() throws IOException {
+		Directory dir = FSDirectory.open(new File(INDEX));
+		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
+		IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_40,
+				analyzer);
+		iwc.setOpenMode(OpenMode.CREATE);
+		IndexWriter writer = new IndexWriter(dir, iwc);
+
+		for (long i = 1; i < 2000; i++) {
+			Document doc = new Document();
+			doc.add(new LongField("id", i, Store.YES));
+			doc.add(new StringField("title", "The big book of boredom - Part "
+					+ (int) i, Store.YES));
+			writer.addDocument(doc);
+		}
+		Document doc = new Document();
+		doc.add(new LongField("id", 9999l, Store.YES));
+		writer.addDocument(doc);
+		writer.close();
+	}
+}