Java tutorial
package com.svenjacobs.lugaene; /* * #%L * LuGAEne * %% * Copyright (C) 2013 Sven Jacobs * %% * 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. * #L% */ 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; import org.apache.lucene.document.StringField; import org.apache.lucene.document.TextField; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.Term; import org.apache.lucene.search.*; import org.apache.lucene.store.Directory; import org.apache.lucene.util.Version; import org.junit.Test; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; /** * @author Sven Jacobs */ public class GaeDirectoryTest extends BaseGaeTest { private static final String FIELD_TITLE = "title"; private static final String FIELD_CONTENTS = "contents"; @Test public void wholeCycle() throws Exception { // Index final Directory directory = new GaeDirectory("Test"); final Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_44); final IndexWriterConfig config = GaeIndexWriterConfigHelper.create(Version.LUCENE_44, analyzer); final IndexWriter indexWriter = new IndexWriter(directory, config); final Document doc1 = new Document(); doc1.add(new StringField(FIELD_TITLE, "Title1", Field.Store.YES)); doc1.add(new TextField(FIELD_CONTENTS, "keyword1 keyword2 lorem ipsum", Field.Store.NO)); indexWriter.addDocument(doc1); final Document doc2 = new Document(); doc2.add(new StringField(FIELD_TITLE, "Title2", Field.Store.YES)); doc2.add(new TextField(FIELD_CONTENTS, "keyword3 keyword4 lorem ipsum", Field.Store.NO)); indexWriter.addDocument(doc2); indexWriter.close(); // Search final DirectoryReader reader = DirectoryReader.open(directory); final IndexSearcher searcher = new IndexSearcher(reader); final BooleanQuery query = new BooleanQuery(); query.add(new TermQuery(new Term(FIELD_TITLE, "Title1")), BooleanClause.Occur.MUST); query.add(new TermQuery(new Term(FIELD_CONTENTS, "lorem")), BooleanClause.Occur.MUST); ScoreDoc[] hits = searcher.search(query, 100).scoreDocs; assertThat(hits.length, is(1)); assertThat(searcher.doc(hits[0].doc).get(FIELD_TITLE), is("Title1")); hits = searcher.search(new TermQuery(new Term(FIELD_CONTENTS, "lorem")), 100).scoreDocs; assertThat(hits.length, is(2)); assertThat(searcher.doc(hits[0].doc).get(FIELD_TITLE), is("Title1")); assertThat(searcher.doc(hits[1].doc).get(FIELD_TITLE), is("Title2")); hits = searcher.search(new TermQuery(new Term(FIELD_CONTENTS, "keyword3")), 100).scoreDocs; assertThat(hits.length, is(1)); assertThat(searcher.doc(hits[0].doc).get(FIELD_TITLE), is("Title2")); } }