pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / analysis / TestKeywordAnalyzer.java
1 package org.apache.lucene.analysis;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import java.io.StringReader;
21
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;
35
36 public class TestKeywordAnalyzer extends BaseTokenStreamTestCase {
37   
38   private RAMDirectory directory;
39   private IndexSearcher searcher;
40
41   @Override
42   public void setUp() throws Exception {
43     super.setUp();
44     directory = new RAMDirectory();
45     IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig(
46         TEST_VERSION_CURRENT, new SimpleAnalyzer(
47         TEST_VERSION_CURRENT)));
48
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);
53
54     writer.close();
55
56     searcher = new IndexSearcher(directory, true);
57   }
58
59   public void testPerFieldAnalyzer() throws Exception {
60     PerFieldAnalyzerWrapper analyzer = new PerFieldAnalyzerWrapper(new SimpleAnalyzer(TEST_VERSION_CURRENT));
61     analyzer.addAnalyzer("partnum", new KeywordAnalyzer());
62
63     QueryParser queryParser = new QueryParser(TEST_VERSION_CURRENT, "description", analyzer);
64     Query query = queryParser.parse("partnum:Q36 AND SPACE");
65
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);
70   }
71
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);
78     doc = new Document();
79     doc.add(new Field("partnum", "Q37", Field.Store.YES, Field.Index.ANALYZED));
80     writer.addDocument(doc);
81     writer.close();
82
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());
88   }
89
90   // LUCENE-1441
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());
97   }
98   
99   /** blast some random strings through the analyzer */
100   public void testRandomStrings() throws Exception {
101     checkRandomData(random, new KeywordAnalyzer(), 10000*RANDOM_MULTIPLIER);
102   }
103 }