Java tutorial
/* * Copyright (C) 2012 Taiki Sugawara * * 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 com.github.buzztaiki.lucene.lastuni; import java.io.IOException; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.IndexWriterConfig.OpenMode; import org.apache.lucene.index.Term; import org.apache.lucene.queryparser.classic.ParseException; import org.apache.lucene.queryparser.classic.QueryParser; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; import org.apache.lucene.store.RAMDirectory; import org.apache.lucene.util.LuceneTestCase; public class CJKSingleCharQueryTest extends LuceneTestCase { private IndexWriter newWriter(Directory dir, Analyzer analyzer) throws IOException { return new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, analyzer).setOpenMode(OpenMode.CREATE)); } private void addDoc(IndexWriter writer, String content) throws IOException { Document doc = new Document(); doc.add(newTextField("content", content, Field.Store.YES)); writer.addDocument(doc); } private void addDoc(Directory dir, Analyzer analyzer, String content) throws IOException { IndexWriter writer = newWriter(dir, analyzer); try { addDoc(writer, content); writer.commit(); } finally { writer.close(); } } private QueryParser newQueryParser(Analyzer analyzer) { // TODO: use flexible parser? QueryParser qp = new CJKSingleCharSupportQueryParser(TEST_VERSION_CURRENT, "content", analyzer); qp.setDefaultOperator(QueryParser.AND_OPERATOR); qp.setAutoGeneratePhraseQueries(true); qp.setAllowLeadingWildcard(true); return qp; } private TopDocs search(Directory dir, Analyzer analyzer, String query) throws IOException, ParseException { IndexSearcher searcher = newSearcher(DirectoryReader.open(dir)); try { QueryParser qp = newQueryParser(analyzer); Query q = qp.parse(query); return searcher.search(q, 10); } finally { searcher.getIndexReader().close(); } } private RAMDirectory dir; private CJKLastUniGramAnalyzer analyzer; private CJKLastUniGramAnalyzer uniAnalyzer; @Override public void setUp() throws Exception { super.setUp(); dir = new RAMDirectory(); analyzer = new CJKLastUniGramAnalyzer(TEST_VERSION_CURRENT, false); uniAnalyzer = new CJKLastUniGramAnalyzer(TEST_VERSION_CURRENT, true); } @Override public void tearDown() throws Exception { dir.close(); super.tearDown(); } public void test???()throws Exception { addDoc(dir, uniAnalyzer, "???"); assertEquals(1, search(dir, analyzer, "???").totalHits); assertEquals(1, search(dir, analyzer, "?").totalHits); assertEquals(1, search(dir, analyzer, "?").totalHits); assertEquals(1, search(dir, analyzer, "?").totalHits); } public void test?????() throws Exception { addDoc(dir, uniAnalyzer, "?????"); assertEquals(1, search(dir, analyzer, "???").totalHits); assertEquals(1, search(dir, analyzer, "?").totalHits); assertEquals(1, search(dir, analyzer, "?").totalHits); assertEquals(1, search(dir, analyzer, "?").totalHits); } public void testB()throws Exception { addDoc(dir, uniAnalyzer, "B"); assertEquals(1, search(dir, analyzer, "B").totalHits); assertEquals(1, search(dir, analyzer, "").totalHits); assertEquals(1, search(dir, analyzer, "B").totalHits); assertEquals(1, search(dir, analyzer, "").totalHits); assertEquals(1, search(dir, analyzer, "B").totalHits); } public void test_???()throws Exception { addDoc(dir, uniAnalyzer, " ???"); assertEquals(1, search(dir, analyzer, "???").totalHits); assertEquals(1, search(dir, analyzer, "").totalHits); } public void test()throws Exception { addDoc(dir, uniAnalyzer, ""); assertEquals(1, search(dir, analyzer, "").totalHits); } public void test()throws Exception { addDoc(dir, uniAnalyzer, ""); assertEquals(1, search(dir, analyzer, "").totalHits); assertEquals(0, search(dir, analyzer, "?").totalHits); assertEquals(1, search(dir, analyzer, "*").totalHits); } public void test()throws Exception { addDoc(dir, uniAnalyzer, ""); assertEquals(1, search(dir, analyzer, "").totalHits); assertEquals(1, search(dir, analyzer, "?").totalHits); assertEquals(1, search(dir, analyzer, "*").totalHits); } }