Apache Lucene open-source search software
KeywordAnalyzer contribution - adapted from _Lucene in Action_ code
Erik Hatcher826fef7f6ab3
diff --git a/contrib/analyzers/src/java/org/apache/lucene/analysis/KeywordAnalyzer.java b/contrib/analyzers/src/java/org/apache/lucene/analysis/KeywordAnalyzer.javanew file mode 100644index 00000000000..94d6ce55621--- /dev/null+++ b/contrib/analyzers/src/java/org/apache/lucene/analysis/KeywordAnalyzer.java@@ -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 filediff --git a/contrib/analyzers/src/test/org/apache/lucene/analysis/TestKeywordAnalyzer.java b/contrib/analyzers/src/test/org/apache/lucene/analysis/TestKeywordAnalyzer.javanew file mode 100644index 00000000000..c20657caaa3--- /dev/null+++ b/contrib/analyzers/src/test/org/apache/lucene/analysis/TestKeywordAnalyzer.java@@ -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());+ }+}