mirror of https://github.com/apache/lucene.git
LUCENE-3273: Converted all Lucene core tests away from using QueryParser
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1143231 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
470154ca59
commit
e91f6033fe
|
@ -25,12 +25,7 @@ import org.apache.lucene.document.Document;
|
||||||
import org.apache.lucene.document.Field;
|
import org.apache.lucene.document.Field;
|
||||||
import org.apache.lucene.index.Term;
|
import org.apache.lucene.index.Term;
|
||||||
import org.apache.lucene.index.RandomIndexWriter;
|
import org.apache.lucene.index.RandomIndexWriter;
|
||||||
import org.apache.lucene.queryParser.ParseException;
|
import org.apache.lucene.search.*;
|
||||||
import org.apache.lucene.queryParser.QueryParser;
|
|
||||||
import org.apache.lucene.search.TopDocs;
|
|
||||||
import org.apache.lucene.search.IndexSearcher;
|
|
||||||
import org.apache.lucene.search.Query;
|
|
||||||
import org.apache.lucene.search.TermQuery;
|
|
||||||
import org.apache.lucene.store.Directory;
|
import org.apache.lucene.store.Directory;
|
||||||
import org.apache.lucene.util.LuceneTestCase;
|
import org.apache.lucene.util.LuceneTestCase;
|
||||||
|
|
||||||
|
@ -42,7 +37,7 @@ import org.apache.lucene.util.LuceneTestCase;
|
||||||
*/
|
*/
|
||||||
public class TestDemo extends LuceneTestCase {
|
public class TestDemo extends LuceneTestCase {
|
||||||
|
|
||||||
public void testDemo() throws IOException, ParseException {
|
public void testDemo() throws IOException {
|
||||||
Analyzer analyzer = new MockAnalyzer(random);
|
Analyzer analyzer = new MockAnalyzer(random);
|
||||||
|
|
||||||
// Store the index in memory:
|
// Store the index in memory:
|
||||||
|
@ -63,9 +58,7 @@ public class TestDemo extends LuceneTestCase {
|
||||||
IndexSearcher isearcher = new IndexSearcher(directory, true); // read-only=true
|
IndexSearcher isearcher = new IndexSearcher(directory, true); // read-only=true
|
||||||
|
|
||||||
assertEquals(1, isearcher.search(new TermQuery(new Term("fieldname", longTerm)), 1).totalHits);
|
assertEquals(1, isearcher.search(new TermQuery(new Term("fieldname", longTerm)), 1).totalHits);
|
||||||
// Parse a simple query that searches for "text":
|
Query query = new TermQuery(new Term("fieldname", "text"));
|
||||||
QueryParser parser = new QueryParser(TEST_VERSION_CURRENT, "fieldname", analyzer);
|
|
||||||
Query query = parser.parse("text");
|
|
||||||
TopDocs hits = isearcher.search(query, null, 1);
|
TopDocs hits = isearcher.search(query, null, 1);
|
||||||
assertEquals(1, hits.totalHits);
|
assertEquals(1, hits.totalHits);
|
||||||
// Iterate through the results:
|
// Iterate through the results:
|
||||||
|
@ -75,8 +68,10 @@ public class TestDemo extends LuceneTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test simple phrase query
|
// Test simple phrase query
|
||||||
query = parser.parse("\"to be\"");
|
PhraseQuery phraseQuery = new PhraseQuery();
|
||||||
assertEquals(1, isearcher.search(query, null, 1).totalHits);
|
phraseQuery.add(new Term("fieldname", "to"));
|
||||||
|
phraseQuery.add(new Term("fieldname", "be"));
|
||||||
|
assertEquals(1, isearcher.search(phraseQuery, null, 1).totalHits);
|
||||||
|
|
||||||
isearcher.close();
|
isearcher.close();
|
||||||
directory.close();
|
directory.close();
|
||||||
|
|
|
@ -17,6 +17,8 @@ package org.apache.lucene;
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
|
@ -30,7 +32,6 @@ import org.apache.lucene.document.*;
|
||||||
import org.apache.lucene.analysis.*;
|
import org.apache.lucene.analysis.*;
|
||||||
import org.apache.lucene.index.*;
|
import org.apache.lucene.index.*;
|
||||||
import org.apache.lucene.search.*;
|
import org.apache.lucene.search.*;
|
||||||
import org.apache.lucene.queryParser.*;
|
|
||||||
|
|
||||||
/** JUnit adaptation of an older test case SearchTest. */
|
/** JUnit adaptation of an older test case SearchTest. */
|
||||||
public class TestSearch extends LuceneTestCase {
|
public class TestSearch extends LuceneTestCase {
|
||||||
|
@ -100,24 +101,13 @@ public class TestSearch extends LuceneTestCase {
|
||||||
|
|
||||||
IndexSearcher searcher = new IndexSearcher(directory, true);
|
IndexSearcher searcher = new IndexSearcher(directory, true);
|
||||||
|
|
||||||
String[] queries = {
|
|
||||||
"a b",
|
|
||||||
"\"a b\"",
|
|
||||||
"\"a b c\"",
|
|
||||||
"a c",
|
|
||||||
"\"a c\"",
|
|
||||||
"\"a c e\"",
|
|
||||||
};
|
|
||||||
ScoreDoc[] hits = null;
|
ScoreDoc[] hits = null;
|
||||||
|
|
||||||
Sort sort = new Sort(new SortField[] {
|
Sort sort = new Sort(new SortField[] {
|
||||||
SortField.FIELD_SCORE,
|
SortField.FIELD_SCORE,
|
||||||
new SortField("id", SortField.Type.INT)});
|
new SortField("id", SortField.Type.INT)});
|
||||||
|
|
||||||
QueryParser parser = new QueryParser(TEST_VERSION_CURRENT, "contents", analyzer);
|
for (Query query : buildQueries()) {
|
||||||
parser.setPhraseSlop(4);
|
|
||||||
for (int j = 0; j < queries.length; j++) {
|
|
||||||
Query query = parser.parse(queries[j]);
|
|
||||||
out.println("Query: " + query.toString("contents"));
|
out.println("Query: " + query.toString("contents"));
|
||||||
if (VERBOSE) {
|
if (VERBOSE) {
|
||||||
System.out.println("TEST: query=" + query);
|
System.out.println("TEST: query=" + query);
|
||||||
|
@ -134,4 +124,42 @@ public class TestSearch extends LuceneTestCase {
|
||||||
searcher.close();
|
searcher.close();
|
||||||
directory.close();
|
directory.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private List<Query> buildQueries() {
|
||||||
|
List<Query> queries = new ArrayList<Query>();
|
||||||
|
|
||||||
|
BooleanQuery booleanAB = new BooleanQuery();
|
||||||
|
booleanAB.add(new TermQuery(new Term("contents", "a")), BooleanClause.Occur.SHOULD);
|
||||||
|
booleanAB.add(new TermQuery(new Term("contents", "b")), BooleanClause.Occur.SHOULD);
|
||||||
|
queries.add(booleanAB);
|
||||||
|
|
||||||
|
PhraseQuery phraseAB = new PhraseQuery();
|
||||||
|
phraseAB.add(new Term("contents", "a"));
|
||||||
|
phraseAB.add(new Term("contents", "b"));
|
||||||
|
queries.add(phraseAB);
|
||||||
|
|
||||||
|
PhraseQuery phraseABC = new PhraseQuery();
|
||||||
|
phraseABC.add(new Term("contents", "a"));
|
||||||
|
phraseABC.add(new Term("contents", "b"));
|
||||||
|
phraseABC.add(new Term("contents", "c"));
|
||||||
|
queries.add(phraseABC);
|
||||||
|
|
||||||
|
BooleanQuery booleanAC = new BooleanQuery();
|
||||||
|
booleanAC.add(new TermQuery(new Term("contents", "a")), BooleanClause.Occur.SHOULD);
|
||||||
|
booleanAC.add(new TermQuery(new Term("contents", "c")), BooleanClause.Occur.SHOULD);
|
||||||
|
queries.add(booleanAC);
|
||||||
|
|
||||||
|
PhraseQuery phraseAC = new PhraseQuery();
|
||||||
|
phraseAC.add(new Term("contents", "a"));
|
||||||
|
phraseAC.add(new Term("contents", "c"));
|
||||||
|
queries.add(phraseAC);
|
||||||
|
|
||||||
|
PhraseQuery phraseACE = new PhraseQuery();
|
||||||
|
phraseACE.add(new Term("contents", "a"));
|
||||||
|
phraseACE.add(new Term("contents", "c"));
|
||||||
|
phraseACE.add(new Term("contents", "e"));
|
||||||
|
queries.add(phraseACE);
|
||||||
|
|
||||||
|
return queries;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,6 @@ import org.apache.lucene.document.*;
|
||||||
import org.apache.lucene.analysis.*;
|
import org.apache.lucene.analysis.*;
|
||||||
import org.apache.lucene.index.*;
|
import org.apache.lucene.index.*;
|
||||||
import org.apache.lucene.search.*;
|
import org.apache.lucene.search.*;
|
||||||
import org.apache.lucene.queryParser.*;
|
|
||||||
import org.apache.lucene.util.LuceneTestCase;
|
import org.apache.lucene.util.LuceneTestCase;
|
||||||
import junit.framework.TestSuite;
|
import junit.framework.TestSuite;
|
||||||
import junit.textui.TestRunner;
|
import junit.textui.TestRunner;
|
||||||
|
@ -102,9 +101,7 @@ public class TestSearchForDuplicates extends LuceneTestCase {
|
||||||
// try a search without OR
|
// try a search without OR
|
||||||
IndexSearcher searcher = new IndexSearcher(directory, true);
|
IndexSearcher searcher = new IndexSearcher(directory, true);
|
||||||
|
|
||||||
QueryParser parser = new QueryParser(TEST_VERSION_CURRENT, PRIORITY_FIELD, analyzer);
|
Query query = new TermQuery(new Term(PRIORITY_FIELD, HIGH_PRIORITY));
|
||||||
|
|
||||||
Query query = parser.parse(HIGH_PRIORITY);
|
|
||||||
out.println("Query: " + query.toString(PRIORITY_FIELD));
|
out.println("Query: " + query.toString(PRIORITY_FIELD));
|
||||||
if (VERBOSE) {
|
if (VERBOSE) {
|
||||||
System.out.println("TEST: search query=" + query);
|
System.out.println("TEST: search query=" + query);
|
||||||
|
@ -124,12 +121,12 @@ public class TestSearchForDuplicates extends LuceneTestCase {
|
||||||
searcher = new IndexSearcher(directory, true);
|
searcher = new IndexSearcher(directory, true);
|
||||||
hits = null;
|
hits = null;
|
||||||
|
|
||||||
parser = new QueryParser(TEST_VERSION_CURRENT, PRIORITY_FIELD, analyzer);
|
BooleanQuery booleanQuery = new BooleanQuery();
|
||||||
|
booleanQuery.add(new TermQuery(new Term(PRIORITY_FIELD, HIGH_PRIORITY)), BooleanClause.Occur.SHOULD);
|
||||||
|
booleanQuery.add(new TermQuery(new Term(PRIORITY_FIELD, MED_PRIORITY)), BooleanClause.Occur.SHOULD);
|
||||||
|
out.println("Query: " + booleanQuery.toString(PRIORITY_FIELD));
|
||||||
|
|
||||||
query = parser.parse(HIGH_PRIORITY + " OR " + MED_PRIORITY);
|
hits = searcher.search(booleanQuery, null, MAX_DOCS, sort).scoreDocs;
|
||||||
out.println("Query: " + query.toString(PRIORITY_FIELD));
|
|
||||||
|
|
||||||
hits = searcher.search(query, null, MAX_DOCS, sort).scoreDocs;
|
|
||||||
printHits(out, hits, searcher);
|
printHits(out, hits, searcher);
|
||||||
checkHits(hits, MAX_DOCS, searcher);
|
checkHits(hits, MAX_DOCS, searcher);
|
||||||
|
|
||||||
|
|
|
@ -42,11 +42,7 @@ import org.apache.lucene.index.Term;
|
||||||
import org.apache.lucene.index.codecs.CodecProvider;
|
import org.apache.lucene.index.codecs.CodecProvider;
|
||||||
import org.apache.lucene.index.codecs.PerDocValues;
|
import org.apache.lucene.index.codecs.PerDocValues;
|
||||||
import org.apache.lucene.index.values.IndexDocValues.Source;
|
import org.apache.lucene.index.values.IndexDocValues.Source;
|
||||||
import org.apache.lucene.queryParser.ParseException;
|
import org.apache.lucene.search.*;
|
||||||
import org.apache.lucene.queryParser.QueryParser;
|
|
||||||
import org.apache.lucene.search.IndexSearcher;
|
|
||||||
import org.apache.lucene.search.ScoreDoc;
|
|
||||||
import org.apache.lucene.search.TopDocs;
|
|
||||||
import org.apache.lucene.store.Directory;
|
import org.apache.lucene.store.Directory;
|
||||||
import org.apache.lucene.store.LockObtainFailedException;
|
import org.apache.lucene.store.LockObtainFailedException;
|
||||||
import org.apache.lucene.util.BytesRef;
|
import org.apache.lucene.util.BytesRef;
|
||||||
|
@ -77,8 +73,7 @@ public class TestDocValuesIndexing extends LuceneTestCase {
|
||||||
/*
|
/*
|
||||||
* Simple test case to show how to use the API
|
* Simple test case to show how to use the API
|
||||||
*/
|
*/
|
||||||
public void testDocValuesSimple() throws CorruptIndexException, IOException,
|
public void testDocValuesSimple() throws CorruptIndexException, IOException {
|
||||||
ParseException {
|
|
||||||
Directory dir = newDirectory();
|
Directory dir = newDirectory();
|
||||||
IndexWriter writer = new IndexWriter(dir, writerConfig(false));
|
IndexWriter writer = new IndexWriter(dir, writerConfig(false));
|
||||||
for (int i = 0; i < 5; i++) {
|
for (int i = 0; i < 5; i++) {
|
||||||
|
@ -98,9 +93,15 @@ public class TestDocValuesIndexing extends LuceneTestCase {
|
||||||
assertTrue(reader.isOptimized());
|
assertTrue(reader.isOptimized());
|
||||||
|
|
||||||
IndexSearcher searcher = new IndexSearcher(reader);
|
IndexSearcher searcher = new IndexSearcher(reader);
|
||||||
QueryParser parser = new QueryParser(TEST_VERSION_CURRENT, "docId",
|
|
||||||
new MockAnalyzer(random));
|
BooleanQuery query = new BooleanQuery();
|
||||||
TopDocs search = searcher.search(parser.parse("0 OR 1 OR 2 OR 3 OR 4"), 10);
|
query.add(new TermQuery(new Term("docId", "0")), BooleanClause.Occur.SHOULD);
|
||||||
|
query.add(new TermQuery(new Term("docId", "1")), BooleanClause.Occur.SHOULD);
|
||||||
|
query.add(new TermQuery(new Term("docId", "2")), BooleanClause.Occur.SHOULD);
|
||||||
|
query.add(new TermQuery(new Term("docId", "3")), BooleanClause.Occur.SHOULD);
|
||||||
|
query.add(new TermQuery(new Term("docId", "4")), BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
TopDocs search = searcher.search(query, 10);
|
||||||
assertEquals(5, search.totalHits);
|
assertEquals(5, search.totalHits);
|
||||||
ScoreDoc[] scoreDocs = search.scoreDocs;
|
ScoreDoc[] scoreDocs = search.scoreDocs;
|
||||||
IndexDocValues docValues = MultiPerDocValues.getPerDocs(reader).docValues("docId");
|
IndexDocValues docValues = MultiPerDocValues.getPerDocs(reader).docValues("docId");
|
||||||
|
|
|
@ -0,0 +1,105 @@
|
||||||
|
package org.apache.lucene.queryParser;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright ownership.
|
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||||
|
* (the "License"); you may not use this file except in compliance with
|
||||||
|
* the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import org.apache.lucene.analysis.Analyzer;
|
||||||
|
import org.apache.lucene.analysis.TokenStream;
|
||||||
|
import org.apache.lucene.analysis.Tokenizer;
|
||||||
|
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
|
||||||
|
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
|
||||||
|
import org.apache.lucene.index.Term;
|
||||||
|
import org.apache.lucene.search.MultiPhraseQuery;
|
||||||
|
import org.apache.lucene.search.Query;
|
||||||
|
import org.apache.lucene.util.LuceneTestCase;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.Reader;
|
||||||
|
|
||||||
|
public class TestMultiPhraseQueryParsing extends LuceneTestCase {
|
||||||
|
|
||||||
|
private static class TokenAndPos {
|
||||||
|
public final String token;
|
||||||
|
public final int pos;
|
||||||
|
public TokenAndPos(String token, int pos) {
|
||||||
|
this.token = token;
|
||||||
|
this.pos = pos;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class CannedAnalyzer extends Analyzer {
|
||||||
|
private final TokenAndPos[] tokens;
|
||||||
|
|
||||||
|
public CannedAnalyzer(TokenAndPos[] tokens) {
|
||||||
|
this.tokens = tokens;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TokenStream tokenStream(String fieldName, Reader reader) {
|
||||||
|
return new CannedTokenizer(tokens);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class CannedTokenizer extends Tokenizer {
|
||||||
|
private final TokenAndPos[] tokens;
|
||||||
|
private int upto = 0;
|
||||||
|
private int lastPos = 0;
|
||||||
|
private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
|
||||||
|
private final PositionIncrementAttribute posIncrAtt = addAttribute(PositionIncrementAttribute.class);
|
||||||
|
|
||||||
|
public CannedTokenizer(TokenAndPos[] tokens) {
|
||||||
|
this.tokens = tokens;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final boolean incrementToken() throws IOException {
|
||||||
|
clearAttributes();
|
||||||
|
if (upto < tokens.length) {
|
||||||
|
final TokenAndPos token = tokens[upto++];
|
||||||
|
termAtt.setEmpty();
|
||||||
|
termAtt.append(token.token);
|
||||||
|
posIncrAtt.setPositionIncrement(token.pos - lastPos);
|
||||||
|
lastPos = token.pos;
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testMultiPhraseQueryParsing() throws Exception {
|
||||||
|
TokenAndPos[] INCR_0_QUERY_TOKENS_AND = new TokenAndPos[]{
|
||||||
|
new TokenAndPos("a", 0),
|
||||||
|
new TokenAndPos("1", 0),
|
||||||
|
new TokenAndPos("b", 1),
|
||||||
|
new TokenAndPos("1", 1),
|
||||||
|
new TokenAndPos("c", 2)
|
||||||
|
};
|
||||||
|
|
||||||
|
QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, "field", new CannedAnalyzer(INCR_0_QUERY_TOKENS_AND));
|
||||||
|
Query q = qp.parse("\"this text is acually ignored\"");
|
||||||
|
assertTrue("wrong query type!", q instanceof MultiPhraseQuery);
|
||||||
|
|
||||||
|
MultiPhraseQuery multiPhraseQuery = new MultiPhraseQuery();
|
||||||
|
multiPhraseQuery.add(new Term[]{ new Term("field", "a"), new Term("field", "1") }, -1);
|
||||||
|
multiPhraseQuery.add(new Term[]{ new Term("field", "b"), new Term("field", "1") }, 0);
|
||||||
|
multiPhraseQuery.add(new Term[]{ new Term("field", "c") }, 1);
|
||||||
|
|
||||||
|
assertEquals(multiPhraseQuery, q);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -19,7 +19,6 @@ package org.apache.lucene.queryParser;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
import java.text.Collator;
|
|
||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
@ -42,24 +41,10 @@ import org.apache.lucene.document.Field;
|
||||||
import org.apache.lucene.index.IndexWriter;
|
import org.apache.lucene.index.IndexWriter;
|
||||||
import org.apache.lucene.index.Term;
|
import org.apache.lucene.index.Term;
|
||||||
import org.apache.lucene.index.IndexReader;
|
import org.apache.lucene.index.IndexReader;
|
||||||
import org.apache.lucene.search.BooleanQuery;
|
import org.apache.lucene.search.*;
|
||||||
import org.apache.lucene.search.BooleanClause;
|
|
||||||
import org.apache.lucene.search.MultiTermQuery;
|
|
||||||
import org.apache.lucene.search.FuzzyQuery;
|
|
||||||
import org.apache.lucene.search.IndexSearcher;
|
|
||||||
import org.apache.lucene.search.MatchAllDocsQuery;
|
|
||||||
import org.apache.lucene.search.PhraseQuery;
|
|
||||||
import org.apache.lucene.search.PrefixQuery;
|
|
||||||
import org.apache.lucene.search.Query;
|
|
||||||
import org.apache.lucene.search.RegexpQuery;
|
|
||||||
import org.apache.lucene.search.TermRangeQuery;
|
|
||||||
import org.apache.lucene.search.ScoreDoc;
|
|
||||||
import org.apache.lucene.search.TermQuery;
|
|
||||||
import org.apache.lucene.search.WildcardQuery;
|
|
||||||
import org.apache.lucene.search.BooleanClause.Occur;
|
import org.apache.lucene.search.BooleanClause.Occur;
|
||||||
import org.apache.lucene.store.Directory;
|
import org.apache.lucene.store.Directory;
|
||||||
import org.apache.lucene.util.LuceneTestCase;
|
import org.apache.lucene.util.LuceneTestCase;
|
||||||
import org.apache.lucene.util.Version;
|
|
||||||
import org.apache.lucene.util.automaton.BasicAutomata;
|
import org.apache.lucene.util.automaton.BasicAutomata;
|
||||||
import org.apache.lucene.util.automaton.CharacterRunAutomaton;
|
import org.apache.lucene.util.automaton.CharacterRunAutomaton;
|
||||||
import org.apache.lucene.util.automaton.RegExp;
|
import org.apache.lucene.util.automaton.RegExp;
|
||||||
|
@ -1253,4 +1238,96 @@ public class TestQueryParser extends LuceneTestCase {
|
||||||
Query actual = qp.parse("[abc TO def]");
|
Query actual = qp.parse("[abc TO def]");
|
||||||
assertEquals(expected, actual);
|
assertEquals(expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testDistanceAsEditsParsing() throws Exception {
|
||||||
|
QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, "field", new MockAnalyzer(random));
|
||||||
|
FuzzyQuery q = (FuzzyQuery) qp.parse("foobar~2");
|
||||||
|
assertEquals(2f, q.getMinSimilarity(), 0.0001f);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testPhraseQueryToString() throws ParseException {
|
||||||
|
Analyzer analyzer = new MockAnalyzer(random, MockTokenizer.SIMPLE, true, MockTokenFilter.ENGLISH_STOPSET, true);
|
||||||
|
QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, "field", analyzer);
|
||||||
|
qp.setEnablePositionIncrements(true);
|
||||||
|
PhraseQuery q = (PhraseQuery)qp.parse("\"this hi this is a test is\"");
|
||||||
|
assertEquals("field:\"? hi ? ? ? test\"", q.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testParseWildcardAndPhraseQueries() throws ParseException {
|
||||||
|
String field = "content";
|
||||||
|
QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, field, new MockAnalyzer(random));
|
||||||
|
qp.setAllowLeadingWildcard(true);
|
||||||
|
|
||||||
|
String prefixQueries[][] = {
|
||||||
|
{"a*", "ab*", "abc*",},
|
||||||
|
{"h*", "hi*", "hij*", "\\\\7*"},
|
||||||
|
{"o*", "op*", "opq*", "\\\\\\\\*"},
|
||||||
|
};
|
||||||
|
|
||||||
|
String wildcardQueries[][] = {
|
||||||
|
{"*a*", "*ab*", "*abc**", "ab*e*", "*g?", "*f?1", "abc**"},
|
||||||
|
{"*h*", "*hi*", "*hij**", "hi*k*", "*n?", "*m?1", "hij**"},
|
||||||
|
{"*o*", "*op*", "*opq**", "op*q*", "*u?", "*t?1", "opq**"},
|
||||||
|
};
|
||||||
|
|
||||||
|
// test queries that must be prefix queries
|
||||||
|
for (int i = 0; i < prefixQueries.length; i++) {
|
||||||
|
for (int j = 0; j < prefixQueries[i].length; j++) {
|
||||||
|
String queryString = prefixQueries[i][j];
|
||||||
|
Query q = qp.parse(queryString);
|
||||||
|
assertEquals(PrefixQuery.class, q.getClass());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// test queries that must be wildcard queries
|
||||||
|
for (int i = 0; i < wildcardQueries.length; i++) {
|
||||||
|
for (int j = 0; j < wildcardQueries[i].length; j++) {
|
||||||
|
String qtxt = wildcardQueries[i][j];
|
||||||
|
Query q = qp.parse(qtxt);
|
||||||
|
assertEquals(WildcardQuery.class, q.getClass());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testPhraseQueryPositionIncrements() throws Exception {
|
||||||
|
CharacterRunAutomaton stopStopList =
|
||||||
|
new CharacterRunAutomaton(new RegExp("[sS][tT][oO][pP]").toAutomaton());
|
||||||
|
|
||||||
|
QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, "field",
|
||||||
|
new MockAnalyzer(random, MockTokenizer.WHITESPACE, false, stopStopList, false));
|
||||||
|
|
||||||
|
PhraseQuery phraseQuery = new PhraseQuery();
|
||||||
|
phraseQuery.add(new Term("field", "1"));
|
||||||
|
phraseQuery.add(new Term("field", "2"));
|
||||||
|
|
||||||
|
assertEquals(phraseQuery, qp.parse("\"1 2\""));
|
||||||
|
assertEquals(phraseQuery, qp.parse("\"1 stop 2\""));
|
||||||
|
|
||||||
|
qp.setEnablePositionIncrements(true);
|
||||||
|
assertEquals(phraseQuery, qp.parse("\"1 stop 2\""));
|
||||||
|
|
||||||
|
qp.setEnablePositionIncrements(false);
|
||||||
|
assertEquals(phraseQuery, qp.parse("\"1 stop 2\""));
|
||||||
|
|
||||||
|
qp = new QueryParser(TEST_VERSION_CURRENT, "field",
|
||||||
|
new MockAnalyzer(random, MockTokenizer.WHITESPACE, false, stopStopList, true));
|
||||||
|
qp.setEnablePositionIncrements(true);
|
||||||
|
|
||||||
|
phraseQuery = new PhraseQuery();
|
||||||
|
phraseQuery.add(new Term("field", "1"));
|
||||||
|
phraseQuery.add(new Term("field", "2"), 2);
|
||||||
|
assertEquals(phraseQuery, qp.parse("\"1 stop 2\""));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testMatchAllQueryParsing() throws Exception {
|
||||||
|
// test simple parsing of MatchAllDocsQuery
|
||||||
|
QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, "key", new MockAnalyzer(random));
|
||||||
|
assertEquals(new MatchAllDocsQuery(), qp.parse(new MatchAllDocsQuery().toString()));
|
||||||
|
|
||||||
|
// test parsing with non-default boost
|
||||||
|
MatchAllDocsQuery query = new MatchAllDocsQuery();
|
||||||
|
query.setBoost(2.3f);
|
||||||
|
assertEquals(query, qp.parse(query.toString()));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,8 +26,6 @@ import org.apache.lucene.document.Field;
|
||||||
import org.apache.lucene.index.RandomIndexWriter;
|
import org.apache.lucene.index.RandomIndexWriter;
|
||||||
import org.apache.lucene.index.Term;
|
import org.apache.lucene.index.Term;
|
||||||
import org.apache.lucene.index.IndexReader;
|
import org.apache.lucene.index.IndexReader;
|
||||||
import org.apache.lucene.queryParser.ParseException;
|
|
||||||
import org.apache.lucene.queryParser.QueryParser;
|
|
||||||
import org.apache.lucene.store.Directory;
|
import org.apache.lucene.store.Directory;
|
||||||
import org.apache.lucene.store.MockDirectoryWrapper;
|
import org.apache.lucene.store.MockDirectoryWrapper;
|
||||||
import org.apache.lucene.store.RAMDirectory;
|
import org.apache.lucene.store.RAMDirectory;
|
||||||
|
@ -117,16 +115,7 @@ public class TestBoolean2 extends LuceneTestCase {
|
||||||
"w1 w3 xx w2 yy w3"
|
"w1 w3 xx w2 yy w3"
|
||||||
};
|
};
|
||||||
|
|
||||||
public Query makeQuery(String queryText) throws ParseException {
|
public void queriesTest(Query query, int[] expDocNrs) throws Exception {
|
||||||
Query q = (new QueryParser(TEST_VERSION_CURRENT, field, new MockAnalyzer(random))).parse(queryText);
|
|
||||||
return q;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void queriesTest(String queryText, int[] expDocNrs) throws Exception {
|
|
||||||
//System.out.println();
|
|
||||||
//System.out.println("Query: " + queryText);
|
|
||||||
|
|
||||||
Query query = makeQuery(queryText);
|
|
||||||
TopScoreDocCollector collector = TopScoreDocCollector.create(1000, false);
|
TopScoreDocCollector collector = TopScoreDocCollector.create(1000, false);
|
||||||
searcher.search(query, null, collector);
|
searcher.search(query, null, collector);
|
||||||
ScoreDoc[] hits1 = collector.topDocs().scoreDocs;
|
ScoreDoc[] hits1 = collector.topDocs().scoreDocs;
|
||||||
|
@ -143,70 +132,98 @@ public class TestBoolean2 extends LuceneTestCase {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testQueries01() throws Exception {
|
public void testQueries01() throws Exception {
|
||||||
String queryText = "+w3 +xx";
|
BooleanQuery query = new BooleanQuery();
|
||||||
|
query.add(new TermQuery(new Term(field, "w3")), BooleanClause.Occur.MUST);
|
||||||
|
query.add(new TermQuery(new Term(field, "xx")), BooleanClause.Occur.MUST);
|
||||||
int[] expDocNrs = {2,3};
|
int[] expDocNrs = {2,3};
|
||||||
queriesTest(queryText, expDocNrs);
|
queriesTest(query, expDocNrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testQueries02() throws Exception {
|
public void testQueries02() throws Exception {
|
||||||
String queryText = "+w3 xx";
|
BooleanQuery query = new BooleanQuery();
|
||||||
|
query.add(new TermQuery(new Term(field, "w3")), BooleanClause.Occur.MUST);
|
||||||
|
query.add(new TermQuery(new Term(field, "xx")), BooleanClause.Occur.SHOULD);
|
||||||
int[] expDocNrs = {2,3,1,0};
|
int[] expDocNrs = {2,3,1,0};
|
||||||
queriesTest(queryText, expDocNrs);
|
queriesTest(query, expDocNrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testQueries03() throws Exception {
|
public void testQueries03() throws Exception {
|
||||||
String queryText = "w3 xx";
|
BooleanQuery query = new BooleanQuery();
|
||||||
|
query.add(new TermQuery(new Term(field, "w3")), BooleanClause.Occur.SHOULD);
|
||||||
|
query.add(new TermQuery(new Term(field, "xx")), BooleanClause.Occur.SHOULD);
|
||||||
int[] expDocNrs = {2,3,1,0};
|
int[] expDocNrs = {2,3,1,0};
|
||||||
queriesTest(queryText, expDocNrs);
|
queriesTest(query, expDocNrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testQueries04() throws Exception {
|
public void testQueries04() throws Exception {
|
||||||
String queryText = "w3 -xx";
|
BooleanQuery query = new BooleanQuery();
|
||||||
|
query.add(new TermQuery(new Term(field, "w3")), BooleanClause.Occur.SHOULD);
|
||||||
|
query.add(new TermQuery(new Term(field, "xx")), BooleanClause.Occur.MUST_NOT);
|
||||||
int[] expDocNrs = {1,0};
|
int[] expDocNrs = {1,0};
|
||||||
queriesTest(queryText, expDocNrs);
|
queriesTest(query, expDocNrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testQueries05() throws Exception {
|
public void testQueries05() throws Exception {
|
||||||
String queryText = "+w3 -xx";
|
BooleanQuery query = new BooleanQuery();
|
||||||
|
query.add(new TermQuery(new Term(field, "w3")), BooleanClause.Occur.MUST);
|
||||||
|
query.add(new TermQuery(new Term(field, "xx")), BooleanClause.Occur.MUST_NOT);
|
||||||
int[] expDocNrs = {1,0};
|
int[] expDocNrs = {1,0};
|
||||||
queriesTest(queryText, expDocNrs);
|
queriesTest(query, expDocNrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testQueries06() throws Exception {
|
public void testQueries06() throws Exception {
|
||||||
String queryText = "+w3 -xx -w5";
|
BooleanQuery query = new BooleanQuery();
|
||||||
|
query.add(new TermQuery(new Term(field, "w3")), BooleanClause.Occur.MUST);
|
||||||
|
query.add(new TermQuery(new Term(field, "xx")), BooleanClause.Occur.MUST_NOT);
|
||||||
|
query.add(new TermQuery(new Term(field, "w5")), BooleanClause.Occur.MUST_NOT);
|
||||||
int[] expDocNrs = {1};
|
int[] expDocNrs = {1};
|
||||||
queriesTest(queryText, expDocNrs);
|
queriesTest(query, expDocNrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testQueries07() throws Exception {
|
public void testQueries07() throws Exception {
|
||||||
String queryText = "-w3 -xx -w5";
|
BooleanQuery query = new BooleanQuery();
|
||||||
|
query.add(new TermQuery(new Term(field, "w3")), BooleanClause.Occur.MUST_NOT);
|
||||||
|
query.add(new TermQuery(new Term(field, "xx")), BooleanClause.Occur.MUST_NOT);
|
||||||
|
query.add(new TermQuery(new Term(field, "w5")), BooleanClause.Occur.MUST_NOT);
|
||||||
int[] expDocNrs = {};
|
int[] expDocNrs = {};
|
||||||
queriesTest(queryText, expDocNrs);
|
queriesTest(query, expDocNrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testQueries08() throws Exception {
|
public void testQueries08() throws Exception {
|
||||||
String queryText = "+w3 xx -w5";
|
BooleanQuery query = new BooleanQuery();
|
||||||
|
query.add(new TermQuery(new Term(field, "w3")), BooleanClause.Occur.MUST);
|
||||||
|
query.add(new TermQuery(new Term(field, "xx")), BooleanClause.Occur.SHOULD);
|
||||||
|
query.add(new TermQuery(new Term(field, "w5")), BooleanClause.Occur.MUST_NOT);
|
||||||
int[] expDocNrs = {2,3,1};
|
int[] expDocNrs = {2,3,1};
|
||||||
queriesTest(queryText, expDocNrs);
|
queriesTest(query, expDocNrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testQueries09() throws Exception {
|
public void testQueries09() throws Exception {
|
||||||
String queryText = "+w3 +xx +w2 zz";
|
BooleanQuery query = new BooleanQuery();
|
||||||
|
query.add(new TermQuery(new Term(field, "w3")), BooleanClause.Occur.MUST);
|
||||||
|
query.add(new TermQuery(new Term(field, "xx")), BooleanClause.Occur.MUST);
|
||||||
|
query.add(new TermQuery(new Term(field, "w2")), BooleanClause.Occur.MUST);
|
||||||
|
query.add(new TermQuery(new Term(field, "zz")), BooleanClause.Occur.SHOULD);
|
||||||
int[] expDocNrs = {2, 3};
|
int[] expDocNrs = {2, 3};
|
||||||
queriesTest(queryText, expDocNrs);
|
queriesTest(query, expDocNrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testQueries10() throws Exception {
|
public void testQueries10() throws Exception {
|
||||||
String queryText = "+w3 +xx +w2 zz";
|
BooleanQuery query = new BooleanQuery();
|
||||||
|
query.add(new TermQuery(new Term(field, "w3")), BooleanClause.Occur.MUST);
|
||||||
|
query.add(new TermQuery(new Term(field, "xx")), BooleanClause.Occur.MUST);
|
||||||
|
query.add(new TermQuery(new Term(field, "w2")), BooleanClause.Occur.MUST);
|
||||||
|
query.add(new TermQuery(new Term(field, "zz")), BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
int[] expDocNrs = {2, 3};
|
int[] expDocNrs = {2, 3};
|
||||||
SimilarityProvider oldSimilarity = searcher.getSimilarityProvider();
|
SimilarityProvider oldSimilarity = searcher.getSimilarityProvider();
|
||||||
try {
|
try {
|
||||||
|
@ -216,7 +233,7 @@ public class TestBoolean2 extends LuceneTestCase {
|
||||||
return overlap / ((float)maxOverlap - 1);
|
return overlap / ((float)maxOverlap - 1);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
queriesTest(queryText, expDocNrs);
|
queriesTest(query, expDocNrs);
|
||||||
} finally {
|
} finally {
|
||||||
searcher.setSimilarityProvider(oldSimilarity);
|
searcher.setSimilarityProvider(oldSimilarity);
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,6 @@ import org.apache.lucene.index.IndexReader;
|
||||||
import org.apache.lucene.index.MultiReader;
|
import org.apache.lucene.index.MultiReader;
|
||||||
import org.apache.lucene.index.RandomIndexWriter;
|
import org.apache.lucene.index.RandomIndexWriter;
|
||||||
import org.apache.lucene.index.Term;
|
import org.apache.lucene.index.Term;
|
||||||
import org.apache.lucene.queryParser.QueryParser;
|
|
||||||
import org.apache.lucene.store.Directory;
|
import org.apache.lucene.store.Directory;
|
||||||
import org.apache.lucene.util.LuceneTestCase;
|
import org.apache.lucene.util.LuceneTestCase;
|
||||||
import org.apache.lucene.util.NamedThreadFactory;
|
import org.apache.lucene.util.NamedThreadFactory;
|
||||||
|
@ -143,18 +142,21 @@ public class TestBooleanQuery extends LuceneTestCase {
|
||||||
IndexReader reader2 = iw2.getReader();
|
IndexReader reader2 = iw2.getReader();
|
||||||
iw2.close();
|
iw2.close();
|
||||||
|
|
||||||
QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, "field", new MockAnalyzer(random));
|
BooleanQuery query = new BooleanQuery(); // Query: +foo -ba*
|
||||||
qp.setMultiTermRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);
|
query.add(new TermQuery(new Term("field", "foo")), BooleanClause.Occur.MUST);
|
||||||
|
WildcardQuery wildcardQuery = new WildcardQuery(new Term("field", "ba*"));
|
||||||
|
wildcardQuery.setRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE);
|
||||||
|
query.add(wildcardQuery, BooleanClause.Occur.MUST_NOT);
|
||||||
|
|
||||||
MultiReader multireader = new MultiReader(reader1, reader2);
|
MultiReader multireader = new MultiReader(reader1, reader2);
|
||||||
IndexSearcher searcher = new IndexSearcher(multireader);
|
IndexSearcher searcher = new IndexSearcher(multireader);
|
||||||
assertEquals(0, searcher.search(qp.parse("+foo -ba*"), 10).totalHits);
|
assertEquals(0, searcher.search(query, 10).totalHits);
|
||||||
|
|
||||||
final ExecutorService es = Executors.newCachedThreadPool(new NamedThreadFactory("NRT search threads"));
|
final ExecutorService es = Executors.newCachedThreadPool(new NamedThreadFactory("NRT search threads"));
|
||||||
searcher = new IndexSearcher(multireader, es);
|
searcher = new IndexSearcher(multireader, es);
|
||||||
if (VERBOSE)
|
if (VERBOSE)
|
||||||
System.out.println("rewritten form: " + searcher.rewrite(qp.parse("+foo -ba*")));
|
System.out.println("rewritten form: " + searcher.rewrite(query));
|
||||||
assertEquals(0, searcher.search(qp.parse("+foo -ba*"), 10).totalHits);
|
assertEquals(0, searcher.search(query, 10).totalHits);
|
||||||
es.shutdown();
|
es.shutdown();
|
||||||
es.awaitTermination(1, TimeUnit.SECONDS);
|
es.awaitTermination(1, TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@ package org.apache.lucene.search;
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import org.apache.lucene.index.Term;
|
||||||
import org.apache.lucene.search.BooleanClause.Occur;
|
import org.apache.lucene.search.BooleanClause.Occur;
|
||||||
import org.apache.lucene.search.spans.*;
|
import org.apache.lucene.search.spans.*;
|
||||||
|
|
||||||
|
@ -58,7 +59,11 @@ public class TestComplexExplanations extends TestExplanations {
|
||||||
|
|
||||||
BooleanQuery q = new BooleanQuery();
|
BooleanQuery q = new BooleanQuery();
|
||||||
|
|
||||||
q.add(qp.parse("\"w1 w2\"~1"), Occur.MUST);
|
PhraseQuery phraseQuery = new PhraseQuery();
|
||||||
|
phraseQuery.setSlop(1);
|
||||||
|
phraseQuery.add(new Term(FIELD, "w1"));
|
||||||
|
phraseQuery.add(new Term(FIELD, "w2"));
|
||||||
|
q.add(phraseQuery, Occur.MUST);
|
||||||
q.add(snear(st("w2"),
|
q.add(snear(st("w2"),
|
||||||
sor("w5","zz"),
|
sor("w5","zz"),
|
||||||
4, true),
|
4, true),
|
||||||
|
@ -66,7 +71,7 @@ public class TestComplexExplanations extends TestExplanations {
|
||||||
q.add(snear(sf("w3",2), st("w2"), st("w3"), 5, true),
|
q.add(snear(sf("w3",2), st("w2"), st("w3"), 5, true),
|
||||||
Occur.SHOULD);
|
Occur.SHOULD);
|
||||||
|
|
||||||
Query t = new FilteredQuery(qp.parse("xx"),
|
Query t = new FilteredQuery(new TermQuery(new Term(FIELD, "xx")),
|
||||||
new ItemizedFilter(new int[] {1,3}));
|
new ItemizedFilter(new int[] {1,3}));
|
||||||
t.setBoost(1000);
|
t.setBoost(1000);
|
||||||
q.add(t, Occur.SHOULD);
|
q.add(t, Occur.SHOULD);
|
||||||
|
@ -79,14 +84,25 @@ public class TestComplexExplanations extends TestExplanations {
|
||||||
dm.add(snear(st("w2"),
|
dm.add(snear(st("w2"),
|
||||||
sor("w5","zz"),
|
sor("w5","zz"),
|
||||||
4, true));
|
4, true));
|
||||||
dm.add(qp.parse("QQ"));
|
dm.add(new TermQuery(new Term(FIELD, "QQ")));
|
||||||
dm.add(qp.parse("xx yy -zz"));
|
|
||||||
dm.add(qp.parse("-xx -w1"));
|
BooleanQuery xxYYZZ = new BooleanQuery();
|
||||||
|
xxYYZZ.add(new TermQuery(new Term(FIELD, "xx")), Occur.SHOULD);
|
||||||
|
xxYYZZ.add(new TermQuery(new Term(FIELD, "yy")), Occur.SHOULD);
|
||||||
|
xxYYZZ.add(new TermQuery(new Term(FIELD, "zz")), Occur.MUST_NOT);
|
||||||
|
|
||||||
|
dm.add(xxYYZZ);
|
||||||
|
|
||||||
|
BooleanQuery xxW1 = new BooleanQuery();
|
||||||
|
xxW1.add(new TermQuery(new Term(FIELD, "xx")), Occur.MUST_NOT);
|
||||||
|
xxW1.add(new TermQuery(new Term(FIELD, "w1")), Occur.MUST_NOT);
|
||||||
|
|
||||||
|
dm.add(xxW1);
|
||||||
|
|
||||||
DisjunctionMaxQuery dm2 = new DisjunctionMaxQuery(0.5f);
|
DisjunctionMaxQuery dm2 = new DisjunctionMaxQuery(0.5f);
|
||||||
dm2.add(qp.parse("w1"));
|
dm2.add(new TermQuery(new Term(FIELD, "w1")));
|
||||||
dm2.add(qp.parse("w2"));
|
dm2.add(new TermQuery(new Term(FIELD, "w2")));
|
||||||
dm2.add(qp.parse("w3"));
|
dm2.add(new TermQuery(new Term(FIELD, "w3")));
|
||||||
dm.add(dm2);
|
dm.add(dm2);
|
||||||
|
|
||||||
q.add(dm, Occur.SHOULD);
|
q.add(dm, Occur.SHOULD);
|
||||||
|
@ -106,7 +122,11 @@ public class TestComplexExplanations extends TestExplanations {
|
||||||
|
|
||||||
BooleanQuery q = new BooleanQuery();
|
BooleanQuery q = new BooleanQuery();
|
||||||
|
|
||||||
q.add(qp.parse("\"w1 w2\"~1"), Occur.MUST);
|
PhraseQuery phraseQuery = new PhraseQuery();
|
||||||
|
phraseQuery.setSlop(1);
|
||||||
|
phraseQuery.add(new Term(FIELD, "w1"));
|
||||||
|
phraseQuery.add(new Term(FIELD, "w2"));
|
||||||
|
q.add(phraseQuery, Occur.MUST);
|
||||||
q.add(snear(st("w2"),
|
q.add(snear(st("w2"),
|
||||||
sor("w5","zz"),
|
sor("w5","zz"),
|
||||||
4, true),
|
4, true),
|
||||||
|
@ -114,7 +134,7 @@ public class TestComplexExplanations extends TestExplanations {
|
||||||
q.add(snear(sf("w3",2), st("w2"), st("w3"), 5, true),
|
q.add(snear(sf("w3",2), st("w2"), st("w3"), 5, true),
|
||||||
Occur.SHOULD);
|
Occur.SHOULD);
|
||||||
|
|
||||||
Query t = new FilteredQuery(qp.parse("xx"),
|
Query t = new FilteredQuery(new TermQuery(new Term(FIELD, "xx")),
|
||||||
new ItemizedFilter(new int[] {1,3}));
|
new ItemizedFilter(new int[] {1,3}));
|
||||||
t.setBoost(1000);
|
t.setBoost(1000);
|
||||||
q.add(t, Occur.SHOULD);
|
q.add(t, Occur.SHOULD);
|
||||||
|
@ -127,14 +147,25 @@ public class TestComplexExplanations extends TestExplanations {
|
||||||
dm.add(snear(st("w2"),
|
dm.add(snear(st("w2"),
|
||||||
sor("w5","zz"),
|
sor("w5","zz"),
|
||||||
4, true));
|
4, true));
|
||||||
dm.add(qp.parse("QQ"));
|
dm.add(new TermQuery(new Term(FIELD, "QQ")));
|
||||||
dm.add(qp.parse("xx yy -zz"));
|
|
||||||
dm.add(qp.parse("-xx -w1"));
|
BooleanQuery xxYYZZ = new BooleanQuery();
|
||||||
|
xxYYZZ.add(new TermQuery(new Term(FIELD, "xx")), Occur.SHOULD);
|
||||||
|
xxYYZZ.add(new TermQuery(new Term(FIELD, "yy")), Occur.SHOULD);
|
||||||
|
xxYYZZ.add(new TermQuery(new Term(FIELD, "zz")), Occur.MUST_NOT);
|
||||||
|
|
||||||
|
dm.add(xxYYZZ);
|
||||||
|
|
||||||
|
BooleanQuery xxW1 = new BooleanQuery();
|
||||||
|
xxW1.add(new TermQuery(new Term(FIELD, "xx")), Occur.MUST_NOT);
|
||||||
|
xxW1.add(new TermQuery(new Term(FIELD, "w1")), Occur.MUST_NOT);
|
||||||
|
|
||||||
|
dm.add(xxW1);
|
||||||
|
|
||||||
DisjunctionMaxQuery dm2 = new DisjunctionMaxQuery(0.5f);
|
DisjunctionMaxQuery dm2 = new DisjunctionMaxQuery(0.5f);
|
||||||
dm2.add(qp.parse("w1"));
|
dm2.add(new TermQuery(new Term(FIELD, "w1")));
|
||||||
dm2.add(qp.parse("w2"));
|
dm2.add(new TermQuery(new Term(FIELD, "w2")));
|
||||||
dm2.add(qp.parse("w3"));
|
dm2.add(new TermQuery(new Term(FIELD, "w3")));
|
||||||
dm.add(dm2);
|
dm.add(dm2);
|
||||||
|
|
||||||
q.add(dm, Occur.SHOULD);
|
q.add(dm, Occur.SHOULD);
|
||||||
|
@ -161,7 +192,9 @@ public class TestComplexExplanations extends TestExplanations {
|
||||||
// with scores of 0 wrapped in other queries
|
// with scores of 0 wrapped in other queries
|
||||||
|
|
||||||
public void testT3() throws Exception {
|
public void testT3() throws Exception {
|
||||||
bqtest("w1^0.0", new int[] { 0,1,2,3 });
|
TermQuery query = new TermQuery(new Term(FIELD, "w1"));
|
||||||
|
query.setBoost(0);
|
||||||
|
bqtest(query, new int[] { 0,1,2,3 });
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testMA3() throws Exception {
|
public void testMA3() throws Exception {
|
||||||
|
@ -171,7 +204,9 @@ public class TestComplexExplanations extends TestExplanations {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testFQ5() throws Exception {
|
public void testFQ5() throws Exception {
|
||||||
bqtest(new FilteredQuery(qp.parse("xx^0"),
|
TermQuery query = new TermQuery(new Term(FIELD, "xx"));
|
||||||
|
query.setBoost(0);
|
||||||
|
bqtest(new FilteredQuery(query,
|
||||||
new ItemizedFilter(new int[] {1,3})),
|
new ItemizedFilter(new int[] {1,3})),
|
||||||
new int[] {3});
|
new int[] {3});
|
||||||
}
|
}
|
||||||
|
@ -184,8 +219,19 @@ public class TestComplexExplanations extends TestExplanations {
|
||||||
|
|
||||||
public void testDMQ10() throws Exception {
|
public void testDMQ10() throws Exception {
|
||||||
DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
|
DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
|
||||||
q.add(qp.parse("yy w5^100"));
|
|
||||||
q.add(qp.parse("xx^0"));
|
BooleanQuery query = new BooleanQuery();
|
||||||
|
query.add(new TermQuery(new Term(FIELD, "yy")), Occur.SHOULD);
|
||||||
|
TermQuery boostedQuery = new TermQuery(new Term(FIELD, "w5"));
|
||||||
|
boostedQuery.setBoost(100);
|
||||||
|
query.add(boostedQuery, Occur.SHOULD);
|
||||||
|
|
||||||
|
q.add(query);
|
||||||
|
|
||||||
|
TermQuery xxBoostedQuery = new TermQuery(new Term(FIELD, "xx"));
|
||||||
|
xxBoostedQuery.setBoost(0);
|
||||||
|
|
||||||
|
q.add(xxBoostedQuery);
|
||||||
q.setBoost(0.0f);
|
q.setBoost(0.0f);
|
||||||
bqtest(q, new int[] { 0,2,3 });
|
bqtest(q, new int[] { 0,2,3 });
|
||||||
}
|
}
|
||||||
|
@ -201,21 +247,51 @@ public class TestComplexExplanations extends TestExplanations {
|
||||||
|
|
||||||
public void testBQ12() throws Exception {
|
public void testBQ12() throws Exception {
|
||||||
// NOTE: using qtest not bqtest
|
// NOTE: using qtest not bqtest
|
||||||
qtest("w1 w2^0.0", new int[] { 0,1,2,3 });
|
BooleanQuery query = new BooleanQuery();
|
||||||
|
query.add(new TermQuery(new Term(FIELD, "w1")), Occur.SHOULD);
|
||||||
|
TermQuery boostedQuery = new TermQuery(new Term(FIELD, "w2"));
|
||||||
|
boostedQuery.setBoost(0);
|
||||||
|
query.add(boostedQuery, Occur.SHOULD);
|
||||||
|
|
||||||
|
qtest(query, new int[] { 0,1,2,3 });
|
||||||
}
|
}
|
||||||
public void testBQ13() throws Exception {
|
public void testBQ13() throws Exception {
|
||||||
// NOTE: using qtest not bqtest
|
// NOTE: using qtest not bqtest
|
||||||
qtest("w1 -w5^0.0", new int[] { 1,2,3 });
|
BooleanQuery query = new BooleanQuery();
|
||||||
|
query.add(new TermQuery(new Term(FIELD, "w1")), Occur.SHOULD);
|
||||||
|
TermQuery boostedQuery = new TermQuery(new Term(FIELD, "w5"));
|
||||||
|
boostedQuery.setBoost(0);
|
||||||
|
query.add(boostedQuery, Occur.MUST_NOT);
|
||||||
|
|
||||||
|
qtest(query, new int[] { 1,2,3 });
|
||||||
}
|
}
|
||||||
public void testBQ18() throws Exception {
|
public void testBQ18() throws Exception {
|
||||||
// NOTE: using qtest not bqtest
|
// NOTE: using qtest not bqtest
|
||||||
qtest("+w1^0.0 w2", new int[] { 0,1,2,3 });
|
BooleanQuery query = new BooleanQuery();
|
||||||
|
TermQuery boostedQuery = new TermQuery(new Term(FIELD, "w1"));
|
||||||
|
boostedQuery.setBoost(0);
|
||||||
|
query.add(boostedQuery, Occur.MUST);
|
||||||
|
query.add(new TermQuery(new Term(FIELD, "w2")), Occur.SHOULD);
|
||||||
|
|
||||||
|
qtest(query, new int[] { 0,1,2,3 });
|
||||||
}
|
}
|
||||||
public void testBQ21() throws Exception {
|
public void testBQ21() throws Exception {
|
||||||
bqtest("(+w1 w2)^0.0", new int[] { 0,1,2,3 });
|
BooleanQuery query = new BooleanQuery();
|
||||||
|
query.add(new TermQuery(new Term(FIELD, "w1")), Occur.MUST);
|
||||||
|
query.add(new TermQuery(new Term(FIELD, "w2")), Occur.SHOULD);
|
||||||
|
query.setBoost(0);
|
||||||
|
|
||||||
|
bqtest(query, new int[] { 0,1,2,3 });
|
||||||
}
|
}
|
||||||
public void testBQ22() throws Exception {
|
public void testBQ22() throws Exception {
|
||||||
bqtest("(+w1^0.0 w2)^0.0", new int[] { 0,1,2,3 });
|
BooleanQuery query = new BooleanQuery();
|
||||||
|
TermQuery boostedQuery = new TermQuery(new Term(FIELD, "w1"));
|
||||||
|
boostedQuery.setBoost(0);
|
||||||
|
query.add(boostedQuery, Occur.MUST);
|
||||||
|
query.add(new TermQuery(new Term(FIELD, "w2")), Occur.SHOULD);
|
||||||
|
query.setBoost(0);
|
||||||
|
|
||||||
|
bqtest(query, new int[] { 0,1,2,3 });
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testST3() throws Exception {
|
public void testST3() throws Exception {
|
||||||
|
|
|
@ -19,6 +19,7 @@ package org.apache.lucene.search;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import org.apache.lucene.index.Term;
|
||||||
import org.apache.lucene.util.LuceneTestCase;
|
import org.apache.lucene.util.LuceneTestCase;
|
||||||
|
|
||||||
import org.apache.lucene.analysis.MockAnalyzer;
|
import org.apache.lucene.analysis.MockAnalyzer;
|
||||||
|
@ -27,7 +28,6 @@ import org.apache.lucene.document.Document;
|
||||||
import org.apache.lucene.document.Field;
|
import org.apache.lucene.document.Field;
|
||||||
import org.apache.lucene.index.IndexReader;
|
import org.apache.lucene.index.IndexReader;
|
||||||
import org.apache.lucene.index.RandomIndexWriter;
|
import org.apache.lucene.index.RandomIndexWriter;
|
||||||
import org.apache.lucene.queryParser.QueryParser;
|
|
||||||
import org.apache.lucene.search.Query;
|
import org.apache.lucene.search.Query;
|
||||||
import org.apache.lucene.search.Sort;
|
import org.apache.lucene.search.Sort;
|
||||||
import org.apache.lucene.search.SortField;
|
import org.apache.lucene.search.SortField;
|
||||||
|
@ -80,9 +80,7 @@ public class TestDateSort extends LuceneTestCase {
|
||||||
IndexSearcher searcher = newSearcher(reader);
|
IndexSearcher searcher = newSearcher(reader);
|
||||||
|
|
||||||
Sort sort = new Sort(new SortField(DATE_TIME_FIELD, SortField.Type.STRING, true));
|
Sort sort = new Sort(new SortField(DATE_TIME_FIELD, SortField.Type.STRING, true));
|
||||||
|
Query query = new TermQuery(new Term(TEXT_FIELD, "document"));
|
||||||
QueryParser queryParser = new QueryParser(TEST_VERSION_CURRENT, TEXT_FIELD, new MockAnalyzer(random));
|
|
||||||
Query query = queryParser.parse("Document");
|
|
||||||
|
|
||||||
// Execute the search and process the search results.
|
// Execute the search and process the search results.
|
||||||
String[] actualOrder = new String[5];
|
String[] actualOrder = new String[5];
|
||||||
|
|
|
@ -17,8 +17,6 @@ package org.apache.lucene.search;
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import org.apache.lucene.queryParser.QueryParser;
|
|
||||||
import org.apache.lucene.queryParser.ParseException;
|
|
||||||
import org.apache.lucene.analysis.MockAnalyzer;
|
import org.apache.lucene.analysis.MockAnalyzer;
|
||||||
import org.apache.lucene.document.Document;
|
import org.apache.lucene.document.Document;
|
||||||
import org.apache.lucene.document.Field;
|
import org.apache.lucene.document.Field;
|
||||||
|
@ -58,8 +56,6 @@ public class TestExplanations extends LuceneTestCase {
|
||||||
public static final String FIELD = "field";
|
public static final String FIELD = "field";
|
||||||
// same contents, but no field boost
|
// same contents, but no field boost
|
||||||
public static final String ALTFIELD = "alt";
|
public static final String ALTFIELD = "alt";
|
||||||
public static final QueryParser qp =
|
|
||||||
new QueryParser(TEST_VERSION_CURRENT, FIELD, new MockAnalyzer(random));
|
|
||||||
|
|
||||||
@AfterClass
|
@AfterClass
|
||||||
public static void afterClassTestExplanations() throws Exception {
|
public static void afterClassTestExplanations() throws Exception {
|
||||||
|
@ -96,15 +92,6 @@ public class TestExplanations extends LuceneTestCase {
|
||||||
"w1 w3 xx w2 yy w3 zz"
|
"w1 w3 xx w2 yy w3 zz"
|
||||||
};
|
};
|
||||||
|
|
||||||
public Query makeQuery(String queryText) throws ParseException {
|
|
||||||
return qp.parse(queryText);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** check the expDocNrs first, then check the query (and the explanations) */
|
|
||||||
public void qtest(String queryText, int[] expDocNrs) throws Exception {
|
|
||||||
qtest(makeQuery(queryText), expDocNrs);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** check the expDocNrs first, then check the query (and the explanations) */
|
/** check the expDocNrs first, then check the query (and the explanations) */
|
||||||
public void qtest(Query q, int[] expDocNrs) throws Exception {
|
public void qtest(Query q, int[] expDocNrs) throws Exception {
|
||||||
CheckHits.checkHitCollector(random, q, FIELD, searcher, expDocNrs);
|
CheckHits.checkHitCollector(random, q, FIELD, searcher, expDocNrs);
|
||||||
|
@ -120,15 +107,6 @@ public class TestExplanations extends LuceneTestCase {
|
||||||
qtest(reqB(q), expDocNrs);
|
qtest(reqB(q), expDocNrs);
|
||||||
qtest(optB(q), expDocNrs);
|
qtest(optB(q), expDocNrs);
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* Tests a query using qtest after wrapping it with both optB and reqB
|
|
||||||
* @see #qtest
|
|
||||||
* @see #reqB
|
|
||||||
* @see #optB
|
|
||||||
*/
|
|
||||||
public void bqtest(String queryText, int[] expDocNrs) throws Exception {
|
|
||||||
bqtest(makeQuery(queryText), expDocNrs);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convenience subclass of FieldCacheTermsFilter
|
* Convenience subclass of FieldCacheTermsFilter
|
||||||
|
@ -213,13 +191,6 @@ public class TestExplanations extends LuceneTestCase {
|
||||||
return new SpanFirstQuery(st(s), b);
|
return new SpanFirstQuery(st(s), b);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* MACRO: Wraps a Query in a BooleanQuery so that it is optional, along
|
|
||||||
* with a second prohibited clause which will never match anything
|
|
||||||
*/
|
|
||||||
public Query optB(String q) throws Exception {
|
|
||||||
return optB(makeQuery(q));
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* MACRO: Wraps a Query in a BooleanQuery so that it is optional, along
|
* MACRO: Wraps a Query in a BooleanQuery so that it is optional, along
|
||||||
* with a second prohibited clause which will never match anything
|
* with a second prohibited clause which will never match anything
|
||||||
|
@ -231,13 +202,6 @@ public class TestExplanations extends LuceneTestCase {
|
||||||
return bq;
|
return bq;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* MACRO: Wraps a Query in a BooleanQuery so that it is required, along
|
|
||||||
* with a second optional clause which will match everything
|
|
||||||
*/
|
|
||||||
public Query reqB(String q) throws Exception {
|
|
||||||
return reqB(makeQuery(q));
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* MACRO: Wraps a Query in a BooleanQuery so that it is required, along
|
* MACRO: Wraps a Query in a BooleanQuery so that it is required, along
|
||||||
* with a second optional clause which will match everything
|
* with a second optional clause which will match everything
|
||||||
|
|
|
@ -28,7 +28,6 @@ import org.apache.lucene.index.IndexReader;
|
||||||
import org.apache.lucene.index.MultiReader;
|
import org.apache.lucene.index.MultiReader;
|
||||||
import org.apache.lucene.index.RandomIndexWriter;
|
import org.apache.lucene.index.RandomIndexWriter;
|
||||||
import org.apache.lucene.index.Term;
|
import org.apache.lucene.index.Term;
|
||||||
import org.apache.lucene.queryParser.QueryParser;
|
|
||||||
import org.apache.lucene.store.Directory;
|
import org.apache.lucene.store.Directory;
|
||||||
import org.apache.lucene.util.LuceneTestCase;
|
import org.apache.lucene.util.LuceneTestCase;
|
||||||
|
|
||||||
|
@ -410,7 +409,7 @@ public class TestFuzzyQuery extends LuceneTestCase {
|
||||||
IndexReader r = w.getReader();
|
IndexReader r = w.getReader();
|
||||||
w.close();
|
w.close();
|
||||||
|
|
||||||
Query q = new QueryParser(TEST_VERSION_CURRENT, "field", analyzer).parse( "giga~0.9" );
|
Query q = new FuzzyQuery(new Term("field", "giga"), 0.9f);
|
||||||
|
|
||||||
// 3. search
|
// 3. search
|
||||||
IndexSearcher searcher = newSearcher(r);
|
IndexSearcher searcher = newSearcher(r);
|
||||||
|
@ -422,12 +421,6 @@ public class TestFuzzyQuery extends LuceneTestCase {
|
||||||
index.close();
|
index.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testDistanceAsEditsParsing() throws Exception {
|
|
||||||
QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, "field", new MockAnalyzer(random));
|
|
||||||
FuzzyQuery q = (FuzzyQuery) qp.parse("foobar~2");
|
|
||||||
assertEquals(2f, q.getMinSimilarity(), 0.0001f);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testDistanceAsEditsSearching() throws Exception {
|
public void testDistanceAsEditsSearching() throws Exception {
|
||||||
Directory index = newDirectory();
|
Directory index = newDirectory();
|
||||||
RandomIndexWriter w = new RandomIndexWriter(random, index);
|
RandomIndexWriter w = new RandomIndexWriter(random, index);
|
||||||
|
@ -437,19 +430,18 @@ public class TestFuzzyQuery extends LuceneTestCase {
|
||||||
IndexReader reader = w.getReader();
|
IndexReader reader = w.getReader();
|
||||||
IndexSearcher searcher = newSearcher(reader);
|
IndexSearcher searcher = newSearcher(reader);
|
||||||
w.close();
|
w.close();
|
||||||
QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, "field", new MockAnalyzer(random));
|
|
||||||
|
|
||||||
FuzzyQuery q = (FuzzyQuery) qp.parse("fouba~2");
|
FuzzyQuery q = new FuzzyQuery(new Term("field", "fouba"), 2);
|
||||||
ScoreDoc[] hits = searcher.search(q, 10).scoreDocs;
|
ScoreDoc[] hits = searcher.search(q, 10).scoreDocs;
|
||||||
assertEquals(1, hits.length);
|
assertEquals(1, hits.length);
|
||||||
assertEquals("foobar", searcher.doc(hits[0].doc).get("field"));
|
assertEquals("foobar", searcher.doc(hits[0].doc).get("field"));
|
||||||
|
|
||||||
q = (FuzzyQuery) qp.parse("foubara~2");
|
q = new FuzzyQuery(new Term("field", "foubara"), 2);
|
||||||
hits = searcher.search(q, 10).scoreDocs;
|
hits = searcher.search(q, 10).scoreDocs;
|
||||||
assertEquals(1, hits.length);
|
assertEquals(1, hits.length);
|
||||||
assertEquals("foobar", searcher.doc(hits[0].doc).get("field"));
|
assertEquals("foobar", searcher.doc(hits[0].doc).get("field"));
|
||||||
|
|
||||||
q = (FuzzyQuery) qp.parse("t~3");
|
q = new FuzzyQuery(new Term("field", "t"), 3);
|
||||||
hits = searcher.search(q, 10).scoreDocs;
|
hits = searcher.search(q, 10).scoreDocs;
|
||||||
assertEquals(1, hits.length);
|
assertEquals(1, hits.length);
|
||||||
assertEquals("test", searcher.doc(hits[0].doc).get("field"));
|
assertEquals("test", searcher.doc(hits[0].doc).get("field"));
|
||||||
|
|
|
@ -25,7 +25,6 @@ import org.apache.lucene.document.Field;
|
||||||
import org.apache.lucene.index.IndexWriter;
|
import org.apache.lucene.index.IndexWriter;
|
||||||
import org.apache.lucene.index.Term;
|
import org.apache.lucene.index.Term;
|
||||||
import org.apache.lucene.index.IndexReader;
|
import org.apache.lucene.index.IndexReader;
|
||||||
import org.apache.lucene.queryParser.QueryParser;
|
|
||||||
import org.apache.lucene.store.Directory;
|
import org.apache.lucene.store.Directory;
|
||||||
|
|
||||||
import org.apache.lucene.util.LuceneTestCase;
|
import org.apache.lucene.util.LuceneTestCase;
|
||||||
|
@ -97,18 +96,6 @@ public class TestMatchAllDocsQuery extends LuceneTestCase {
|
||||||
hits = is.search(new MatchAllDocsQuery(), null, 1000).scoreDocs;
|
hits = is.search(new MatchAllDocsQuery(), null, 1000).scoreDocs;
|
||||||
assertEquals(2, hits.length);
|
assertEquals(2, hits.length);
|
||||||
|
|
||||||
// test parsable toString()
|
|
||||||
QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, "key", analyzer);
|
|
||||||
hits = is.search(qp.parse(new MatchAllDocsQuery().toString()), null, 1000).scoreDocs;
|
|
||||||
assertEquals(2, hits.length);
|
|
||||||
|
|
||||||
// test parsable toString() with non default boost
|
|
||||||
Query maq = new MatchAllDocsQuery();
|
|
||||||
maq.setBoost(2.3f);
|
|
||||||
Query pq = qp.parse(maq.toString());
|
|
||||||
hits = is.search(pq, null, 1000).scoreDocs;
|
|
||||||
assertEquals(2, hits.length);
|
|
||||||
|
|
||||||
is.close();
|
is.close();
|
||||||
ir.close();
|
ir.close();
|
||||||
dir.close();
|
dir.close();
|
||||||
|
|
|
@ -24,7 +24,6 @@ import org.apache.lucene.index.TermsEnum;
|
||||||
import org.apache.lucene.index.IndexReader;
|
import org.apache.lucene.index.IndexReader;
|
||||||
import org.apache.lucene.index.MultiFields;
|
import org.apache.lucene.index.MultiFields;
|
||||||
import org.apache.lucene.queryParser.ParseException;
|
import org.apache.lucene.queryParser.ParseException;
|
||||||
import org.apache.lucene.queryParser.QueryParser;
|
|
||||||
import org.apache.lucene.search.Explanation.IDFExplanation;
|
import org.apache.lucene.search.Explanation.IDFExplanation;
|
||||||
import org.apache.lucene.store.Directory;
|
import org.apache.lucene.store.Directory;
|
||||||
import org.apache.lucene.util.BytesRef;
|
import org.apache.lucene.util.BytesRef;
|
||||||
|
@ -479,13 +478,17 @@ public class TestMultiPhraseQuery extends LuceneTestCase {
|
||||||
* in each position - one of each position is sufficient (OR logic)
|
* in each position - one of each position is sufficient (OR logic)
|
||||||
*/
|
*/
|
||||||
public void testZeroPosIncrSloppyParsedAnd() throws IOException, ParseException {
|
public void testZeroPosIncrSloppyParsedAnd() throws IOException, ParseException {
|
||||||
QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, "field", new CannedAnalyzer(INCR_0_QUERY_TOKENS_AND));
|
// QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, "field", new CannedAnalyzer(INCR_0_QUERY_TOKENS_AND)); // nocommit - move this to its own test
|
||||||
final Query q = qp.parse("\"this text is acually ignored\"");
|
// final Query q = qp.parse("\"this text is acually ignored\"");
|
||||||
assertTrue("wrong query type!", q instanceof MultiPhraseQuery);
|
// assertTrue("wrong query type!", q instanceof MultiPhraseQuery);
|
||||||
|
MultiPhraseQuery q = new MultiPhraseQuery();
|
||||||
|
q.add(new Term[]{ new Term("field", "a"), new Term("field", "1") }, -1);
|
||||||
|
q.add(new Term[]{ new Term("field", "b"), new Term("field", "1") }, 0);
|
||||||
|
q.add(new Term[]{ new Term("field", "c") }, 1);
|
||||||
doTestZeroPosIncrSloppy(q, 0);
|
doTestZeroPosIncrSloppy(q, 0);
|
||||||
((MultiPhraseQuery) q).setSlop(1);
|
q.setSlop(1);
|
||||||
doTestZeroPosIncrSloppy(q, 0);
|
doTestZeroPosIncrSloppy(q, 0);
|
||||||
((MultiPhraseQuery) q).setSlop(2);
|
q.setSlop(2);
|
||||||
doTestZeroPosIncrSloppy(q, 1);
|
doTestZeroPosIncrSloppy(q, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,11 +17,11 @@ package org.apache.lucene.search;
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import org.apache.lucene.index.Term;
|
||||||
import org.apache.lucene.util.LuceneTestCase;
|
import org.apache.lucene.util.LuceneTestCase;
|
||||||
|
|
||||||
import org.apache.lucene.index.IndexReader;
|
import org.apache.lucene.index.IndexReader;
|
||||||
import org.apache.lucene.index.RandomIndexWriter;
|
import org.apache.lucene.index.RandomIndexWriter;
|
||||||
import org.apache.lucene.queryParser.QueryParser;
|
|
||||||
import org.apache.lucene.store.Directory;
|
import org.apache.lucene.store.Directory;
|
||||||
import org.apache.lucene.analysis.MockAnalyzer;
|
import org.apache.lucene.analysis.MockAnalyzer;
|
||||||
import org.apache.lucene.document.Document;
|
import org.apache.lucene.document.Document;
|
||||||
|
@ -44,9 +44,11 @@ public class TestNot extends LuceneTestCase {
|
||||||
IndexReader reader = writer.getReader();
|
IndexReader reader = writer.getReader();
|
||||||
|
|
||||||
IndexSearcher searcher = newSearcher(reader);
|
IndexSearcher searcher = newSearcher(reader);
|
||||||
QueryParser parser = new QueryParser(TEST_VERSION_CURRENT, "field", new MockAnalyzer(random));
|
|
||||||
Query query = parser.parse("a NOT b");
|
BooleanQuery query = new BooleanQuery();
|
||||||
//System.out.println(query);
|
query.add(new TermQuery(new Term("field", "a")), BooleanClause.Occur.SHOULD);
|
||||||
|
query.add(new TermQuery(new Term("field", "b")), BooleanClause.Occur.MUST_NOT);
|
||||||
|
|
||||||
ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
|
ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
|
||||||
assertEquals(0, hits.length);
|
assertEquals(0, hits.length);
|
||||||
writer.close();
|
writer.close();
|
||||||
|
|
|
@ -23,7 +23,6 @@ import org.apache.lucene.analysis.tokenattributes.*;
|
||||||
import org.apache.lucene.document.*;
|
import org.apache.lucene.document.*;
|
||||||
import org.apache.lucene.index.*;
|
import org.apache.lucene.index.*;
|
||||||
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
|
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
|
||||||
import org.apache.lucene.queryParser.QueryParser;
|
|
||||||
import org.apache.lucene.store.*;
|
import org.apache.lucene.store.*;
|
||||||
import org.apache.lucene.util.Version;
|
import org.apache.lucene.util.Version;
|
||||||
import org.apache.lucene.util._TestUtil;
|
import org.apache.lucene.util._TestUtil;
|
||||||
|
@ -382,10 +381,10 @@ public class TestPhraseQuery extends LuceneTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testToString() throws Exception {
|
public void testToString() throws Exception {
|
||||||
Analyzer analyzer = new MockAnalyzer(random, MockTokenizer.SIMPLE, true, MockTokenFilter.ENGLISH_STOPSET, true);
|
PhraseQuery q = new PhraseQuery(); // Query "this hi this is a test is"
|
||||||
QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, "field", analyzer);
|
q.add(new Term("field", "hi"), 1);
|
||||||
qp.setEnablePositionIncrements(true);
|
q.add(new Term("field", "test"), 5);
|
||||||
PhraseQuery q = (PhraseQuery)qp.parse("\"this hi this is a test is\"");
|
|
||||||
assertEquals("field:\"? hi ? ? ? test\"", q.toString());
|
assertEquals("field:\"? hi ? ? ? test\"", q.toString());
|
||||||
q.add(new Term("field", "hello"), 1);
|
q.add(new Term("field", "hello"), 1);
|
||||||
assertEquals("field:\"? hi|hello ? ? ? test\"", q.toString());
|
assertEquals("field:\"? hi|hello ? ? ? test\"", q.toString());
|
||||||
|
|
|
@ -37,7 +37,6 @@ import org.apache.lucene.index.IndexReader;
|
||||||
import org.apache.lucene.index.RandomIndexWriter;
|
import org.apache.lucene.index.RandomIndexWriter;
|
||||||
import org.apache.lucene.index.SlowMultiReaderWrapper;
|
import org.apache.lucene.index.SlowMultiReaderWrapper;
|
||||||
import org.apache.lucene.index.Term;
|
import org.apache.lucene.index.Term;
|
||||||
import org.apache.lucene.queryParser.QueryParser;
|
|
||||||
import org.apache.lucene.store.Directory;
|
import org.apache.lucene.store.Directory;
|
||||||
import org.apache.lucene.search.payloads.PayloadSpanUtil;
|
import org.apache.lucene.search.payloads.PayloadSpanUtil;
|
||||||
import org.apache.lucene.search.spans.MultiSpansWrapper;
|
import org.apache.lucene.search.spans.MultiSpansWrapper;
|
||||||
|
@ -194,38 +193,6 @@ public class TestPositionIncrement extends LuceneTestCase {
|
||||||
hits = searcher.search(q, null, 1000).scoreDocs;
|
hits = searcher.search(q, null, 1000).scoreDocs;
|
||||||
assertEquals(0, hits.length);
|
assertEquals(0, hits.length);
|
||||||
|
|
||||||
// should not find "1 2" because there is a gap of 1 in the index
|
|
||||||
QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, "field",
|
|
||||||
new MockAnalyzer(random, MockTokenizer.WHITESPACE, false, stopStopList, false));
|
|
||||||
q = (PhraseQuery) qp.parse("\"1 2\"");
|
|
||||||
hits = searcher.search(q, null, 1000).scoreDocs;
|
|
||||||
assertEquals(0, hits.length);
|
|
||||||
|
|
||||||
// omitted stop word cannot help because stop filter swallows the increments.
|
|
||||||
q = (PhraseQuery) qp.parse("\"1 stop 2\"");
|
|
||||||
hits = searcher.search(q, null, 1000).scoreDocs;
|
|
||||||
assertEquals(0, hits.length);
|
|
||||||
|
|
||||||
// query parser alone won't help, because stop filter swallows the increments.
|
|
||||||
qp.setEnablePositionIncrements(true);
|
|
||||||
q = (PhraseQuery) qp.parse("\"1 stop 2\"");
|
|
||||||
hits = searcher.search(q, null, 1000).scoreDocs;
|
|
||||||
assertEquals(0, hits.length);
|
|
||||||
|
|
||||||
// stop filter alone won't help, because query parser swallows the increments.
|
|
||||||
qp.setEnablePositionIncrements(false);
|
|
||||||
q = (PhraseQuery) qp.parse("\"1 stop 2\"");
|
|
||||||
hits = searcher.search(q, null, 1000).scoreDocs;
|
|
||||||
assertEquals(0, hits.length);
|
|
||||||
|
|
||||||
// when both qp qnd stopFilter propagate increments, we should find the doc.
|
|
||||||
qp = new QueryParser(TEST_VERSION_CURRENT, "field",
|
|
||||||
new MockAnalyzer(random, MockTokenizer.WHITESPACE, false, stopStopList, true));
|
|
||||||
qp.setEnablePositionIncrements(true);
|
|
||||||
q = (PhraseQuery) qp.parse("\"1 stop 2\"");
|
|
||||||
hits = searcher.search(q, null, 1000).scoreDocs;
|
|
||||||
assertEquals(1, hits.length);
|
|
||||||
|
|
||||||
searcher.close();
|
searcher.close();
|
||||||
reader.close();
|
reader.close();
|
||||||
store.close();
|
store.close();
|
||||||
|
|
|
@ -17,6 +17,8 @@ package org.apache.lucene.search;
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import org.apache.lucene.index.Term;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TestExplanations subclass focusing on basic query types
|
* TestExplanations subclass focusing on basic query types
|
||||||
*/
|
*/
|
||||||
|
@ -30,10 +32,12 @@ public class TestSimpleExplanations extends TestExplanations {
|
||||||
/* simple term tests */
|
/* simple term tests */
|
||||||
|
|
||||||
public void testT1() throws Exception {
|
public void testT1() throws Exception {
|
||||||
qtest("w1", new int[] { 0,1,2,3 });
|
qtest(new TermQuery(new Term(FIELD, "w1")), new int[] { 0,1,2,3 });
|
||||||
}
|
}
|
||||||
public void testT2() throws Exception {
|
public void testT2() throws Exception {
|
||||||
qtest("w1^1000", new int[] { 0,1,2,3 });
|
TermQuery termQuery = new TermQuery(new Term(FIELD, "w1"));
|
||||||
|
termQuery.setBoost(100);
|
||||||
|
qtest(termQuery, new int[] { 0,1,2,3 });
|
||||||
}
|
}
|
||||||
|
|
||||||
/* MatchAllDocs */
|
/* MatchAllDocs */
|
||||||
|
@ -50,51 +54,78 @@ public class TestSimpleExplanations extends TestExplanations {
|
||||||
/* some simple phrase tests */
|
/* some simple phrase tests */
|
||||||
|
|
||||||
public void testP1() throws Exception {
|
public void testP1() throws Exception {
|
||||||
qtest("\"w1 w2\"", new int[] { 0 });
|
PhraseQuery phraseQuery = new PhraseQuery();
|
||||||
|
phraseQuery.add(new Term(FIELD, "w1"));
|
||||||
|
phraseQuery.add(new Term(FIELD, "w2"));
|
||||||
|
qtest(phraseQuery, new int[] { 0 });
|
||||||
}
|
}
|
||||||
public void testP2() throws Exception {
|
public void testP2() throws Exception {
|
||||||
qtest("\"w1 w3\"", new int[] { 1,3 });
|
PhraseQuery phraseQuery = new PhraseQuery();
|
||||||
|
phraseQuery.add(new Term(FIELD, "w1"));
|
||||||
|
phraseQuery.add(new Term(FIELD, "w3"));
|
||||||
|
qtest(phraseQuery, new int[] { 1,3 });
|
||||||
}
|
}
|
||||||
public void testP3() throws Exception {
|
public void testP3() throws Exception {
|
||||||
qtest("\"w1 w2\"~1", new int[] { 0,1,2 });
|
PhraseQuery phraseQuery = new PhraseQuery();
|
||||||
|
phraseQuery.setSlop(1);
|
||||||
|
phraseQuery.add(new Term(FIELD, "w1"));
|
||||||
|
phraseQuery.add(new Term(FIELD, "w2"));
|
||||||
|
qtest(phraseQuery, new int[] { 0,1,2 });
|
||||||
}
|
}
|
||||||
public void testP4() throws Exception {
|
public void testP4() throws Exception {
|
||||||
qtest("\"w2 w3\"~1", new int[] { 0,1,2,3 });
|
PhraseQuery phraseQuery = new PhraseQuery();
|
||||||
|
phraseQuery.setSlop(1);
|
||||||
|
phraseQuery.add(new Term(FIELD, "w2"));
|
||||||
|
phraseQuery.add(new Term(FIELD, "w3"));
|
||||||
|
qtest(phraseQuery, new int[] { 0,1,2,3 });
|
||||||
}
|
}
|
||||||
public void testP5() throws Exception {
|
public void testP5() throws Exception {
|
||||||
qtest("\"w3 w2\"~1", new int[] { 1,3 });
|
PhraseQuery phraseQuery = new PhraseQuery();
|
||||||
|
phraseQuery.setSlop(1);
|
||||||
|
phraseQuery.add(new Term(FIELD, "w3"));
|
||||||
|
phraseQuery.add(new Term(FIELD, "w2"));
|
||||||
|
qtest(phraseQuery, new int[] { 1,3 });
|
||||||
}
|
}
|
||||||
public void testP6() throws Exception {
|
public void testP6() throws Exception {
|
||||||
qtest("\"w3 w2\"~2", new int[] { 0,1,3 });
|
PhraseQuery phraseQuery = new PhraseQuery();
|
||||||
|
phraseQuery.setSlop(2);
|
||||||
|
phraseQuery.add(new Term(FIELD, "w3"));
|
||||||
|
phraseQuery.add(new Term(FIELD, "w2"));
|
||||||
|
qtest(phraseQuery, new int[] { 0,1,3 });
|
||||||
}
|
}
|
||||||
public void testP7() throws Exception {
|
public void testP7() throws Exception {
|
||||||
qtest("\"w3 w2\"~3", new int[] { 0,1,2,3 });
|
PhraseQuery phraseQuery = new PhraseQuery();
|
||||||
|
phraseQuery.setSlop(3);
|
||||||
|
phraseQuery.add(new Term(FIELD, "w3"));
|
||||||
|
phraseQuery.add(new Term(FIELD, "w2"));
|
||||||
|
qtest(phraseQuery, new int[] { 0,1,2,3 });
|
||||||
}
|
}
|
||||||
|
|
||||||
/* some simple filtered query tests */
|
/* some simple filtered query tests */
|
||||||
|
|
||||||
public void testFQ1() throws Exception {
|
public void testFQ1() throws Exception {
|
||||||
qtest(new FilteredQuery(qp.parse("w1"),
|
qtest(new FilteredQuery(new TermQuery(new Term(FIELD, "w1")),
|
||||||
new ItemizedFilter(new int[] {0,1,2,3})),
|
new ItemizedFilter(new int[] {0,1,2,3})),
|
||||||
new int[] {0,1,2,3});
|
new int[] {0,1,2,3});
|
||||||
}
|
}
|
||||||
public void testFQ2() throws Exception {
|
public void testFQ2() throws Exception {
|
||||||
qtest(new FilteredQuery(qp.parse("w1"),
|
qtest(new FilteredQuery(new TermQuery(new Term(FIELD, "w1")),
|
||||||
new ItemizedFilter(new int[] {0,2,3})),
|
new ItemizedFilter(new int[] {0,2,3})),
|
||||||
new int[] {0,2,3});
|
new int[] {0,2,3});
|
||||||
}
|
}
|
||||||
public void testFQ3() throws Exception {
|
public void testFQ3() throws Exception {
|
||||||
qtest(new FilteredQuery(qp.parse("xx"),
|
qtest(new FilteredQuery(new TermQuery(new Term(FIELD, "xx")),
|
||||||
new ItemizedFilter(new int[] {1,3})),
|
new ItemizedFilter(new int[] {1,3})),
|
||||||
new int[] {3});
|
new int[] {3});
|
||||||
}
|
}
|
||||||
public void testFQ4() throws Exception {
|
public void testFQ4() throws Exception {
|
||||||
qtest(new FilteredQuery(qp.parse("xx^1000"),
|
TermQuery termQuery = new TermQuery(new Term(FIELD, "xx"));
|
||||||
new ItemizedFilter(new int[] {1,3})),
|
termQuery.setBoost(1000);
|
||||||
|
qtest(new FilteredQuery(termQuery, new ItemizedFilter(new int[] {1,3})),
|
||||||
new int[] {3});
|
new int[] {3});
|
||||||
}
|
}
|
||||||
public void testFQ6() throws Exception {
|
public void testFQ6() throws Exception {
|
||||||
Query q = new FilteredQuery(qp.parse("xx"),
|
Query q = new FilteredQuery(new TermQuery(new Term(FIELD, "xx")),
|
||||||
new ItemizedFilter(new int[] {1,3}));
|
new ItemizedFilter(new int[] {1,3}));
|
||||||
q.setBoost(1000);
|
q.setBoost(1000);
|
||||||
qtest(q, new int[] {3});
|
qtest(q, new int[] {3});
|
||||||
|
@ -120,56 +151,93 @@ public class TestSimpleExplanations extends TestExplanations {
|
||||||
|
|
||||||
public void testDMQ1() throws Exception {
|
public void testDMQ1() throws Exception {
|
||||||
DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.0f);
|
DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.0f);
|
||||||
q.add(qp.parse("w1"));
|
q.add(new TermQuery(new Term(FIELD, "w1")));
|
||||||
q.add(qp.parse("w5"));
|
q.add(new TermQuery(new Term(FIELD, "w5")));
|
||||||
qtest(q, new int[] { 0,1,2,3 });
|
qtest(q, new int[] { 0,1,2,3 });
|
||||||
}
|
}
|
||||||
public void testDMQ2() throws Exception {
|
public void testDMQ2() throws Exception {
|
||||||
DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
|
DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
|
||||||
q.add(qp.parse("w1"));
|
q.add(new TermQuery(new Term(FIELD, "w1")));
|
||||||
q.add(qp.parse("w5"));
|
q.add(new TermQuery(new Term(FIELD, "w5")));
|
||||||
qtest(q, new int[] { 0,1,2,3 });
|
qtest(q, new int[] { 0,1,2,3 });
|
||||||
}
|
}
|
||||||
public void testDMQ3() throws Exception {
|
public void testDMQ3() throws Exception {
|
||||||
DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
|
DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
|
||||||
q.add(qp.parse("QQ"));
|
q.add(new TermQuery(new Term(FIELD, "QQ")));
|
||||||
q.add(qp.parse("w5"));
|
q.add(new TermQuery(new Term(FIELD, "w5")));
|
||||||
qtest(q, new int[] { 0 });
|
qtest(q, new int[] { 0 });
|
||||||
}
|
}
|
||||||
public void testDMQ4() throws Exception {
|
public void testDMQ4() throws Exception {
|
||||||
DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
|
DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
|
||||||
q.add(qp.parse("QQ"));
|
q.add(new TermQuery(new Term(FIELD, "QQ")));
|
||||||
q.add(qp.parse("xx"));
|
q.add(new TermQuery(new Term(FIELD, "xx")));
|
||||||
qtest(q, new int[] { 2,3 });
|
qtest(q, new int[] { 2,3 });
|
||||||
}
|
}
|
||||||
public void testDMQ5() throws Exception {
|
public void testDMQ5() throws Exception {
|
||||||
DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
|
DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
|
||||||
q.add(qp.parse("yy -QQ"));
|
|
||||||
q.add(qp.parse("xx"));
|
BooleanQuery booleanQuery = new BooleanQuery();
|
||||||
|
booleanQuery.add(new TermQuery(new Term(FIELD, "yy")), BooleanClause.Occur.SHOULD);
|
||||||
|
booleanQuery.add(new TermQuery(new Term(FIELD, "QQ")), BooleanClause.Occur.MUST_NOT);
|
||||||
|
|
||||||
|
q.add(booleanQuery);
|
||||||
|
q.add(new TermQuery(new Term(FIELD, "xx")));
|
||||||
qtest(q, new int[] { 2,3 });
|
qtest(q, new int[] { 2,3 });
|
||||||
}
|
}
|
||||||
public void testDMQ6() throws Exception {
|
public void testDMQ6() throws Exception {
|
||||||
DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
|
DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
|
||||||
q.add(qp.parse("-yy w3"));
|
|
||||||
q.add(qp.parse("xx"));
|
BooleanQuery booleanQuery = new BooleanQuery();
|
||||||
|
booleanQuery.add(new TermQuery(new Term(FIELD, "yy")), BooleanClause.Occur.MUST_NOT);
|
||||||
|
booleanQuery.add(new TermQuery(new Term(FIELD, "w3")), BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
q.add(booleanQuery);
|
||||||
|
q.add(new TermQuery(new Term(FIELD, "xx")));
|
||||||
qtest(q, new int[] { 0,1,2,3 });
|
qtest(q, new int[] { 0,1,2,3 });
|
||||||
}
|
}
|
||||||
public void testDMQ7() throws Exception {
|
public void testDMQ7() throws Exception {
|
||||||
DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
|
DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
|
||||||
q.add(qp.parse("-yy w3"));
|
|
||||||
q.add(qp.parse("w2"));
|
BooleanQuery booleanQuery = new BooleanQuery();
|
||||||
|
booleanQuery.add(new TermQuery(new Term(FIELD, "yy")), BooleanClause.Occur.MUST_NOT);
|
||||||
|
booleanQuery.add(new TermQuery(new Term(FIELD, "w3")), BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
q.add(booleanQuery);
|
||||||
|
q.add(new TermQuery(new Term(FIELD, "w2")));
|
||||||
qtest(q, new int[] { 0,1,2,3 });
|
qtest(q, new int[] { 0,1,2,3 });
|
||||||
}
|
}
|
||||||
public void testDMQ8() throws Exception {
|
public void testDMQ8() throws Exception {
|
||||||
DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
|
DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
|
||||||
q.add(qp.parse("yy w5^100"));
|
|
||||||
q.add(qp.parse("xx^100000"));
|
BooleanQuery booleanQuery = new BooleanQuery();
|
||||||
|
booleanQuery.add(new TermQuery(new Term(FIELD, "yy")), BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
TermQuery boostedQuery = new TermQuery(new Term(FIELD, "w5"));
|
||||||
|
boostedQuery.setBoost(100);
|
||||||
|
booleanQuery.add(boostedQuery, BooleanClause.Occur.SHOULD);
|
||||||
|
q.add(booleanQuery);
|
||||||
|
|
||||||
|
TermQuery xxBoostedQuery = new TermQuery(new Term(FIELD, "xx"));
|
||||||
|
xxBoostedQuery.setBoost(100000);
|
||||||
|
q.add(xxBoostedQuery);
|
||||||
|
|
||||||
qtest(q, new int[] { 0,2,3 });
|
qtest(q, new int[] { 0,2,3 });
|
||||||
}
|
}
|
||||||
public void testDMQ9() throws Exception {
|
public void testDMQ9() throws Exception {
|
||||||
DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
|
DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
|
||||||
q.add(qp.parse("yy w5^100"));
|
|
||||||
q.add(qp.parse("xx^0"));
|
BooleanQuery booleanQuery = new BooleanQuery();
|
||||||
|
booleanQuery.add(new TermQuery(new Term(FIELD, "yy")), BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
TermQuery boostedQuery = new TermQuery(new Term(FIELD, "w5"));
|
||||||
|
boostedQuery.setBoost(100);
|
||||||
|
booleanQuery.add(boostedQuery, BooleanClause.Occur.SHOULD);
|
||||||
|
q.add(booleanQuery);
|
||||||
|
|
||||||
|
TermQuery xxBoostedQuery = new TermQuery(new Term(FIELD, "xx"));
|
||||||
|
xxBoostedQuery.setBoost(0);
|
||||||
|
q.add(xxBoostedQuery);
|
||||||
|
|
||||||
qtest(q, new int[] { 0,2,3 });
|
qtest(q, new int[] { 0,2,3 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -217,74 +285,199 @@ public class TestSimpleExplanations extends TestExplanations {
|
||||||
/* some simple tests of boolean queries containing term queries */
|
/* some simple tests of boolean queries containing term queries */
|
||||||
|
|
||||||
public void testBQ1() throws Exception {
|
public void testBQ1() throws Exception {
|
||||||
qtest("+w1 +w2", new int[] { 0,1,2,3 });
|
BooleanQuery query = new BooleanQuery();
|
||||||
|
query.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.MUST);
|
||||||
|
query.add(new TermQuery(new Term(FIELD, "w2")), BooleanClause.Occur.MUST);
|
||||||
|
qtest(query, new int[] { 0,1,2,3 });
|
||||||
}
|
}
|
||||||
public void testBQ2() throws Exception {
|
public void testBQ2() throws Exception {
|
||||||
qtest("+yy +w3", new int[] { 2,3 });
|
BooleanQuery query = new BooleanQuery();
|
||||||
|
query.add(new TermQuery(new Term(FIELD, "yy")), BooleanClause.Occur.MUST);
|
||||||
|
query.add(new TermQuery(new Term(FIELD, "w3")), BooleanClause.Occur.MUST);
|
||||||
|
qtest(query, new int[] { 2,3 });
|
||||||
}
|
}
|
||||||
public void testBQ3() throws Exception {
|
public void testBQ3() throws Exception {
|
||||||
qtest("yy +w3", new int[] { 0,1,2,3 });
|
BooleanQuery query = new BooleanQuery();
|
||||||
|
query.add(new TermQuery(new Term(FIELD, "yy")), BooleanClause.Occur.SHOULD);
|
||||||
|
query.add(new TermQuery(new Term(FIELD, "w3")), BooleanClause.Occur.MUST);
|
||||||
|
qtest(query, new int[] { 0,1,2,3 });
|
||||||
}
|
}
|
||||||
public void testBQ4() throws Exception {
|
public void testBQ4() throws Exception {
|
||||||
qtest("w1 (-xx w2)", new int[] { 0,1,2,3 });
|
BooleanQuery outerQuery = new BooleanQuery();
|
||||||
|
outerQuery.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
BooleanQuery innerQuery = new BooleanQuery();
|
||||||
|
innerQuery.add(new TermQuery(new Term(FIELD, "xx")), BooleanClause.Occur.MUST_NOT);
|
||||||
|
innerQuery.add(new TermQuery(new Term(FIELD, "w2")), BooleanClause.Occur.SHOULD);
|
||||||
|
outerQuery.add(innerQuery, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
qtest(outerQuery, new int[] { 0,1,2,3 });
|
||||||
}
|
}
|
||||||
public void testBQ5() throws Exception {
|
public void testBQ5() throws Exception {
|
||||||
qtest("w1 (+qq w2)", new int[] { 0,1,2,3 });
|
BooleanQuery outerQuery = new BooleanQuery();
|
||||||
|
outerQuery.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
BooleanQuery innerQuery = new BooleanQuery();
|
||||||
|
innerQuery.add(new TermQuery(new Term(FIELD, "qq")), BooleanClause.Occur.MUST);
|
||||||
|
innerQuery.add(new TermQuery(new Term(FIELD, "w2")), BooleanClause.Occur.SHOULD);
|
||||||
|
outerQuery.add(innerQuery, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
qtest(outerQuery, new int[] { 0,1,2,3 });
|
||||||
}
|
}
|
||||||
public void testBQ6() throws Exception {
|
public void testBQ6() throws Exception {
|
||||||
qtest("w1 -(-qq w5)", new int[] { 1,2,3 });
|
BooleanQuery outerQuery = new BooleanQuery();
|
||||||
|
outerQuery.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
BooleanQuery innerQuery = new BooleanQuery();
|
||||||
|
innerQuery.add(new TermQuery(new Term(FIELD, "qq")), BooleanClause.Occur.MUST_NOT);
|
||||||
|
innerQuery.add(new TermQuery(new Term(FIELD, "w5")), BooleanClause.Occur.SHOULD);
|
||||||
|
outerQuery.add(innerQuery, BooleanClause.Occur.MUST_NOT);
|
||||||
|
|
||||||
|
qtest(outerQuery, new int[] { 1,2,3 });
|
||||||
}
|
}
|
||||||
public void testBQ7() throws Exception {
|
public void testBQ7() throws Exception {
|
||||||
qtest("+w1 +(qq (xx -w2) (+w3 +w4))", new int[] { 0 });
|
BooleanQuery outerQuery = new BooleanQuery();
|
||||||
|
outerQuery.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.MUST);
|
||||||
|
|
||||||
|
BooleanQuery innerQuery = new BooleanQuery();
|
||||||
|
innerQuery.add(new TermQuery(new Term(FIELD, "qq")), BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
BooleanQuery childLeft = new BooleanQuery();
|
||||||
|
childLeft.add(new TermQuery(new Term(FIELD, "xx")), BooleanClause.Occur.SHOULD);
|
||||||
|
childLeft.add(new TermQuery(new Term(FIELD, "w2")), BooleanClause.Occur.MUST_NOT);
|
||||||
|
innerQuery.add(childLeft, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
BooleanQuery childRight = new BooleanQuery();
|
||||||
|
childRight.add(new TermQuery(new Term(FIELD, "w3")), BooleanClause.Occur.MUST);
|
||||||
|
childRight.add(new TermQuery(new Term(FIELD, "w4")), BooleanClause.Occur.MUST);
|
||||||
|
innerQuery.add(childRight, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
outerQuery.add(innerQuery, BooleanClause.Occur.MUST);
|
||||||
|
|
||||||
|
qtest(outerQuery, new int[] { 0 });
|
||||||
}
|
}
|
||||||
public void testBQ8() throws Exception {
|
public void testBQ8() throws Exception {
|
||||||
qtest("+w1 (qq (xx -w2) (+w3 +w4))", new int[] { 0,1,2,3 });
|
BooleanQuery outerQuery = new BooleanQuery();
|
||||||
|
outerQuery.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.MUST);
|
||||||
|
|
||||||
|
BooleanQuery innerQuery = new BooleanQuery();
|
||||||
|
innerQuery.add(new TermQuery(new Term(FIELD, "qq")), BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
BooleanQuery childLeft = new BooleanQuery();
|
||||||
|
childLeft.add(new TermQuery(new Term(FIELD, "xx")), BooleanClause.Occur.SHOULD);
|
||||||
|
childLeft.add(new TermQuery(new Term(FIELD, "w2")), BooleanClause.Occur.MUST_NOT);
|
||||||
|
innerQuery.add(childLeft, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
BooleanQuery childRight = new BooleanQuery();
|
||||||
|
childRight.add(new TermQuery(new Term(FIELD, "w3")), BooleanClause.Occur.MUST);
|
||||||
|
childRight.add(new TermQuery(new Term(FIELD, "w4")), BooleanClause.Occur.MUST);
|
||||||
|
innerQuery.add(childRight, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
outerQuery.add(innerQuery, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
qtest(outerQuery, new int[] { 0,1,2,3 });
|
||||||
}
|
}
|
||||||
public void testBQ9() throws Exception {
|
public void testBQ9() throws Exception {
|
||||||
qtest("+w1 (qq (-xx w2) -(+w3 +w4))", new int[] { 0,1,2,3 });
|
BooleanQuery outerQuery = new BooleanQuery();
|
||||||
|
outerQuery.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.MUST);
|
||||||
|
|
||||||
|
BooleanQuery innerQuery = new BooleanQuery();
|
||||||
|
innerQuery.add(new TermQuery(new Term(FIELD, "qq")), BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
BooleanQuery childLeft = new BooleanQuery();
|
||||||
|
childLeft.add(new TermQuery(new Term(FIELD, "xx")), BooleanClause.Occur.MUST_NOT);
|
||||||
|
childLeft.add(new TermQuery(new Term(FIELD, "w2")), BooleanClause.Occur.SHOULD);
|
||||||
|
innerQuery.add(childLeft, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
BooleanQuery childRight = new BooleanQuery();
|
||||||
|
childRight.add(new TermQuery(new Term(FIELD, "w3")), BooleanClause.Occur.MUST);
|
||||||
|
childRight.add(new TermQuery(new Term(FIELD, "w4")), BooleanClause.Occur.MUST);
|
||||||
|
innerQuery.add(childRight, BooleanClause.Occur.MUST_NOT);
|
||||||
|
|
||||||
|
outerQuery.add(innerQuery, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
qtest(outerQuery, new int[] { 0,1,2,3 });
|
||||||
}
|
}
|
||||||
public void testBQ10() throws Exception {
|
public void testBQ10() throws Exception {
|
||||||
qtest("+w1 +(qq (-xx w2) -(+w3 +w4))", new int[] { 1 });
|
BooleanQuery outerQuery = new BooleanQuery();
|
||||||
|
outerQuery.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.MUST);
|
||||||
|
|
||||||
|
BooleanQuery innerQuery = new BooleanQuery();
|
||||||
|
innerQuery.add(new TermQuery(new Term(FIELD, "qq")), BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
BooleanQuery childLeft = new BooleanQuery();
|
||||||
|
childLeft.add(new TermQuery(new Term(FIELD, "xx")), BooleanClause.Occur.MUST_NOT);
|
||||||
|
childLeft.add(new TermQuery(new Term(FIELD, "w2")), BooleanClause.Occur.SHOULD);
|
||||||
|
innerQuery.add(childLeft, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
BooleanQuery childRight = new BooleanQuery();
|
||||||
|
childRight.add(new TermQuery(new Term(FIELD, "w3")), BooleanClause.Occur.MUST);
|
||||||
|
childRight.add(new TermQuery(new Term(FIELD, "w4")), BooleanClause.Occur.MUST);
|
||||||
|
innerQuery.add(childRight, BooleanClause.Occur.MUST_NOT);
|
||||||
|
|
||||||
|
outerQuery.add(innerQuery, BooleanClause.Occur.MUST);
|
||||||
|
|
||||||
|
qtest(outerQuery, new int[] { 1 });
|
||||||
}
|
}
|
||||||
public void testBQ11() throws Exception {
|
public void testBQ11() throws Exception {
|
||||||
qtest("w1 w2^1000.0", new int[] { 0,1,2,3 });
|
BooleanQuery query = new BooleanQuery();
|
||||||
|
query.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.SHOULD);
|
||||||
|
TermQuery boostedQuery = new TermQuery(new Term(FIELD, "w1"));
|
||||||
|
boostedQuery.setBoost(1000);
|
||||||
|
query.add(boostedQuery, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
qtest(query, new int[] { 0,1,2,3 });
|
||||||
}
|
}
|
||||||
public void testBQ14() throws Exception {
|
public void testBQ14() throws Exception {
|
||||||
BooleanQuery q = new BooleanQuery(true);
|
BooleanQuery q = new BooleanQuery(true);
|
||||||
q.add(qp.parse("QQQQQ"), BooleanClause.Occur.SHOULD);
|
q.add(new TermQuery(new Term(FIELD, "QQQQQ")), BooleanClause.Occur.SHOULD);
|
||||||
q.add(qp.parse("w1"), BooleanClause.Occur.SHOULD);
|
q.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.SHOULD);
|
||||||
qtest(q, new int[] { 0,1,2,3 });
|
qtest(q, new int[] { 0,1,2,3 });
|
||||||
}
|
}
|
||||||
public void testBQ15() throws Exception {
|
public void testBQ15() throws Exception {
|
||||||
BooleanQuery q = new BooleanQuery(true);
|
BooleanQuery q = new BooleanQuery(true);
|
||||||
q.add(qp.parse("QQQQQ"), BooleanClause.Occur.MUST_NOT);
|
q.add(new TermQuery(new Term(FIELD, "QQQQQ")), BooleanClause.Occur.MUST_NOT);
|
||||||
q.add(qp.parse("w1"), BooleanClause.Occur.SHOULD);
|
q.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.SHOULD);
|
||||||
qtest(q, new int[] { 0,1,2,3 });
|
qtest(q, new int[] { 0,1,2,3 });
|
||||||
}
|
}
|
||||||
public void testBQ16() throws Exception {
|
public void testBQ16() throws Exception {
|
||||||
BooleanQuery q = new BooleanQuery(true);
|
BooleanQuery q = new BooleanQuery(true);
|
||||||
q.add(qp.parse("QQQQQ"), BooleanClause.Occur.SHOULD);
|
q.add(new TermQuery(new Term(FIELD, "QQQQQ")), BooleanClause.Occur.SHOULD);
|
||||||
q.add(qp.parse("w1 -xx"), BooleanClause.Occur.SHOULD);
|
|
||||||
|
BooleanQuery booleanQuery = new BooleanQuery();
|
||||||
|
booleanQuery.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.SHOULD);
|
||||||
|
booleanQuery.add(new TermQuery(new Term(FIELD, "xx")), BooleanClause.Occur.MUST_NOT);
|
||||||
|
|
||||||
|
q.add(booleanQuery, BooleanClause.Occur.SHOULD);
|
||||||
qtest(q, new int[] { 0,1 });
|
qtest(q, new int[] { 0,1 });
|
||||||
}
|
}
|
||||||
public void testBQ17() throws Exception {
|
public void testBQ17() throws Exception {
|
||||||
BooleanQuery q = new BooleanQuery(true);
|
BooleanQuery q = new BooleanQuery(true);
|
||||||
q.add(qp.parse("w2"), BooleanClause.Occur.SHOULD);
|
q.add(new TermQuery(new Term(FIELD, "w2")), BooleanClause.Occur.SHOULD);
|
||||||
q.add(qp.parse("w1 -xx"), BooleanClause.Occur.SHOULD);
|
|
||||||
|
BooleanQuery booleanQuery = new BooleanQuery();
|
||||||
|
booleanQuery.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.SHOULD);
|
||||||
|
booleanQuery.add(new TermQuery(new Term(FIELD, "xx")), BooleanClause.Occur.MUST_NOT);
|
||||||
|
|
||||||
|
q.add(booleanQuery, BooleanClause.Occur.SHOULD);
|
||||||
qtest(q, new int[] { 0,1,2,3 });
|
qtest(q, new int[] { 0,1,2,3 });
|
||||||
}
|
}
|
||||||
public void testBQ19() throws Exception {
|
public void testBQ19() throws Exception {
|
||||||
qtest("-yy w3", new int[] { 0,1 });
|
BooleanQuery query = new BooleanQuery();
|
||||||
|
query.add(new TermQuery(new Term(FIELD, "yy")), BooleanClause.Occur.MUST_NOT);
|
||||||
|
query.add(new TermQuery(new Term(FIELD, "w3")), BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
qtest(query, new int[] { 0,1 });
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testBQ20() throws Exception {
|
public void testBQ20() throws Exception {
|
||||||
BooleanQuery q = new BooleanQuery();
|
BooleanQuery q = new BooleanQuery();
|
||||||
q.setMinimumNumberShouldMatch(2);
|
q.setMinimumNumberShouldMatch(2);
|
||||||
q.add(qp.parse("QQQQQ"), BooleanClause.Occur.SHOULD);
|
q.add(new TermQuery(new Term(FIELD, "QQQQQ")), BooleanClause.Occur.SHOULD);
|
||||||
q.add(qp.parse("yy"), BooleanClause.Occur.SHOULD);
|
q.add(new TermQuery(new Term(FIELD, "yy")), BooleanClause.Occur.SHOULD);
|
||||||
q.add(qp.parse("zz"), BooleanClause.Occur.SHOULD);
|
q.add(new TermQuery(new Term(FIELD, "zz")), BooleanClause.Occur.SHOULD);
|
||||||
q.add(qp.parse("w5"), BooleanClause.Occur.SHOULD);
|
q.add(new TermQuery(new Term(FIELD, "w5")), BooleanClause.Occur.SHOULD);
|
||||||
q.add(qp.parse("w4"), BooleanClause.Occur.SHOULD);
|
q.add(new TermQuery(new Term(FIELD, "w4")), BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
qtest(q, new int[] { 0,3 });
|
qtest(q, new int[] { 0,3 });
|
||||||
|
|
||||||
|
@ -293,58 +486,260 @@ public class TestSimpleExplanations extends TestExplanations {
|
||||||
/* BQ of TQ: using alt so some fields have zero boost and some don't */
|
/* BQ of TQ: using alt so some fields have zero boost and some don't */
|
||||||
|
|
||||||
public void testMultiFieldBQ1() throws Exception {
|
public void testMultiFieldBQ1() throws Exception {
|
||||||
qtest("+w1 +alt:w2", new int[] { 0,1,2,3 });
|
BooleanQuery query = new BooleanQuery();
|
||||||
|
query.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.MUST);
|
||||||
|
query.add(new TermQuery(new Term(ALTFIELD, "w2")), BooleanClause.Occur.MUST);
|
||||||
|
|
||||||
|
qtest(query, new int[] { 0,1,2,3 });
|
||||||
}
|
}
|
||||||
public void testMultiFieldBQ2() throws Exception {
|
public void testMultiFieldBQ2() throws Exception {
|
||||||
qtest("+yy +alt:w3", new int[] { 2,3 });
|
BooleanQuery query = new BooleanQuery();
|
||||||
|
query.add(new TermQuery(new Term(FIELD, "yy")), BooleanClause.Occur.MUST);
|
||||||
|
query.add(new TermQuery(new Term(ALTFIELD, "w3")), BooleanClause.Occur.MUST);
|
||||||
|
|
||||||
|
qtest(query, new int[] { 2,3 });
|
||||||
}
|
}
|
||||||
public void testMultiFieldBQ3() throws Exception {
|
public void testMultiFieldBQ3() throws Exception {
|
||||||
qtest("yy +alt:w3", new int[] { 0,1,2,3 });
|
BooleanQuery query = new BooleanQuery();
|
||||||
|
query.add(new TermQuery(new Term(FIELD, "yy")), BooleanClause.Occur.SHOULD);
|
||||||
|
query.add(new TermQuery(new Term(ALTFIELD, "w3")), BooleanClause.Occur.MUST);
|
||||||
|
|
||||||
|
qtest(query, new int[] { 0,1,2,3 });
|
||||||
}
|
}
|
||||||
public void testMultiFieldBQ4() throws Exception {
|
public void testMultiFieldBQ4() throws Exception {
|
||||||
qtest("w1 (-xx alt:w2)", new int[] { 0,1,2,3 });
|
BooleanQuery outerQuery = new BooleanQuery();
|
||||||
|
outerQuery.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
BooleanQuery innerQuery = new BooleanQuery();
|
||||||
|
innerQuery.add(new TermQuery(new Term(FIELD, "xx")), BooleanClause.Occur.MUST_NOT);
|
||||||
|
innerQuery.add(new TermQuery(new Term(ALTFIELD, "w2")), BooleanClause.Occur.SHOULD);
|
||||||
|
outerQuery.add(innerQuery, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
qtest(outerQuery, new int[] { 0,1,2,3 });
|
||||||
}
|
}
|
||||||
public void testMultiFieldBQ5() throws Exception {
|
public void testMultiFieldBQ5() throws Exception {
|
||||||
qtest("w1 (+alt:qq alt:w2)", new int[] { 0,1,2,3 });
|
BooleanQuery outerQuery = new BooleanQuery();
|
||||||
|
outerQuery.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
BooleanQuery innerQuery = new BooleanQuery();
|
||||||
|
innerQuery.add(new TermQuery(new Term(ALTFIELD, "qq")), BooleanClause.Occur.MUST);
|
||||||
|
innerQuery.add(new TermQuery(new Term(ALTFIELD, "w2")), BooleanClause.Occur.SHOULD);
|
||||||
|
outerQuery.add(innerQuery, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
qtest(outerQuery, new int[] { 0,1,2,3 });
|
||||||
}
|
}
|
||||||
public void testMultiFieldBQ6() throws Exception {
|
public void testMultiFieldBQ6() throws Exception {
|
||||||
qtest("w1 -(-alt:qq alt:w5)", new int[] { 1,2,3 });
|
BooleanQuery outerQuery = new BooleanQuery();
|
||||||
|
outerQuery.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
BooleanQuery innerQuery = new BooleanQuery();
|
||||||
|
innerQuery.add(new TermQuery(new Term(ALTFIELD, "qq")), BooleanClause.Occur.MUST_NOT);
|
||||||
|
innerQuery.add(new TermQuery(new Term(ALTFIELD, "w5")), BooleanClause.Occur.SHOULD);
|
||||||
|
outerQuery.add(innerQuery, BooleanClause.Occur.MUST_NOT);
|
||||||
|
|
||||||
|
qtest(outerQuery, new int[] { 1,2,3 });
|
||||||
}
|
}
|
||||||
public void testMultiFieldBQ7() throws Exception {
|
public void testMultiFieldBQ7() throws Exception {
|
||||||
qtest("+w1 +(alt:qq (alt:xx -alt:w2) (+alt:w3 +alt:w4))", new int[] { 0 });
|
BooleanQuery outerQuery = new BooleanQuery();
|
||||||
|
outerQuery.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.MUST);
|
||||||
|
|
||||||
|
BooleanQuery innerQuery = new BooleanQuery();
|
||||||
|
innerQuery.add(new TermQuery(new Term(ALTFIELD, "qq")), BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
BooleanQuery childLeft = new BooleanQuery();
|
||||||
|
childLeft.add(new TermQuery(new Term(ALTFIELD, "xx")), BooleanClause.Occur.SHOULD);
|
||||||
|
childLeft.add(new TermQuery(new Term(ALTFIELD, "w2")), BooleanClause.Occur.MUST_NOT);
|
||||||
|
innerQuery.add(childLeft, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
BooleanQuery childRight = new BooleanQuery();
|
||||||
|
childRight.add(new TermQuery(new Term(ALTFIELD, "w3")), BooleanClause.Occur.MUST);
|
||||||
|
childRight.add(new TermQuery(new Term(ALTFIELD, "w4")), BooleanClause.Occur.MUST);
|
||||||
|
innerQuery.add(childRight, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
outerQuery.add(innerQuery, BooleanClause.Occur.MUST);
|
||||||
|
|
||||||
|
qtest(outerQuery, new int[] { 0 });
|
||||||
}
|
}
|
||||||
public void testMultiFieldBQ8() throws Exception {
|
public void testMultiFieldBQ8() throws Exception {
|
||||||
qtest("+alt:w1 (qq (alt:xx -w2) (+alt:w3 +w4))", new int[] { 0,1,2,3 });
|
BooleanQuery outerQuery = new BooleanQuery();
|
||||||
|
outerQuery.add(new TermQuery(new Term(ALTFIELD, "w1")), BooleanClause.Occur.MUST);
|
||||||
|
|
||||||
|
BooleanQuery innerQuery = new BooleanQuery();
|
||||||
|
innerQuery.add(new TermQuery(new Term(FIELD, "qq")), BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
BooleanQuery childLeft = new BooleanQuery();
|
||||||
|
childLeft.add(new TermQuery(new Term(ALTFIELD, "xx")), BooleanClause.Occur.SHOULD);
|
||||||
|
childLeft.add(new TermQuery(new Term(FIELD, "w2")), BooleanClause.Occur.MUST_NOT);
|
||||||
|
innerQuery.add(childLeft, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
BooleanQuery childRight = new BooleanQuery();
|
||||||
|
childRight.add(new TermQuery(new Term(ALTFIELD, "w3")), BooleanClause.Occur.MUST);
|
||||||
|
childRight.add(new TermQuery(new Term(FIELD, "w4")), BooleanClause.Occur.MUST);
|
||||||
|
innerQuery.add(childRight, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
outerQuery.add(innerQuery, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
qtest(outerQuery, new int[] { 0,1,2,3 });
|
||||||
}
|
}
|
||||||
public void testMultiFieldBQ9() throws Exception {
|
public void testMultiFieldBQ9() throws Exception {
|
||||||
qtest("+w1 (alt:qq (-xx w2) -(+alt:w3 +w4))", new int[] { 0,1,2,3 });
|
BooleanQuery outerQuery = new BooleanQuery();
|
||||||
|
outerQuery.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.MUST);
|
||||||
|
|
||||||
|
BooleanQuery innerQuery = new BooleanQuery();
|
||||||
|
innerQuery.add(new TermQuery(new Term(ALTFIELD, "qq")), BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
BooleanQuery childLeft = new BooleanQuery();
|
||||||
|
childLeft.add(new TermQuery(new Term(FIELD, "xx")), BooleanClause.Occur.MUST_NOT);
|
||||||
|
childLeft.add(new TermQuery(new Term(FIELD, "w2")), BooleanClause.Occur.SHOULD);
|
||||||
|
innerQuery.add(childLeft, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
BooleanQuery childRight = new BooleanQuery();
|
||||||
|
childRight.add(new TermQuery(new Term(ALTFIELD, "w3")), BooleanClause.Occur.MUST);
|
||||||
|
childRight.add(new TermQuery(new Term(FIELD, "w4")), BooleanClause.Occur.MUST);
|
||||||
|
innerQuery.add(childRight, BooleanClause.Occur.MUST_NOT);
|
||||||
|
|
||||||
|
outerQuery.add(innerQuery, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
qtest(outerQuery, new int[] { 0,1,2,3 });
|
||||||
}
|
}
|
||||||
public void testMultiFieldBQ10() throws Exception {
|
public void testMultiFieldBQ10() throws Exception {
|
||||||
qtest("+w1 +(alt:qq (-xx alt:w2) -(+alt:w3 +w4))", new int[] { 1 });
|
BooleanQuery outerQuery = new BooleanQuery();
|
||||||
|
outerQuery.add(new TermQuery(new Term(FIELD, "w1")), BooleanClause.Occur.MUST);
|
||||||
|
|
||||||
|
BooleanQuery innerQuery = new BooleanQuery();
|
||||||
|
innerQuery.add(new TermQuery(new Term(ALTFIELD, "qq")), BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
BooleanQuery childLeft = new BooleanQuery();
|
||||||
|
childLeft.add(new TermQuery(new Term(FIELD, "xx")), BooleanClause.Occur.MUST_NOT);
|
||||||
|
childLeft.add(new TermQuery(new Term(ALTFIELD, "w2")), BooleanClause.Occur.SHOULD);
|
||||||
|
innerQuery.add(childLeft, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
BooleanQuery childRight = new BooleanQuery();
|
||||||
|
childRight.add(new TermQuery(new Term(ALTFIELD, "w3")), BooleanClause.Occur.MUST);
|
||||||
|
childRight.add(new TermQuery(new Term(FIELD, "w4")), BooleanClause.Occur.MUST);
|
||||||
|
innerQuery.add(childRight, BooleanClause.Occur.MUST_NOT);
|
||||||
|
|
||||||
|
outerQuery.add(innerQuery, BooleanClause.Occur.MUST);
|
||||||
|
|
||||||
|
qtest(outerQuery, new int[] { 1 });
|
||||||
}
|
}
|
||||||
|
|
||||||
/* BQ of PQ: using alt so some fields have zero boost and some don't */
|
/* BQ of PQ: using alt so some fields have zero boost and some don't */
|
||||||
|
|
||||||
public void testMultiFieldBQofPQ1() throws Exception {
|
public void testMultiFieldBQofPQ1() throws Exception {
|
||||||
qtest("\"w1 w2\" alt:\"w1 w2\"", new int[] { 0 });
|
BooleanQuery query = new BooleanQuery();
|
||||||
|
|
||||||
|
PhraseQuery leftChild = new PhraseQuery();
|
||||||
|
leftChild.add(new Term(FIELD, "w1"));
|
||||||
|
leftChild.add(new Term(FIELD, "w2"));
|
||||||
|
query.add(leftChild, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
PhraseQuery rightChild = new PhraseQuery();
|
||||||
|
rightChild.add(new Term(ALTFIELD, "w1"));
|
||||||
|
rightChild.add(new Term(ALTFIELD, "w2"));
|
||||||
|
query.add(rightChild, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
qtest(query, new int[] { 0 });
|
||||||
}
|
}
|
||||||
public void testMultiFieldBQofPQ2() throws Exception {
|
public void testMultiFieldBQofPQ2() throws Exception {
|
||||||
qtest("\"w1 w3\" alt:\"w1 w3\"", new int[] { 1,3 });
|
BooleanQuery query = new BooleanQuery();
|
||||||
|
|
||||||
|
PhraseQuery leftChild = new PhraseQuery();
|
||||||
|
leftChild.add(new Term(FIELD, "w1"));
|
||||||
|
leftChild.add(new Term(FIELD, "w3"));
|
||||||
|
query.add(leftChild, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
PhraseQuery rightChild = new PhraseQuery();
|
||||||
|
rightChild.add(new Term(ALTFIELD, "w1"));
|
||||||
|
rightChild.add(new Term(ALTFIELD, "w3"));
|
||||||
|
query.add(rightChild, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
qtest(query, new int[] { 1,3 });
|
||||||
}
|
}
|
||||||
public void testMultiFieldBQofPQ3() throws Exception {
|
public void testMultiFieldBQofPQ3() throws Exception {
|
||||||
qtest("\"w1 w2\"~1 alt:\"w1 w2\"~1", new int[] { 0,1,2 });
|
BooleanQuery query = new BooleanQuery();
|
||||||
|
|
||||||
|
PhraseQuery leftChild = new PhraseQuery();
|
||||||
|
leftChild.setSlop(1);
|
||||||
|
leftChild.add(new Term(FIELD, "w1"));
|
||||||
|
leftChild.add(new Term(FIELD, "w2"));
|
||||||
|
query.add(leftChild, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
PhraseQuery rightChild = new PhraseQuery();
|
||||||
|
rightChild.setSlop(1);
|
||||||
|
rightChild.add(new Term(ALTFIELD, "w1"));
|
||||||
|
rightChild.add(new Term(ALTFIELD, "w2"));
|
||||||
|
query.add(rightChild, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
qtest(query, new int[] { 0,1,2 });
|
||||||
}
|
}
|
||||||
public void testMultiFieldBQofPQ4() throws Exception {
|
public void testMultiFieldBQofPQ4() throws Exception {
|
||||||
qtest("\"w2 w3\"~1 alt:\"w2 w3\"~1", new int[] { 0,1,2,3 });
|
BooleanQuery query = new BooleanQuery();
|
||||||
|
|
||||||
|
PhraseQuery leftChild = new PhraseQuery();
|
||||||
|
leftChild.setSlop(1);
|
||||||
|
leftChild.add(new Term(FIELD, "w2"));
|
||||||
|
leftChild.add(new Term(FIELD, "w3"));
|
||||||
|
query.add(leftChild, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
PhraseQuery rightChild = new PhraseQuery();
|
||||||
|
rightChild.setSlop(1);
|
||||||
|
rightChild.add(new Term(ALTFIELD, "w2"));
|
||||||
|
rightChild.add(new Term(ALTFIELD, "w3"));
|
||||||
|
query.add(rightChild, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
qtest(query, new int[] { 0,1,2,3 });
|
||||||
}
|
}
|
||||||
public void testMultiFieldBQofPQ5() throws Exception {
|
public void testMultiFieldBQofPQ5() throws Exception {
|
||||||
qtest("\"w3 w2\"~1 alt:\"w3 w2\"~1", new int[] { 1,3 });
|
BooleanQuery query = new BooleanQuery();
|
||||||
|
|
||||||
|
PhraseQuery leftChild = new PhraseQuery();
|
||||||
|
leftChild.setSlop(1);
|
||||||
|
leftChild.add(new Term(FIELD, "w3"));
|
||||||
|
leftChild.add(new Term(FIELD, "w2"));
|
||||||
|
query.add(leftChild, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
PhraseQuery rightChild = new PhraseQuery();
|
||||||
|
rightChild.setSlop(1);
|
||||||
|
rightChild.add(new Term(ALTFIELD, "w3"));
|
||||||
|
rightChild.add(new Term(ALTFIELD, "w2"));
|
||||||
|
query.add(rightChild, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
qtest(query, new int[] { 1,3 });
|
||||||
}
|
}
|
||||||
public void testMultiFieldBQofPQ6() throws Exception {
|
public void testMultiFieldBQofPQ6() throws Exception {
|
||||||
qtest("\"w3 w2\"~2 alt:\"w3 w2\"~2", new int[] { 0,1,3 });
|
BooleanQuery query = new BooleanQuery();
|
||||||
|
|
||||||
|
PhraseQuery leftChild = new PhraseQuery();
|
||||||
|
leftChild.setSlop(2);
|
||||||
|
leftChild.add(new Term(FIELD, "w3"));
|
||||||
|
leftChild.add(new Term(FIELD, "w2"));
|
||||||
|
query.add(leftChild, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
PhraseQuery rightChild = new PhraseQuery();
|
||||||
|
rightChild.setSlop(2);
|
||||||
|
rightChild.add(new Term(ALTFIELD, "w3"));
|
||||||
|
rightChild.add(new Term(ALTFIELD, "w2"));
|
||||||
|
query.add(rightChild, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
qtest(query, new int[] { 0,1,3 });
|
||||||
}
|
}
|
||||||
public void testMultiFieldBQofPQ7() throws Exception {
|
public void testMultiFieldBQofPQ7() throws Exception {
|
||||||
qtest("\"w3 w2\"~3 alt:\"w3 w2\"~3", new int[] { 0,1,2,3 });
|
BooleanQuery query = new BooleanQuery();
|
||||||
|
|
||||||
|
PhraseQuery leftChild = new PhraseQuery();
|
||||||
|
leftChild.setSlop(3);
|
||||||
|
leftChild.add(new Term(FIELD, "w3"));
|
||||||
|
leftChild.add(new Term(FIELD, "w2"));
|
||||||
|
query.add(leftChild, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
PhraseQuery rightChild = new PhraseQuery();
|
||||||
|
rightChild.setSlop(1);
|
||||||
|
rightChild.add(new Term(ALTFIELD, "w3"));
|
||||||
|
rightChild.add(new Term(ALTFIELD, "w2"));
|
||||||
|
query.add(rightChild, BooleanClause.Occur.SHOULD);
|
||||||
|
|
||||||
|
qtest(query, new int[] { 0,1,2,3 });
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@ import org.apache.lucene.document.Field;
|
||||||
import org.apache.lucene.index.IndexReader;
|
import org.apache.lucene.index.IndexReader;
|
||||||
import org.apache.lucene.index.IndexReader.AtomicReaderContext;
|
import org.apache.lucene.index.IndexReader.AtomicReaderContext;
|
||||||
import org.apache.lucene.index.RandomIndexWriter;
|
import org.apache.lucene.index.RandomIndexWriter;
|
||||||
import org.apache.lucene.queryParser.QueryParser;
|
import org.apache.lucene.index.Term;
|
||||||
import org.apache.lucene.search.TimeLimitingCollector.TimeExceededException;
|
import org.apache.lucene.search.TimeLimitingCollector.TimeExceededException;
|
||||||
import org.apache.lucene.store.Directory;
|
import org.apache.lucene.store.Directory;
|
||||||
import org.apache.lucene.util.LuceneTestCase;
|
import org.apache.lucene.util.LuceneTestCase;
|
||||||
|
@ -84,13 +84,17 @@ public class TestTimeLimitingCollector extends LuceneTestCase {
|
||||||
iw.close();
|
iw.close();
|
||||||
searcher = newSearcher(reader);
|
searcher = newSearcher(reader);
|
||||||
|
|
||||||
String qtxt = "one";
|
BooleanQuery booleanQuery = new BooleanQuery();
|
||||||
|
booleanQuery.add(new TermQuery(new Term(FIELD_NAME, "one")), BooleanClause.Occur.SHOULD);
|
||||||
// start from 1, so that the 0th doc never matches
|
// start from 1, so that the 0th doc never matches
|
||||||
for (int i = 1; i < docText.length; i++) {
|
for (int i = 1; i < docText.length; i++) {
|
||||||
qtxt += ' ' + docText[i]; // large query so that search will be longer
|
String[] docTextParts = docText[i].split("\\s+");
|
||||||
|
for (String docTextPart : docTextParts) { // large query so that search will be longer
|
||||||
|
booleanQuery.add(new TermQuery(new Term(FIELD_NAME, docTextPart)), BooleanClause.Occur.SHOULD);
|
||||||
}
|
}
|
||||||
QueryParser queryParser = new QueryParser(TEST_VERSION_CURRENT, FIELD_NAME, new MockAnalyzer(random));
|
}
|
||||||
query = queryParser.parse(qtxt);
|
|
||||||
|
query = booleanQuery;
|
||||||
|
|
||||||
// warm the searcher
|
// warm the searcher
|
||||||
searcher.search(query, null, 1000);
|
searcher.search(query, null, 1000);
|
||||||
|
|
|
@ -28,7 +28,6 @@ import org.apache.lucene.index.MultiFields;
|
||||||
import org.apache.lucene.index.RandomIndexWriter;
|
import org.apache.lucene.index.RandomIndexWriter;
|
||||||
import org.apache.lucene.index.Term;
|
import org.apache.lucene.index.Term;
|
||||||
import org.apache.lucene.index.Terms;
|
import org.apache.lucene.index.Terms;
|
||||||
import org.apache.lucene.queryParser.QueryParser;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@ -268,32 +267,74 @@ public class TestWildcard
|
||||||
*/
|
*/
|
||||||
public void testParsingAndSearching() throws Exception {
|
public void testParsingAndSearching() throws Exception {
|
||||||
String field = "content";
|
String field = "content";
|
||||||
QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, field, new MockAnalyzer(random));
|
|
||||||
qp.setAllowLeadingWildcard(true);
|
|
||||||
String docs[] = {
|
String docs[] = {
|
||||||
"\\ abcdefg1",
|
"\\ abcdefg1",
|
||||||
"\\79 hijklmn1",
|
"\\79 hijklmn1",
|
||||||
"\\\\ opqrstu1",
|
"\\\\ opqrstu1",
|
||||||
};
|
};
|
||||||
|
|
||||||
// queries that should find all docs
|
// queries that should find all docs
|
||||||
String matchAll[] = {
|
Query matchAll[] = {
|
||||||
"*", "*1", "**1", "*?", "*?1", "?*1", "**", "***", "\\\\*"
|
new WildcardQuery(new Term(field, "*")),
|
||||||
|
new WildcardQuery(new Term(field, "*1")),
|
||||||
|
new WildcardQuery(new Term(field, "**1")),
|
||||||
|
new WildcardQuery(new Term(field, "*?")),
|
||||||
|
new WildcardQuery(new Term(field, "*?1")),
|
||||||
|
new WildcardQuery(new Term(field, "?*1")),
|
||||||
|
new WildcardQuery(new Term(field, "**")),
|
||||||
|
new WildcardQuery(new Term(field, "***")),
|
||||||
|
new WildcardQuery(new Term(field, "\\\\*"))
|
||||||
};
|
};
|
||||||
|
|
||||||
// queries that should find no docs
|
// queries that should find no docs
|
||||||
String matchNone[] = {
|
Query matchNone[] = {
|
||||||
"a*h", "a?h", "*a*h", "?a", "a?",
|
new WildcardQuery(new Term(field, "a*h")),
|
||||||
|
new WildcardQuery(new Term(field, "a?h")),
|
||||||
|
new WildcardQuery(new Term(field, "*a*h")),
|
||||||
|
new WildcardQuery(new Term(field, "?a")),
|
||||||
|
new WildcardQuery(new Term(field, "a?"))
|
||||||
};
|
};
|
||||||
// queries that should be parsed to prefix queries
|
|
||||||
String matchOneDocPrefix[][] = {
|
PrefixQuery matchOneDocPrefix[][] = {
|
||||||
{"a*", "ab*", "abc*", }, // these should find only doc 0
|
{new PrefixQuery(new Term(field, "a")),
|
||||||
{"h*", "hi*", "hij*", "\\\\7*"}, // these should find only doc 1
|
new PrefixQuery(new Term(field, "ab")),
|
||||||
{"o*", "op*", "opq*", "\\\\\\\\*"}, // these should find only doc 2
|
new PrefixQuery(new Term(field, "abc"))}, // these should find only doc 0
|
||||||
|
|
||||||
|
{new PrefixQuery(new Term(field, "h")),
|
||||||
|
new PrefixQuery(new Term(field, "hi")),
|
||||||
|
new PrefixQuery(new Term(field, "hij")),
|
||||||
|
new PrefixQuery(new Term(field, "\\7"))}, // these should find only doc 1
|
||||||
|
|
||||||
|
{new PrefixQuery(new Term(field, "o")),
|
||||||
|
new PrefixQuery(new Term(field, "op")),
|
||||||
|
new PrefixQuery(new Term(field, "opq")),
|
||||||
|
new PrefixQuery(new Term(field, "\\\\"))}, // these should find only doc 2
|
||||||
};
|
};
|
||||||
// queries that should be parsed to wildcard queries
|
|
||||||
String matchOneDocWild[][] = {
|
WildcardQuery matchOneDocWild[][] = {
|
||||||
{"*a*", "*ab*", "*abc**", "ab*e*", "*g?", "*f?1", "abc**"}, // these should find only doc 0
|
|
||||||
{"*h*", "*hi*", "*hij**", "hi*k*", "*n?", "*m?1", "hij**"}, // these should find only doc 1
|
{new WildcardQuery(new Term(field, "*a*")), // these should find only doc 0
|
||||||
{"*o*", "*op*", "*opq**", "op*q*", "*u?", "*t?1", "opq**"}, // these should find only doc 2
|
new WildcardQuery(new Term(field, "*ab*")),
|
||||||
|
new WildcardQuery(new Term(field, "*abc**")),
|
||||||
|
new WildcardQuery(new Term(field, "ab*e*")),
|
||||||
|
new WildcardQuery(new Term(field, "*g?")),
|
||||||
|
new WildcardQuery(new Term(field, "*f?1"))},
|
||||||
|
|
||||||
|
{new WildcardQuery(new Term(field, "*h*")), // these should find only doc 1
|
||||||
|
new WildcardQuery(new Term(field, "*hi*")),
|
||||||
|
new WildcardQuery(new Term(field, "*hij**")),
|
||||||
|
new WildcardQuery(new Term(field, "hi*k*")),
|
||||||
|
new WildcardQuery(new Term(field, "*n?")),
|
||||||
|
new WildcardQuery(new Term(field, "*m?1")),
|
||||||
|
new WildcardQuery(new Term(field, "hij**"))},
|
||||||
|
|
||||||
|
{new WildcardQuery(new Term(field, "*o*")), // these should find only doc 2
|
||||||
|
new WildcardQuery(new Term(field, "*op*")),
|
||||||
|
new WildcardQuery(new Term(field, "*opq**")),
|
||||||
|
new WildcardQuery(new Term(field, "op*q*")),
|
||||||
|
new WildcardQuery(new Term(field, "*u?")),
|
||||||
|
new WildcardQuery(new Term(field, "*t?1")),
|
||||||
|
new WildcardQuery(new Term(field, "opq**"))}
|
||||||
};
|
};
|
||||||
|
|
||||||
// prepare the index
|
// prepare the index
|
||||||
|
@ -311,43 +352,35 @@ public class TestWildcard
|
||||||
IndexSearcher searcher = new IndexSearcher(dir, true);
|
IndexSearcher searcher = new IndexSearcher(dir, true);
|
||||||
|
|
||||||
// test queries that must find all
|
// test queries that must find all
|
||||||
for (int i = 0; i < matchAll.length; i++) {
|
for (Query q : matchAll) {
|
||||||
String qtxt = matchAll[i];
|
if (VERBOSE) System.out.println("matchAll: q=" + q + " " + q.getClass().getName());
|
||||||
Query q = qp.parse(qtxt);
|
|
||||||
if (VERBOSE) System.out.println("matchAll: qtxt="+qtxt+" q="+q+" "+q.getClass().getName());
|
|
||||||
ScoreDoc[] hits = searcher.search(q, null, 1000).scoreDocs;
|
ScoreDoc[] hits = searcher.search(q, null, 1000).scoreDocs;
|
||||||
assertEquals(docs.length, hits.length);
|
assertEquals(docs.length, hits.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
// test queries that must find none
|
// test queries that must find none
|
||||||
for (int i = 0; i < matchNone.length; i++) {
|
for (Query q : matchNone) {
|
||||||
String qtxt = matchNone[i];
|
if (VERBOSE) System.out.println("matchNone: q=" + q + " " + q.getClass().getName());
|
||||||
Query q = qp.parse(qtxt);
|
|
||||||
if (VERBOSE) System.out.println("matchNone: qtxt="+qtxt+" q="+q+" "+q.getClass().getName());
|
|
||||||
ScoreDoc[] hits = searcher.search(q, null, 1000).scoreDocs;
|
ScoreDoc[] hits = searcher.search(q, null, 1000).scoreDocs;
|
||||||
assertEquals(0, hits.length);
|
assertEquals(0, hits.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
// test queries that must be prefix queries and must find only one doc
|
// thest the prefi queries find only one doc
|
||||||
for (int i = 0; i < matchOneDocPrefix.length; i++) {
|
for (int i = 0; i < matchOneDocPrefix.length; i++) {
|
||||||
for (int j = 0; j < matchOneDocPrefix[i].length; j++) {
|
for (int j = 0; j < matchOneDocPrefix[i].length; j++) {
|
||||||
String qtxt = matchOneDocPrefix[i][j];
|
Query q = matchOneDocPrefix[i][j];
|
||||||
Query q = qp.parse(qtxt);
|
if (VERBOSE) System.out.println("match 1 prefix: doc="+docs[i]+" q="+q+" "+q.getClass().getName());
|
||||||
if (VERBOSE) System.out.println("match 1 prefix: doc="+docs[i]+" qtxt="+qtxt+" q="+q+" "+q.getClass().getName());
|
|
||||||
assertEquals(PrefixQuery.class, q.getClass());
|
|
||||||
ScoreDoc[] hits = searcher.search(q, null, 1000).scoreDocs;
|
ScoreDoc[] hits = searcher.search(q, null, 1000).scoreDocs;
|
||||||
assertEquals(1,hits.length);
|
assertEquals(1,hits.length);
|
||||||
assertEquals(i,hits[0].doc);
|
assertEquals(i,hits[0].doc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// test queries that must be wildcard queries and must find only one doc
|
// test the wildcard queries find only one doc
|
||||||
for (int i = 0; i < matchOneDocPrefix.length; i++) {
|
for (int i = 0; i < matchOneDocWild.length; i++) {
|
||||||
for (int j = 0; j < matchOneDocWild[i].length; j++) {
|
for (int j = 0; j < matchOneDocWild[i].length; j++) {
|
||||||
String qtxt = matchOneDocWild[i][j];
|
Query q = matchOneDocWild[i][j];
|
||||||
Query q = qp.parse(qtxt);
|
if (VERBOSE) System.out.println("match 1 wild: doc="+docs[i]+" q="+q+" "+q.getClass().getName());
|
||||||
if (VERBOSE) System.out.println("match 1 wild: doc="+docs[i]+" qtxt="+qtxt+" q="+q+" "+q.getClass().getName());
|
|
||||||
assertEquals(WildcardQuery.class, q.getClass());
|
|
||||||
ScoreDoc[] hits = searcher.search(q, null, 1000).scoreDocs;
|
ScoreDoc[] hits = searcher.search(q, null, 1000).scoreDocs;
|
||||||
assertEquals(1,hits.length);
|
assertEquals(1,hits.length);
|
||||||
assertEquals(i,hits[0].doc);
|
assertEquals(i,hits[0].doc);
|
||||||
|
|
|
@ -25,7 +25,6 @@ import org.apache.lucene.index.IndexReader.AtomicReaderContext;
|
||||||
import org.apache.lucene.index.IndexReader.ReaderContext;
|
import org.apache.lucene.index.IndexReader.ReaderContext;
|
||||||
import org.apache.lucene.index.RandomIndexWriter;
|
import org.apache.lucene.index.RandomIndexWriter;
|
||||||
import org.apache.lucene.index.Term;
|
import org.apache.lucene.index.Term;
|
||||||
import org.apache.lucene.queryParser.QueryParser;
|
|
||||||
import org.apache.lucene.search.CheckHits;
|
import org.apache.lucene.search.CheckHits;
|
||||||
import org.apache.lucene.search.Explanation;
|
import org.apache.lucene.search.Explanation;
|
||||||
import org.apache.lucene.search.IndexSearcher;
|
import org.apache.lucene.search.IndexSearcher;
|
||||||
|
@ -42,8 +41,6 @@ public class TestNearSpansOrdered extends LuceneTestCase {
|
||||||
protected IndexReader reader;
|
protected IndexReader reader;
|
||||||
|
|
||||||
public static final String FIELD = "field";
|
public static final String FIELD = "field";
|
||||||
public static final QueryParser qp =
|
|
||||||
new QueryParser(TEST_VERSION_CURRENT, FIELD, new MockAnalyzer(random));
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void tearDown() throws Exception {
|
public void tearDown() throws Exception {
|
||||||
|
|
Loading…
Reference in New Issue