mirror of https://github.com/apache/lucene.git
KeywordAnalyzer contribution - adapted from _Lucene in Action_ code
git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@152921 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
c1623f84b8
commit
826fef7f6a
|
@ -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;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue