1
package org.apache.lucene.queryParser.complexPhrase;
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 java.util.HashSet;
22
import org.apache.lucene.analysis.Analyzer;
23
import org.apache.lucene.analysis.MockAnalyzer;
24
import org.apache.lucene.document.Document;
25
import org.apache.lucene.document.Field;
26
import org.apache.lucene.index.IndexWriter;
27
import org.apache.lucene.queryParser.QueryParser;
28
import org.apache.lucene.search.IndexSearcher;
29
import org.apache.lucene.search.Query;
30
import org.apache.lucene.search.ScoreDoc;
31
import org.apache.lucene.search.TopDocs;
32
import org.apache.lucene.store.Directory;
33
import org.apache.lucene.util.LuceneTestCase;
35
public class TestComplexPhraseQuery extends LuceneTestCase {
37
Analyzer analyzer = new MockAnalyzer(random);
39
DocData docsContent[] = { new DocData("john smith", "1"),
40
new DocData("johathon smith", "2"),
41
new DocData("john percival smith", "3"),
42
new DocData("jackson waits tom", "4") };
44
private IndexSearcher searcher;
46
String defaultFieldName = "name";
48
public void testComplexPhrases() throws Exception {
49
checkMatches("\"john smith\"", "1"); // Simple multi-term still works
50
checkMatches("\"j* smyth~\"", "1,2"); // wildcards and fuzzies are OK in
52
checkMatches("\"(jo* -john) smith\"", "2"); // boolean logic works
53
checkMatches("\"jo* smith\"~2", "1,2,3"); // position logic works.
54
checkMatches("\"jo* [sma TO smZ]\" ", "1,2"); // range queries supported
55
checkMatches("\"john\"", "1,3"); // Simple single-term still works
56
checkMatches("\"(john OR johathon) smith\"", "1,2"); // boolean logic with
58
checkMatches("\"(jo* -john) smyth~\"", "2"); // boolean logic with
61
// checkMatches("\"john -percival\"", "1"); // not logic doesn't work
64
checkMatches("\"john nosuchword*\"", ""); // phrases with clauses producing
67
checkBadQuery("\"jo* id:1 smith\""); // mixing fields in a phrase is bad
68
checkBadQuery("\"jo* \"smith\" \""); // phrases inside phrases is bad
71
private void checkBadQuery(String qString) {
72
QueryParser qp = new ComplexPhraseQueryParser(TEST_VERSION_CURRENT, defaultFieldName, analyzer);
73
Throwable expected = null;
76
} catch (Throwable e) {
79
assertNotNull("Expected parse error in " + qString, expected);
83
private void checkMatches(String qString, String expectedVals)
85
QueryParser qp = new ComplexPhraseQueryParser(TEST_VERSION_CURRENT, defaultFieldName, analyzer);
86
qp.setFuzzyPrefixLength(1); // usually a good idea
88
Query q = qp.parse(qString);
90
HashSet<String> expecteds = new HashSet<String>();
91
String[] vals = expectedVals.split(",");
92
for (int i = 0; i < vals.length; i++) {
93
if (vals[i].length() > 0)
94
expecteds.add(vals[i]);
97
TopDocs td = searcher.search(q, 10);
98
ScoreDoc[] sd = td.scoreDocs;
99
for (int i = 0; i < sd.length; i++) {
100
Document doc = searcher.doc(sd[i].doc);
101
String id = doc.get("id");
102
assertTrue(qString + "matched doc#" + id + " not expected", expecteds
104
expecteds.remove(id);
107
assertEquals(qString + " missing some matches ", 0, expecteds.size());
112
public void setUp() throws Exception {
115
IndexWriter w = new IndexWriter(rd, newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer));
116
for (int i = 0; i < docsContent.length; i++) {
117
Document doc = new Document();
118
doc.add(newField("name", docsContent[i].name, Field.Store.YES,
119
Field.Index.ANALYZED));
120
doc.add(newField("id", docsContent[i].id, Field.Store.YES,
121
Field.Index.ANALYZED));
125
searcher = new IndexSearcher(rd, true);
129
public void tearDown() throws Exception {
135
static class DocData {
140
public DocData(String name, String id) {