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

« back to all changes in this revision

Viewing changes to solr/core/src/test/org/apache/solr/analysis/TestReversedWildcardFilterFactory.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.solr.analysis;
2
 
/**
3
 
 * Licensed to the Apache Software Foundation (ASF) under one or more
4
 
 * contributor license agreements.  See the NOTICE file distributed with
5
 
 * this work for additional information regarding copyright ownership.
6
 
 * The ASF licenses this file to You under the Apache License, Version 2.0
7
 
 * (the "License"); you may not use this file except in compliance with
8
 
 * the License.  You may obtain a copy of the License at
9
 
 *
10
 
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 
 *
12
 
 * Unless required by applicable law or agreed to in writing, software
13
 
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 
 * See the License for the specific language governing permissions and
16
 
 * limitations under the License.
17
 
 */
18
 
 
19
 
 
20
 
import java.io.IOException;
21
 
import java.io.StringReader;
22
 
 
23
 
import java.util.HashMap;
24
 
import java.util.Map;
25
 
 
26
 
import org.apache.lucene.analysis.Analyzer;
27
 
import org.apache.lucene.analysis.MockTokenizer;
28
 
import org.apache.lucene.analysis.TokenStream;
29
 
import org.apache.lucene.queryParser.ParseException;
30
 
import org.apache.lucene.search.Query;
31
 
import org.apache.solr.SolrTestCaseJ4;
32
 
import org.apache.solr.schema.IndexSchema;
33
 
import org.apache.solr.search.SolrQueryParser;
34
 
import org.junit.Before;
35
 
import org.junit.BeforeClass;
36
 
import org.junit.Test;
37
 
 
38
 
import static org.apache.solr.analysis.BaseTokenTestCase.*;
39
 
 
40
 
public class TestReversedWildcardFilterFactory extends SolrTestCaseJ4 {
41
 
  Map<String,String> args = new HashMap<String, String>();
42
 
  ReversedWildcardFilterFactory factory = new ReversedWildcardFilterFactory();
43
 
  IndexSchema schema;
44
 
 
45
 
  @BeforeClass
46
 
  public static void beforeClass() throws Exception {
47
 
    initCore("solrconfig.xml","schema-reversed.xml");
48
 
  }
49
 
  
50
 
  @Override
51
 
  @Before
52
 
  public void setUp() throws Exception {
53
 
    super.setUp();
54
 
    schema = new IndexSchema(solrConfig, getSchemaFile(), null);
55
 
  }
56
 
 
57
 
  @Test
58
 
  public void testReversedTokens() throws IOException {
59
 
    String text = "simple text";
60
 
    args.put("withOriginal", "true");
61
 
    factory.init(args);
62
 
    TokenStream input = factory.create(new MockTokenizer(new StringReader(text), MockTokenizer.WHITESPACE, false));
63
 
    assertTokenStreamContents(input, 
64
 
        new String[] { "\u0001elpmis", "simple", "\u0001txet", "text" },
65
 
        new int[] { 1, 0, 1, 0 });
66
 
 
67
 
    // now without original tokens
68
 
    args.put("withOriginal", "false");
69
 
    factory.init(args);
70
 
    input = factory.create(new MockTokenizer(new StringReader(text), MockTokenizer.WHITESPACE, false));
71
 
    assertTokenStreamContents(input,
72
 
        new String[] { "\u0001elpmis", "\u0001txet" },
73
 
        new int[] { 1, 1 });
74
 
  }
75
 
  
76
 
  @Test
77
 
  public void testIndexingAnalysis() throws Exception {
78
 
    Analyzer a = schema.getAnalyzer();
79
 
    String text = "one two three si\uD834\uDD1Ex";
80
 
 
81
 
    // field one
82
 
    TokenStream input = a.tokenStream("one", new StringReader(text));
83
 
    assertTokenStreamContents(input,
84
 
        new String[] { "\u0001eno", "one", "\u0001owt", "two", 
85
 
          "\u0001eerht", "three", "\u0001x\uD834\uDD1Eis", "si\uD834\uDD1Ex" },
86
 
        new int[] { 0, 0, 4, 4, 8, 8, 14, 14 },
87
 
        new int[] { 3, 3, 7, 7, 13, 13, 19, 19 },
88
 
        new int[] { 1, 0, 1, 0, 1, 0, 1, 0 }
89
 
    );
90
 
    // field two
91
 
    input = a.tokenStream("two", new StringReader(text));
92
 
    assertTokenStreamContents(input,
93
 
        new String[] { "\u0001eno", "\u0001owt", 
94
 
          "\u0001eerht", "\u0001x\uD834\uDD1Eis" },
95
 
        new int[] { 0, 4, 8, 14 },
96
 
        new int[] { 3, 7, 13, 19 },
97
 
        new int[] { 1, 1, 1, 1 }
98
 
    );
99
 
    // field three
100
 
    input = a.tokenStream("three", new StringReader(text));
101
 
    assertTokenStreamContents(input,
102
 
        new String[] { "one", "two", "three", "si\uD834\uDD1Ex" },
103
 
        new int[] { 0, 4, 8, 14 },
104
 
        new int[] { 3, 7, 13, 19 }
105
 
    );
106
 
  }
107
 
  
108
 
  @Test
109
 
  public void testQueryParsing() throws IOException, ParseException {
110
 
 
111
 
    SolrQueryParser parserOne = new SolrQueryParser(schema, "one");
112
 
    assertTrue(parserOne.getAllowLeadingWildcard());
113
 
    SolrQueryParser parserTwo = new SolrQueryParser(schema, "two");
114
 
    assertTrue(parserTwo.getAllowLeadingWildcard());
115
 
    SolrQueryParser parserThree = new SolrQueryParser(schema, "three");
116
 
    // XXX note: this should be false, but for now we return true for any field,
117
 
    // XXX if at least one field uses the reversing
118
 
    assertTrue(parserThree.getAllowLeadingWildcard());
119
 
    String text = "one +two *hree f*ur fiv* *si\uD834\uDD1Ex";
120
 
    String expectedOne = "one:one +one:two one:\u0001eerh* one:\u0001ru*f one:fiv* one:\u0001x\uD834\uDD1Eis*";
121
 
    String expectedTwo = "two:one +two:two two:\u0001eerh* two:\u0001ru*f two:fiv* two:\u0001x\uD834\uDD1Eis*";
122
 
    String expectedThree = "three:one +three:two three:*hree three:f*ur three:fiv* three:*si\uD834\uDD1Ex";
123
 
    Query q = parserOne.parse(text);
124
 
    assertEquals(expectedOne, q.toString());
125
 
    q = parserTwo.parse(text);
126
 
    assertEquals(expectedTwo, q.toString());
127
 
    q = parserThree.parse(text);
128
 
    assertEquals(expectedThree, q.toString());
129
 
    // test conditional reversal
130
 
    String condText = "*hree t*ree th*ee thr*e ?hree t?ree th?ee th?*ee " + 
131
 
        "short*token ver*longtoken";
132
 
    String expected = "two:\u0001eerh* two:\u0001eer*t two:\u0001ee*ht " +
133
 
        "two:thr*e " +
134
 
        "two:\u0001eerh? two:\u0001eer?t " +
135
 
        "two:th?ee " +
136
 
        "two:th?*ee " +
137
 
        "two:short*token " +
138
 
        "two:\u0001nekotgnol*rev";
139
 
    q = parserTwo.parse(condText);
140
 
    assertEquals(expected, q.toString());
141
 
  }
142
 
 
143
 
}