1 package org.apache.lucene.analysis;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.io.StringReader;
22 import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
23 import org.apache.lucene.document.Document;
24 import org.apache.lucene.document.Field;
25 import org.apache.lucene.index.IndexReader;
26 import org.apache.lucene.index.IndexWriter;
27 import org.apache.lucene.index.IndexWriterConfig;
28 import org.apache.lucene.index.Term;
29 import org.apache.lucene.index.TermDocs;
30 import org.apache.lucene.queryParser.QueryParser;
31 import org.apache.lucene.search.IndexSearcher;
32 import org.apache.lucene.search.Query;
33 import org.apache.lucene.search.ScoreDoc;
34 import org.apache.lucene.store.RAMDirectory;
36 public class TestKeywordAnalyzer extends BaseTokenStreamTestCase {
38 private RAMDirectory directory;
39 private IndexSearcher searcher;
42 public void setUp() throws Exception {
44 directory = new RAMDirectory();
45 IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig(
46 TEST_VERSION_CURRENT, new SimpleAnalyzer(
47 TEST_VERSION_CURRENT)));
49 Document doc = new Document();
50 doc.add(new Field("partnum", "Q36", Field.Store.YES, Field.Index.NOT_ANALYZED));
51 doc.add(new Field("description", "Illidium Space Modulator", Field.Store.YES, Field.Index.ANALYZED));
52 writer.addDocument(doc);
56 searcher = new IndexSearcher(directory, true);
59 public void testPerFieldAnalyzer() throws Exception {
60 PerFieldAnalyzerWrapper analyzer = new PerFieldAnalyzerWrapper(new SimpleAnalyzer(TEST_VERSION_CURRENT));
61 analyzer.addAnalyzer("partnum", new KeywordAnalyzer());
63 QueryParser queryParser = new QueryParser(TEST_VERSION_CURRENT, "description", analyzer);
64 Query query = queryParser.parse("partnum:Q36 AND SPACE");
66 ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
67 assertEquals("Q36 kept as-is",
68 "+partnum:Q36 +space", query.toString("description"));
69 assertEquals("doc found!", 1, hits.length);
72 public void testMutipleDocument() throws Exception {
73 RAMDirectory dir = new RAMDirectory();
74 IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, new KeywordAnalyzer()));
75 Document doc = new Document();
76 doc.add(new Field("partnum", "Q36", Field.Store.YES, Field.Index.ANALYZED));
77 writer.addDocument(doc);
79 doc.add(new Field("partnum", "Q37", Field.Store.YES, Field.Index.ANALYZED));
80 writer.addDocument(doc);
83 IndexReader reader = IndexReader.open(dir, true);
84 TermDocs td = reader.termDocs(new Term("partnum", "Q36"));
85 assertTrue(td.next());
86 td = reader.termDocs(new Term("partnum", "Q37"));
87 assertTrue(td.next());
91 public void testOffsets() throws Exception {
92 TokenStream stream = new KeywordAnalyzer().tokenStream("field", new StringReader("abcd"));
93 OffsetAttribute offsetAtt = stream.addAttribute(OffsetAttribute.class);
94 assertTrue(stream.incrementToken());
95 assertEquals(0, offsetAtt.startOffset());
96 assertEquals(4, offsetAtt.endOffset());
99 /** blast some random strings through the analyzer */
100 public void testRandomStrings() throws Exception {
101 checkRandomData(random, new KeywordAnalyzer(), 10000*RANDOM_MULTIPLIER);