1 package org.apache.lucene.search;
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 org.apache.lucene.util.LuceneTestCase;
21 import org.apache.lucene.store.Directory;
22 import org.apache.lucene.index.IndexReader;
23 import org.apache.lucene.index.RandomIndexWriter;
24 import org.apache.lucene.index.Term;
26 import org.apache.lucene.document.Document;
27 import org.apache.lucene.document.Field;
30 * Tests {@link PrefixFilter} class.
33 public class TestPrefixFilter extends LuceneTestCase {
34 public void testPrefixFilter() throws Exception {
35 Directory directory = newDirectory();
37 String[] categories = new String[] {"/Computers/Linux",
40 "/Computers/Windows"};
41 RandomIndexWriter writer = new RandomIndexWriter(random, directory);
42 for (int i = 0; i < categories.length; i++) {
43 Document doc = new Document();
44 doc.add(newField("category", categories[i], Field.Store.YES, Field.Index.NOT_ANALYZED));
45 writer.addDocument(doc);
47 IndexReader reader = writer.getReader();
49 // PrefixFilter combined with ConstantScoreQuery
50 PrefixFilter filter = new PrefixFilter(new Term("category", "/Computers"));
51 Query query = new ConstantScoreQuery(filter);
52 IndexSearcher searcher = newSearcher(reader);
53 ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
54 assertEquals(4, hits.length);
56 // test middle of values
57 filter = new PrefixFilter(new Term("category", "/Computers/Mac"));
58 query = new ConstantScoreQuery(filter);
59 hits = searcher.search(query, null, 1000).scoreDocs;
60 assertEquals(2, hits.length);
62 // test start of values
63 filter = new PrefixFilter(new Term("category", "/Computers/Linux"));
64 query = new ConstantScoreQuery(filter);
65 hits = searcher.search(query, null, 1000).scoreDocs;
66 assertEquals(1, hits.length);
69 filter = new PrefixFilter(new Term("category", "/Computers/Windows"));
70 query = new ConstantScoreQuery(filter);
71 hits = searcher.search(query, null, 1000).scoreDocs;
72 assertEquals(1, hits.length);
75 filter = new PrefixFilter(new Term("category", "/Computers/ObsoleteOS"));
76 query = new ConstantScoreQuery(filter);
77 hits = searcher.search(query, null, 1000).scoreDocs;
78 assertEquals(0, hits.length);
80 // test non-existant, before values
81 filter = new PrefixFilter(new Term("category", "/Computers/AAA"));
82 query = new ConstantScoreQuery(filter);
83 hits = searcher.search(query, null, 1000).scoreDocs;
84 assertEquals(0, hits.length);
86 // test non-existant, after values
87 filter = new PrefixFilter(new Term("category", "/Computers/ZZZ"));
88 query = new ConstantScoreQuery(filter);
89 hits = searcher.search(query, null, 1000).scoreDocs;
90 assertEquals(0, hits.length);
92 // test zero length prefix
93 filter = new PrefixFilter(new Term("category", ""));
94 query = new ConstantScoreQuery(filter);
95 hits = searcher.search(query, null, 1000).scoreDocs;
96 assertEquals(4, hits.length);
98 // test non existent field
99 filter = new PrefixFilter(new Term("nonexistantfield", "/Computers"));
100 query = new ConstantScoreQuery(filter);
101 hits = searcher.search(query, null, 1000).scoreDocs;
102 assertEquals(0, hits.length);