01: package org.apache.lucene.analysis;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * 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 org.apache.lucene.util.LuceneTestCase;
21: import org.apache.lucene.index.IndexReader;
22: import org.apache.lucene.index.IndexWriter;
23: import org.apache.lucene.index.Term;
24: import org.apache.lucene.index.TermDocs;
25: import org.apache.lucene.store.RAMDirectory;
26: import org.apache.lucene.document.Document;
27: import org.apache.lucene.document.Field;
28: import org.apache.lucene.search.IndexSearcher;
29: import org.apache.lucene.search.Query;
30: import org.apache.lucene.search.Hits;
31: import org.apache.lucene.queryParser.QueryParser;
32:
33: public class TestKeywordAnalyzer extends LuceneTestCase {
34:
35: private RAMDirectory directory;
36: private IndexSearcher searcher;
37:
38: public void setUp() throws Exception {
39: super .setUp();
40: directory = new RAMDirectory();
41: IndexWriter writer = new IndexWriter(directory,
42: new SimpleAnalyzer(), true);
43:
44: Document doc = new Document();
45: doc.add(new Field("partnum", "Q36", Field.Store.YES,
46: Field.Index.UN_TOKENIZED));
47: doc.add(new Field("description", "Illidium Space Modulator",
48: Field.Store.YES, Field.Index.TOKENIZED));
49: writer.addDocument(doc);
50:
51: writer.close();
52:
53: searcher = new IndexSearcher(directory);
54: }
55:
56: public void testPerFieldAnalyzer() throws Exception {
57: PerFieldAnalyzerWrapper analyzer = new PerFieldAnalyzerWrapper(
58: new SimpleAnalyzer());
59: analyzer.addAnalyzer("partnum", new KeywordAnalyzer());
60:
61: QueryParser queryParser = new QueryParser("description",
62: analyzer);
63: Query query = queryParser.parse("partnum:Q36 AND SPACE");
64:
65: Hits hits = searcher.search(query);
66: assertEquals("Q36 kept as-is", "+partnum:Q36 +space", query
67: .toString("description"));
68: assertEquals("doc found!", 1, hits.length());
69: }
70:
71: public void testMutipleDocument() throws Exception {
72: RAMDirectory dir = new RAMDirectory();
73: IndexWriter writer = new IndexWriter(dir,
74: new KeywordAnalyzer(), true);
75: Document doc = new Document();
76: doc.add(new Field("partnum", "Q36", Field.Store.YES,
77: Field.Index.TOKENIZED));
78: writer.addDocument(doc);
79: doc = new Document();
80: doc.add(new Field("partnum", "Q37", Field.Store.YES,
81: Field.Index.TOKENIZED));
82: writer.addDocument(doc);
83: writer.close();
84:
85: IndexReader reader = IndexReader.open(dir);
86: TermDocs td = reader.termDocs(new Term("partnum", "Q36"));
87: assertTrue(td.next());
88: td = reader.termDocs(new Term("partnum", "Q37"));
89: assertTrue(td.next());
90: }
91: }
|