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

« back to all changes in this revision

Viewing changes to lucene/backwards/src/test/org/apache/lucene/search/TestPhrasePrefixQuery.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
 
 
22
 
import org.apache.lucene.document.Document;
23
 
import org.apache.lucene.document.Field;
24
 
import org.apache.lucene.index.RandomIndexWriter;
25
 
import org.apache.lucene.index.IndexReader;
26
 
import org.apache.lucene.index.Term;
27
 
import org.apache.lucene.index.TermEnum;
28
 
import org.apache.lucene.store.Directory;
29
 
 
30
 
import java.io.IOException;
31
 
import java.util.LinkedList;
32
 
 
33
 
/**
34
 
 * This class tests PhrasePrefixQuery class.
35
 
 */
36
 
public class TestPhrasePrefixQuery extends LuceneTestCase {
37
 
  
38
 
  /**
39
 
     *
40
 
     */
41
 
  public void testPhrasePrefix() throws IOException {
42
 
    Directory indexStore = newDirectory();
43
 
    RandomIndexWriter writer = new RandomIndexWriter(random, indexStore);
44
 
    Document doc1 = new Document();
45
 
    Document doc2 = new Document();
46
 
    Document doc3 = new Document();
47
 
    Document doc4 = new Document();
48
 
    Document doc5 = new Document();
49
 
    doc1.add(newField("body", "blueberry pie", Field.Store.YES,
50
 
        Field.Index.ANALYZED));
51
 
    doc2.add(newField("body", "blueberry strudel", Field.Store.YES,
52
 
        Field.Index.ANALYZED));
53
 
    doc3.add(newField("body", "blueberry pizza", Field.Store.YES,
54
 
        Field.Index.ANALYZED));
55
 
    doc4.add(newField("body", "blueberry chewing gum", Field.Store.YES,
56
 
        Field.Index.ANALYZED));
57
 
    doc5.add(newField("body", "piccadilly circus", Field.Store.YES,
58
 
        Field.Index.ANALYZED));
59
 
    writer.addDocument(doc1);
60
 
    writer.addDocument(doc2);
61
 
    writer.addDocument(doc3);
62
 
    writer.addDocument(doc4);
63
 
    writer.addDocument(doc5);
64
 
    IndexReader reader = writer.getReader();
65
 
    writer.close();
66
 
    
67
 
    IndexSearcher searcher = newSearcher(reader);
68
 
    
69
 
    // PhrasePrefixQuery query1 = new PhrasePrefixQuery();
70
 
    MultiPhraseQuery query1 = new MultiPhraseQuery();
71
 
    // PhrasePrefixQuery query2 = new PhrasePrefixQuery();
72
 
    MultiPhraseQuery query2 = new MultiPhraseQuery();
73
 
    query1.add(new Term("body", "blueberry"));
74
 
    query2.add(new Term("body", "strawberry"));
75
 
    
76
 
    LinkedList<Term> termsWithPrefix = new LinkedList<Term>();
77
 
    
78
 
    // this TermEnum gives "piccadilly", "pie" and "pizza".
79
 
    String prefix = "pi";
80
 
    TermEnum te = reader.terms(new Term("body", prefix + "*"));
81
 
    do {
82
 
        if (te.term().text().startsWith(prefix))
83
 
        {
84
 
            termsWithPrefix.add(te.term());
85
 
        }
86
 
    } while (te.next());
87
 
    
88
 
    query1.add(termsWithPrefix.toArray(new Term[0]));
89
 
    query2.add(termsWithPrefix.toArray(new Term[0]));
90
 
    
91
 
    ScoreDoc[] result;
92
 
    result = searcher.search(query1, null, 1000).scoreDocs;
93
 
    assertEquals(2, result.length);
94
 
    
95
 
    result = searcher.search(query2, null, 1000).scoreDocs;
96
 
    assertEquals(0, result.length);
97
 
    searcher.close();
98
 
    reader.close();
99
 
    indexStore.close();
100
 
  }
101
 
}