Commits

Micha Kops  committed f0d5a0d

Lucene examples extended.

  • Participants
  • Parent commits c6beecd

Comments (0)

Files changed (3)

File lucene-example/pom.xml

 	<artifactId>lucene-example</artifactId>
 	<packaging>jar</packaging>
 
+	<properties>
+		<lucene.version>4.6.0</lucene.version>
+	</properties>
+
 	<dependencies>
 		<dependency>
 			<groupId>com.mysema.querydsl</groupId>
 		<dependency>
 			<groupId>org.apache.lucene</groupId>
 			<artifactId>lucene-core</artifactId>
+			<version>${lucene.version}</version>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.lucene</groupId>
+			<artifactId>lucene-analyzers-common</artifactId>
+			<version>${lucene.version}</version>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.lucene</groupId>
+			<artifactId>lucene-queries</artifactId>
+			<version>4.6.0</version>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.lucene</groupId>
+			<artifactId>lucene-misc</artifactId>
+			<version>4.6.0</version>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.lucene</groupId>
+			<artifactId>lucene-sandbox</artifactId>
+			<version>4.6.0</version>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.lucene</groupId>
+			<artifactId>lucene-queryparser</artifactId>
 			<version>4.6.0</version>
 		</dependency>
 	</dependencies>

File lucene-example/src/main/java/com/hascode/app/LuceneExamples.java

 package com.hascode.app;
 
+import java.io.File;
+import java.io.IOException;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.List;
+
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.document.DateTools;
+import org.apache.lucene.document.DateTools.Resolution;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field.Store;
+import org.apache.lucene.document.StringField;
+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.IndexWriterConfig.OpenMode;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.FSDirectory;
+import org.apache.lucene.util.Version;
+
+import com.hascode.entity.QBook;
+import com.mysema.query.lucene.LuceneQuery;
 
 public class LuceneExamples {
-	public void run() {
+	private static final String INDEX = "target/index";
+
+	public void run() throws IOException {
+		Calendar cal1 = Calendar.getInstance();
+		cal1.set(2010, 0, 0);
+
+		Calendar cal2 = Calendar.getInstance();
+		cal2.set(2011, 0, 0);
+
+		File indexDir = new File(INDEX);
+		indexDir.deleteOnExit();
+
+		Directory index = FSDirectory.open(indexDir);
+		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_46);
+
+		IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_46,
+				analyzer);
+		iwc.setOpenMode(OpenMode.CREATE);
+		IndexWriter writer = new IndexWriter(index, iwc);
+
+		createBook(writer, "The big book of something", "Some Guy",
+				cal2.getTime(), "comedy", "horror", "action");
+		writer.commit();
+		writer.close();
+
+		QBook book = new QBook("doc");
+
+		IndexReader reader = DirectoryReader.open(index);
+		System.out.println(reader.numDocs() + " books stored in index");
+		IndexSearcher searcher = new IndexSearcher(reader);
+		LuceneQuery query = new LuceneQuery(searcher);
+
+		System.out.println("Searching a Lucene Index with Querydsl");
+		LuceneQuery q = query.where(book.author.eq("Some Guy")).orderBy(
+				book.title.asc());
+		System.out.println("generated lucene query: " + q.toString());
+		List<Document> documents = q.list();
+
+		System.out.println(documents.size() + " books found");
+		for (Document doc : documents) {
+			System.out.println(doc);
+		}
+	}
 
+	private void createBook(final IndexWriter writer, final String title,
+			final String author, final Date published, final String... tags)
+			throws IOException {
+		Document doc = new Document();
+		doc.add(new TextField("title", title, Store.YES));
+		doc.add(new TextField("author", author, Store.YES));
+		doc.add(new StringField("published", DateTools.dateToString(published,
+				Resolution.YEAR), Store.YES));
+		for (String tag : tags)
+			doc.add(new StringField("tag", tag, Store.YES));
+		writer.addDocument(doc);
 	}
 
-	public static void main(final String[] args) {
+	public static void main(final String[] args) throws IOException {
 		new LuceneExamples().run();
 	}
 }

File lucene-example/src/main/java/com/hascode/entity/QBook.java

+package com.hascode.entity;
+
+import java.util.Date;
+
+import org.apache.lucene.document.Document;
+
+import com.mysema.query.types.PathMetadataFactory;
+import com.mysema.query.types.path.DatePath;
+import com.mysema.query.types.path.EntityPathBase;
+import com.mysema.query.types.path.StringPath;
+
+public class QBook extends EntityPathBase<Document> {
+	private static final long serialVersionUID = 1L;
+
+	public QBook(final String var) {
+		super(Document.class, PathMetadataFactory.forVariable(var));
+	}
+
+	public final StringPath title = createString("title");
+	public final StringPath author = createString("author");
+	public final DatePath<Date> published = createDate("published", Date.class);
+	// public final ArrayPath<String> tags = createArray ("tags", String.class);
+
+}