~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/src/test/org/apache/lucene/search/TestPrefixFilter.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.apache.lucene.search;
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 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;
25
 
 
26
 
import org.apache.lucene.document.Document;
27
 
import org.apache.lucene.document.Field;
28
 
 
29
 
/**
30
 
 * Tests {@link PrefixFilter} class.
31
 
 *
32
 
 */
33
 
public class TestPrefixFilter extends LuceneTestCase {
34
 
  public void testPrefixFilter() throws Exception {
35
 
    Directory directory = newDirectory();
36
 
 
37
 
    String[] categories = new String[] {"/Computers/Linux",
38
 
                                        "/Computers/Mac/One",
39
 
                                        "/Computers/Mac/Two",
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);
46
 
    }
47
 
    IndexReader reader = writer.getReader();
48
 
 
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);
55
 
 
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);
61
 
 
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);
67
 
 
68
 
    // test end of values
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);
73
 
 
74
 
    // test non-existant
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);
79
 
 
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);
85
 
 
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);
91
 
 
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);
97
 
 
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);
103
 
    
104
 
    writer.close();
105
 
    searcher.close();
106
 
    reader.close();
107
 
    directory.close();
108
 
  }
109
 
}