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

« back to all changes in this revision

Viewing changes to solr/core/src/test/org/apache/solr/spelling/SpellingQueryConverterTest.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
 
/**
2
 
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 
 * contributor license agreements.  See the NOTICE file distributed with
4
 
 * this work for additional information regarding copyright ownership.
5
 
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 
 * (the "License"); you may not use this file except in compliance with
7
 
 * the License.  You may obtain a copy of the License at
8
 
 *
9
 
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
11
 
 * Unless required by applicable law or agreed to in writing, software
12
 
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 
 * See the License for the specific language governing permissions and
15
 
 * limitations under the License.
16
 
 */
17
 
 
18
 
package org.apache.solr.spelling;
19
 
 
20
 
import org.apache.lucene.analysis.Token;
21
 
import org.apache.lucene.analysis.WhitespaceAnalyzer;
22
 
import org.apache.lucene.util.LuceneTestCase;
23
 
import org.apache.solr.common.util.NamedList;
24
 
import org.junit.Test;
25
 
 
26
 
import java.util.Collection;
27
 
 
28
 
 
29
 
/**
30
 
 * Test for SpellingQueryConverter
31
 
 *
32
 
 * @version $Id: SpellingQueryConverterTest.java 1040505 2010-11-30 13:06:02Z rmuir $
33
 
 * @since solr 1.3
34
 
 */
35
 
public class SpellingQueryConverterTest extends LuceneTestCase {
36
 
 
37
 
  @Test
38
 
  public void test() throws Exception {
39
 
    SpellingQueryConverter converter = new SpellingQueryConverter();
40
 
    converter.init(new NamedList());
41
 
    converter.setAnalyzer(new WhitespaceAnalyzer(TEST_VERSION_CURRENT));
42
 
    Collection<Token> tokens = converter.convert("field:foo");
43
 
    assertTrue("tokens is null and it shouldn't be", tokens != null);
44
 
    assertTrue("tokens Size: " + tokens.size() + " is not: " + 1, tokens.size() == 1);
45
 
  }
46
 
 
47
 
  @Test
48
 
  public void testSpecialChars()  {
49
 
    SpellingQueryConverter converter = new SpellingQueryConverter();
50
 
    converter.init(new NamedList());
51
 
    converter.setAnalyzer(new WhitespaceAnalyzer(TEST_VERSION_CURRENT));
52
 
    String original = "field_with_underscore:value_with_underscore";
53
 
    Collection<Token> tokens = converter.convert(original);
54
 
    assertTrue("tokens is null and it shouldn't be", tokens != null);
55
 
    assertEquals("tokens Size: " + tokens.size() + " is not 1", 1, tokens.size());
56
 
    assertTrue("Token offsets do not match", isOffsetCorrect(original, tokens));
57
 
 
58
 
    original = "field_with_digits123:value_with_digits123";
59
 
    tokens = converter.convert(original);
60
 
    assertTrue("tokens is null and it shouldn't be", tokens != null);
61
 
    assertEquals("tokens Size: " + tokens.size() + " is not 1", 1, tokens.size());
62
 
    assertTrue("Token offsets do not match", isOffsetCorrect(original, tokens));
63
 
 
64
 
    original = "field-with-hyphens:value-with-hyphens";
65
 
    tokens = converter.convert(original);
66
 
    assertTrue("tokens is null and it shouldn't be", tokens != null);
67
 
    assertEquals("tokens Size: " + tokens.size() + " is not 1", 1, tokens.size());
68
 
    assertTrue("Token offsets do not match", isOffsetCorrect(original, tokens));
69
 
 
70
 
    // mix 'em up and add some to the value
71
 
//    original = "field_with-123s:value_,.|with-hyphens";
72
 
//    tokens = converter.convert(original);
73
 
//    assertTrue("tokens is null and it shouldn't be", tokens != null);
74
 
//    assertEquals("tokens Size: " + tokens.size() + " is not 1", 1, tokens.size());
75
 
//    assertTrue("Token offsets do not match", isOffsetCorrect(original, tokens));
76
 
 
77
 
    original = "foo:bar^5.0";
78
 
    tokens = converter.convert(original);
79
 
    assertTrue("tokens is null and it shouldn't be", tokens != null);
80
 
    assertEquals("tokens Size: " + tokens.size() + " is not 1", 1, tokens.size());
81
 
    assertTrue("Token offsets do not match", isOffsetCorrect(original, tokens));
82
 
  }
83
 
 
84
 
  private boolean isOffsetCorrect(String s, Collection<Token> tokens) {
85
 
    for (Token token : tokens) {
86
 
      int start = token.startOffset();
87
 
      int end = token.endOffset();
88
 
      if (!s.substring(start, end).equals(token.toString()))  return false;
89
 
    }
90
 
    return true;
91
 
  }
92
 
 
93
 
  @Test
94
 
  public void testUnicode() {
95
 
    SpellingQueryConverter converter = new SpellingQueryConverter();
96
 
    converter.init(new NamedList());
97
 
    converter.setAnalyzer(new WhitespaceAnalyzer(TEST_VERSION_CURRENT));
98
 
    
99
 
    // chinese text value
100
 
    Collection<Token> tokens = converter.convert("text_field:我购买了道具和服装。");
101
 
    assertTrue("tokens is null and it shouldn't be", tokens != null);
102
 
    assertEquals("tokens Size: " + tokens.size() + " is not 1", 1, tokens.size());
103
 
 
104
 
    tokens = converter.convert("text_购field:我购买了道具和服装。");
105
 
    assertTrue("tokens is null and it shouldn't be", tokens != null);
106
 
    assertEquals("tokens Size: " + tokens.size() + " is not 1", 1, tokens.size());
107
 
 
108
 
    tokens = converter.convert("text_field:我购xyz买了道具和服装。");
109
 
    assertTrue("tokens is null and it shouldn't be", tokens != null);
110
 
    assertEquals("tokens Size: " + tokens.size() + " is not 1", 1, tokens.size());
111
 
  }
112
 
 
113
 
  @Test
114
 
  public void testMultipleClauses() {
115
 
    SpellingQueryConverter converter = new SpellingQueryConverter();
116
 
    converter.init(new NamedList());
117
 
    converter.setAnalyzer(new WhitespaceAnalyzer(TEST_VERSION_CURRENT));
118
 
 
119
 
    // two field:value pairs should give two tokens
120
 
    Collection<Token> tokens = converter.convert("买text_field:我购买了道具和服装。 field2:bar");
121
 
    assertTrue("tokens is null and it shouldn't be", tokens != null);
122
 
    assertEquals("tokens Size: " + tokens.size() + " is not 2", 2, tokens.size());
123
 
 
124
 
    // a field:value pair and a search term should give two tokens
125
 
    tokens = converter.convert("text_field:我购买了道具和服装。 bar");
126
 
    assertTrue("tokens is null and it shouldn't be", tokens != null);
127
 
    assertEquals("tokens Size: " + tokens.size() + " is not 2", 2, tokens.size());
128
 
  }
129
 
}