You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucene.apache.org by eh...@apache.org on 2005/02/08 20:13:06 UTC
svn commit: r152921 - in lucene/java/trunk/contrib/analyzers/src:
java/org/apache/lucene/analysis/KeywordAnalyzer.java
test/org/apache/lucene/analysis/TestKeywordAnalyzer.java
Author: ehatcher
Date: Tue Feb 8 11:13:05 2005
New Revision: 152921
URL: http://svn.apache.org/viewcvs?view=rev&rev=152921
Log:
KeywordAnalyzer contribution - adapted from _Lucene in Action_ code
Added:
lucene/java/trunk/contrib/analyzers/src/java/org/apache/lucene/analysis/KeywordAnalyzer.java
lucene/java/trunk/contrib/analyzers/src/test/org/apache/lucene/analysis/TestKeywordAnalyzer.java
Added: lucene/java/trunk/contrib/analyzers/src/java/org/apache/lucene/analysis/KeywordAnalyzer.java
URL: http://svn.apache.org/viewcvs/lucene/java/trunk/contrib/analyzers/src/java/org/apache/lucene/analysis/KeywordAnalyzer.java?view=auto&rev=152921
==============================================================================
--- lucene/java/trunk/contrib/analyzers/src/java/org/apache/lucene/analysis/KeywordAnalyzer.java (added)
+++ lucene/java/trunk/contrib/analyzers/src/java/org/apache/lucene/analysis/KeywordAnalyzer.java Tue Feb 8 11:13:05 2005
@@ -0,0 +1,49 @@
+package org.apache.lucene.analysis;
+
+/**
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * 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.
+ */
+
+import java.io.IOException;
+import java.io.Reader;
+
+/**
+ * "Tokenizes" the entire stream as a single token.
+ */
+public class KeywordAnalyzer extends Analyzer {
+ public TokenStream tokenStream(String fieldName,
+ final Reader reader) {
+ return new TokenStream() {
+ private boolean done;
+ private final char[] buffer = new char[1024];
+ public Token next() throws IOException {
+ if (!done) {
+ done = true;
+ StringBuffer buffer = new StringBuffer();
+ int length;
+ while (true) {
+ length = reader.read(this.buffer);
+ if (length == -1) break;
+
+ buffer.append(this.buffer, 0, length);
+ }
+ String text = buffer.toString();
+ return new Token(text, 0, text.length());
+ }
+ return null;
+ }
+ };
+ }
+}
\ No newline at end of file
Added: lucene/java/trunk/contrib/analyzers/src/test/org/apache/lucene/analysis/TestKeywordAnalyzer.java
URL: http://svn.apache.org/viewcvs/lucene/java/trunk/contrib/analyzers/src/test/org/apache/lucene/analysis/TestKeywordAnalyzer.java?view=auto&rev=152921
==============================================================================
--- lucene/java/trunk/contrib/analyzers/src/test/org/apache/lucene/analysis/TestKeywordAnalyzer.java (added)
+++ lucene/java/trunk/contrib/analyzers/src/test/org/apache/lucene/analysis/TestKeywordAnalyzer.java Tue Feb 8 11:13:05 2005
@@ -0,0 +1,63 @@
+package org.apache.lucene.analysis;
+
+/**
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * 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.
+ */
+
+import junit.framework.TestCase;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.store.RAMDirectory;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.Hits;
+import org.apache.lucene.queryParser.QueryParser;
+
+public class TestKeywordAnalyzer extends TestCase {
+ RAMDirectory directory;
+ private IndexSearcher searcher;
+
+ public void setUp() throws Exception {
+ directory = new RAMDirectory();
+ IndexWriter writer = new IndexWriter(directory,
+ new SimpleAnalyzer(),
+ true);
+
+ Document doc = new Document();
+ doc.add(Field.Keyword("partnum", "Q36"));
+ doc.add(Field.Text("description", "Illidium Space Modulator"));
+ writer.addDocument(doc);
+
+ writer.close();
+
+ searcher = new IndexSearcher(directory);
+ }
+
+ public void testPerFieldAnalyzer() throws Exception {
+ PerFieldAnalyzerWrapper analyzer = new PerFieldAnalyzerWrapper(
+ new SimpleAnalyzer());
+ analyzer.addAnalyzer("partnum", new KeywordAnalyzer());
+
+ Query query = QueryParser.parse("partnum:Q36 AND SPACE",
+ "description",
+ analyzer);
+
+ Hits hits = searcher.search(query);
+ assertEquals("Q36 kept as-is",
+ "+partnum:Q36 +space", query.toString("description"));
+ assertEquals("doc found!", 1, hits.length());
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-dev-help@jakarta.apache.org