public void testBasics() { final int iters = scaledRandomIntBetween(5, 25); for (int j = 0; j < iters; j++) { String[] fields = new String[1 + random().nextInt(10)]; for (int i = 0; i < fields.length; i++) { fields[i] = TestUtil.randomRealisticUnicodeString(random(), 1, 10); } String term = TestUtil.randomRealisticUnicodeString(random(), 1, 10); Term[] terms = toTerms(fields, term); boolean disableCoord = random().nextBoolean(); boolean useBoolean = random().nextBoolean(); float tieBreaker = random().nextFloat(); BlendedTermQuery query = useBoolean ? BlendedTermQuery.booleanBlendedQuery(terms, disableCoord) : BlendedTermQuery.dismaxBlendedQuery(terms, tieBreaker); QueryUtils.check(query); terms = toTerms(fields, term); BlendedTermQuery query2 = useBoolean ? BlendedTermQuery.booleanBlendedQuery(terms, disableCoord) : BlendedTermQuery.dismaxBlendedQuery(terms, tieBreaker); assertEquals(query, query2); } }
private void doTestRank (ValueSource valueSource) throws Exception { FunctionQuery functionQuery = new FunctionQuery(valueSource); IndexReader r = DirectoryReader.open(dir); IndexSearcher s = newSearcher(r); log("test: "+ functionQuery); QueryUtils.check(random(), functionQuery,s); ScoreDoc[] h = s.search(functionQuery, null, 1000).scoreDocs; assertEquals("All docs should be matched!",N_DOCS,h.length); String prevID = "ID"+(N_DOCS+1); // greater than all ids of docs in this test for (int i=0; i<h.length; i++) { String resID = s.doc(h[i].doc).get(ID_FIELD); log(i+". score="+h[i].score+" - "+resID); log(s.explain(functionQuery,h[i].doc)); assertTrue("res id "+resID+" should be < prev res id "+prevID, resID.compareTo(prevID)<0); prevID = resID; } r.close(); }
@Test public void testSpanNearExact() throws Exception { SpanTermQuery term1 = new SpanTermQuery(new Term("field", "seventy")); SpanTermQuery term2 = new SpanTermQuery(new Term("field", "seven")); SpanNearQuery query = new SpanNearQuery(new SpanQuery[] {term1, term2}, 0, true); checkHits(query, new int[] {77, 177, 277, 377, 477, 577, 677, 777, 877, 977, 1077, 1177, 1277, 1377, 1477, 1577, 1677, 1777, 1877, 1977}); assertTrue(searcher.explain(query, 77).getValue() > 0.0f); assertTrue(searcher.explain(query, 977).getValue() > 0.0f); QueryUtils.check(term1); QueryUtils.check(term2); QueryUtils.checkUnequal(term1,term2); }
public void testRewrite1() throws Exception { // mask an anon SpanQuery class that rewrites to something else. SpanQuery q = new FieldMaskingSpanQuery (new SpanTermQuery(new Term("last", "sally")) { @Override public Query rewrite(IndexReader reader) { return new SpanOrQuery(new SpanTermQuery(new Term("first", "sally")), new SpanTermQuery(new Term("first", "james"))); } }, "first"); SpanQuery qr = (SpanQuery) searcher.rewrite(q); QueryUtils.checkUnequal(q, qr); Set<Term> terms = new HashSet<>(); qr.extractTerms(terms); assertEquals(2, terms.size()); }
public void testEquality1() { SpanQuery q1 = new FieldMaskingSpanQuery (new SpanTermQuery(new Term("last", "sally")) , "first"); SpanQuery q2 = new FieldMaskingSpanQuery (new SpanTermQuery(new Term("last", "sally")) , "first"); SpanQuery q3 = new FieldMaskingSpanQuery (new SpanTermQuery(new Term("last", "sally")) , "XXXXX"); SpanQuery q4 = new FieldMaskingSpanQuery (new SpanTermQuery(new Term("last", "XXXXX")) , "first"); SpanQuery q5 = new FieldMaskingSpanQuery (new SpanTermQuery(new Term("xXXX", "sally")) , "first"); QueryUtils.checkEqual(q1, q2); QueryUtils.checkUnequal(q1, q3); QueryUtils.checkUnequal(q1, q4); QueryUtils.checkUnequal(q1, q5); SpanQuery qA = new FieldMaskingSpanQuery (new SpanTermQuery(new Term("last", "sally")) , "first"); qA.setBoost(9f); SpanQuery qB = new FieldMaskingSpanQuery (new SpanTermQuery(new Term("last", "sally")) , "first"); QueryUtils.checkUnequal(qA, qB); qB.setBoost(9f); QueryUtils.checkEqual(qA, qB); }
public void testAverageFunction() throws IOException { PayloadNearQuery query; TopDocs hits; query = newPhraseQuery("field", "twenty two", true, new AveragePayloadFunction()); QueryUtils.check(query); // all 10 hits should have score = 3 because adjacent terms have payloads of 2,4 // and all the similarity factors are set to 1 hits = searcher.search(query, null, 100); assertTrue("hits is null and it shouldn't be", hits != null); assertTrue("should be 10 hits", hits.totalHits == 10); for (int j = 0; j < hits.scoreDocs.length; j++) { ScoreDoc doc = hits.scoreDocs[j]; assertTrue(doc.score + " does not equal: " + 3, doc.score == 3); Explanation explain = searcher.explain(query, hits.scoreDocs[j].doc); String exp = explain.toString(); assertTrue(exp, exp.indexOf("AveragePayloadFunction") > -1); assertTrue(hits.scoreDocs[j].score + " explain value does not equal: " + 3, explain.getValue() == 3f); } }
public void testMaxFunction() throws IOException { PayloadNearQuery query; TopDocs hits; query = newPhraseQuery("field", "twenty two", true, new MaxPayloadFunction()); QueryUtils.check(query); // all 10 hits should have score = 4 (max payload value) hits = searcher.search(query, null, 100); assertTrue("hits is null and it shouldn't be", hits != null); assertTrue("should be 10 hits", hits.totalHits == 10); for (int j = 0; j < hits.scoreDocs.length; j++) { ScoreDoc doc = hits.scoreDocs[j]; assertTrue(doc.score + " does not equal: " + 4, doc.score == 4); Explanation explain = searcher.explain(query, hits.scoreDocs[j].doc); String exp = explain.toString(); assertTrue(exp, exp.indexOf("MaxPayloadFunction") > -1); assertTrue(hits.scoreDocs[j].score + " explain value does not equal: " + 4, explain.getValue() == 4f); } }
public void testMinFunction() throws IOException { PayloadNearQuery query; TopDocs hits; query = newPhraseQuery("field", "twenty two", true, new MinPayloadFunction()); QueryUtils.check(query); // all 10 hits should have score = 2 (min payload value) hits = searcher.search(query, null, 100); assertTrue("hits is null and it shouldn't be", hits != null); assertTrue("should be 10 hits", hits.totalHits == 10); for (int j = 0; j < hits.scoreDocs.length; j++) { ScoreDoc doc = hits.scoreDocs[j]; assertTrue(doc.score + " does not equal: " + 2, doc.score == 2); Explanation explain = searcher.explain(query, hits.scoreDocs[j].doc); String exp = explain.toString(); assertTrue(exp, exp.indexOf("MinPayloadFunction") > -1); assertTrue(hits.scoreDocs[j].score + " explain value does not equal: " + 2, explain.getValue() == 2f); } }
void tryQuerySameTypes(String template, String numbers, String type) throws SyntaxError { String s1 = template; String s2 = template; String s3 = template; String[] numParts = numbers.split(","); String type2 = type.replace("val1", "val2"); for (int idx = 0; s1.contains("#"); ++idx) { String patV = "#v" + Integer.toString(idx); String patN = "#n" + Integer.toString(idx); s1 = s1.replace(patV, type).replace(patN, numParts[idx]); s2 = s2.replace(patV, type).replace(patN, numParts[idx]); s3 = s3.replace(patV, type2).replace(patN, numParts[idx]); } //SolrQueryRequest req1 = req( "q","*:*", "fq", s1); Query q1 = getQuery(s1); Query q2 = getQuery(s2); Query q3 = getQuery(s3); QueryUtils.checkEqual(q1, q2); QueryUtils.checkUnequal(q1, q3); }
private void doTestRank (ValueSource valueSource) throws Exception { FunctionQuery functionQuery = new FunctionQuery(valueSource); IndexReader r = DirectoryReader.open(dir); IndexSearcher s = new IndexSearcher(r); log("test: "+ functionQuery); QueryUtils.check(random(), functionQuery,s); ScoreDoc[] h = s.search(functionQuery, null, 1000).scoreDocs; assertEquals("All docs should be matched!",N_DOCS,h.length); String prevID = "ID"+(N_DOCS+1); // greater than all ids of docs in this test for (int i=0; i<h.length; i++) { String resID = s.doc(h[i].doc).get(ID_FIELD); log(i+". score="+h[i].score+" - "+resID); log(s.explain(functionQuery,h[i].doc)); assertTrue("res id "+resID+" should be < prev res id "+prevID, resID.compareTo(prevID)<0); prevID = resID; } r.close(); }
public void testRewrite1() throws Exception { // mask an anon SpanQuery class that rewrites to something else. SpanQuery q = new FieldMaskingSpanQuery (new SpanTermQuery(new Term("last", "sally")) { @Override public Query rewrite(IndexReader reader) { return new SpanOrQuery(new SpanTermQuery(new Term("first", "sally")), new SpanTermQuery(new Term("first", "james"))); } }, "first"); SpanQuery qr = (SpanQuery) searcher.rewrite(q); QueryUtils.checkUnequal(q, qr); Set<Term> terms = new HashSet<Term>(); qr.extractTerms(terms); assertEquals(2, terms.size()); }
public void testBasics() { MinDocQuery query1 = new MinDocQuery(42); MinDocQuery query2 = new MinDocQuery(42); MinDocQuery query3 = new MinDocQuery(43); QueryUtils.check(query1); QueryUtils.checkEqual(query1, query2); QueryUtils.checkUnequal(query1, query3); }
public void testBasics() { TermsSliceQuery query1 = new TermsSliceQuery("field1", 1, 10); TermsSliceQuery query2 = new TermsSliceQuery("field1", 1, 10); TermsSliceQuery query3 = new TermsSliceQuery("field2", 1, 10); TermsSliceQuery query4 = new TermsSliceQuery("field1", 2, 10); QueryUtils.check(query1); QueryUtils.checkEqual(query1, query2); QueryUtils.checkUnequal(query1, query3); QueryUtils.checkUnequal(query1, query4); }
public void testBasics() { DocValuesSliceQuery query1 = new DocValuesSliceQuery("field1", 1, 10); DocValuesSliceQuery query2 = new DocValuesSliceQuery("field1", 1, 10); DocValuesSliceQuery query3 = new DocValuesSliceQuery("field2", 1, 10); DocValuesSliceQuery query4 = new DocValuesSliceQuery("field1", 2, 10); QueryUtils.check(query1); QueryUtils.checkEqual(query1, query2); QueryUtils.checkUnequal(query1, query3); QueryUtils.checkUnequal(query1, query4); }
public void testBoostingQueryEquals() { TermQuery q1 = new TermQuery(new Term("subject:", "java")); TermQuery q2 = new TermQuery(new Term("subject:", "java")); assertEquals("Two TermQueries with same attributes should be equal", q1, q2); BoostingQuery bq1 = new BoostingQuery(q1, q2, 0.1f); QueryUtils.check(bq1); BoostingQuery bq2 = new BoostingQuery(q1, q2, 0.1f); assertEquals("BoostingQuery with same attributes is not equal", bq1, bq2); }
public void testEqualsHashCode() { CommonTermsQuery query = new CommonTermsQuery(randomOccur(random()), randomOccur(random()), random().nextFloat(), random().nextBoolean()); int terms = atLeast(2); for (int i = 0; i < terms; i++) { query.add(new Term(TestUtil.randomRealisticUnicodeString(random()), TestUtil.randomRealisticUnicodeString(random()))); } QueryUtils.checkHashEquals(query); QueryUtils.checkUnequal(new CommonTermsQuery(randomOccur(random()), randomOccur(random()), random().nextFloat(), random().nextBoolean()), query); { final long seed = random().nextLong(); Random r = new Random(seed); CommonTermsQuery left = new CommonTermsQuery(randomOccur(r), randomOccur(r), r.nextFloat(), r.nextBoolean()); int leftTerms = atLeast(r, 2); for (int i = 0; i < leftTerms; i++) { left.add(new Term(TestUtil.randomRealisticUnicodeString(r), TestUtil .randomRealisticUnicodeString(r))); } left.setHighFreqMinimumNumberShouldMatch(r.nextInt(4)); left.setLowFreqMinimumNumberShouldMatch(r.nextInt(4)); r = new Random(seed); CommonTermsQuery right = new CommonTermsQuery(randomOccur(r), randomOccur(r), r.nextFloat(), r.nextBoolean()); int rightTerms = atLeast(r, 2); for (int i = 0; i < rightTerms; i++) { right.add(new Term(TestUtil.randomRealisticUnicodeString(r), TestUtil .randomRealisticUnicodeString(r))); } right.setHighFreqMinimumNumberShouldMatch(r.nextInt(4)); right.setLowFreqMinimumNumberShouldMatch(r.nextInt(4)); QueryUtils.checkEqual(left, right); } }
public void testQuery() throws IOException { IndexSearcher searcher = newSearcher(reader); // Making sure the query yields 25 documents with the facet "a" DrillDownQuery q = new DrillDownQuery(config); q.add("a"); QueryUtils.check(q); TopDocs docs = searcher.search(q, 100); assertEquals(25, docs.totalHits); // Making sure the query yields 5 documents with the facet "b" and the // previous (facet "a") query as a base query DrillDownQuery q2 = new DrillDownQuery(config, q); q2.add("b"); docs = searcher.search(q2, 100); assertEquals(5, docs.totalHits); // Making sure that a query of both facet "a" and facet "b" yields 5 results DrillDownQuery q3 = new DrillDownQuery(config); q3.add("a"); q3.add("b"); docs = searcher.search(q3, 100); assertEquals(5, docs.totalHits); // Check that content:foo (which yields 50% results) and facet/b (which yields 20%) // would gather together 10 results (10%..) Query fooQuery = new TermQuery(new Term("content", "foo")); DrillDownQuery q4 = new DrillDownQuery(config, fooQuery); q4.add("b"); docs = searcher.search(q4, 100); assertEquals(10, docs.totalHits); }
void checkEqualParsings(String s1, String s2) throws Exception { String fieldName = "foo"; BasicQueryFactory qf = new BasicQueryFactory(16); Query lq1, lq2; lq1 = QueryParser.parse(s1).makeLuceneQueryField(fieldName, qf); lq2 = QueryParser.parse(s2).makeLuceneQueryField(fieldName, qf); QueryUtils.checkEqual(lq1, lq2); }
public void testRewrite0() throws Exception { SpanQuery q = new FieldMaskingSpanQuery (new SpanTermQuery(new Term("last", "sally")) , "first"); q.setBoost(8.7654321f); SpanQuery qr = (SpanQuery) searcher.rewrite(q); QueryUtils.checkEqual(q, qr); Set<Term> terms = new HashSet<>(); qr.extractTerms(terms); assertEquals(1, terms.size()); }
public void testRewrite2() throws Exception { SpanQuery q1 = new SpanTermQuery(new Term("last", "smith")); SpanQuery q2 = new SpanTermQuery(new Term("last", "jones")); SpanQuery q = new SpanNearQuery(new SpanQuery[] { q1, new FieldMaskingSpanQuery(q2, "last")}, 1, true ); Query qr = searcher.rewrite(q); QueryUtils.checkEqual(q, qr); HashSet<Term> set = new HashSet<>(); qr.extractTerms(set); assertEquals(2, set.size()); }
public void testQuery() { PayloadTermQuery boostingFuncTermQuery = new PayloadTermQuery(new Term(PayloadHelper.MULTI_FIELD, "seventy"), new MaxPayloadFunction()); QueryUtils.check(boostingFuncTermQuery); SpanTermQuery spanTermQuery = new SpanTermQuery(new Term(PayloadHelper.MULTI_FIELD, "seventy")); assertTrue(boostingFuncTermQuery.equals(spanTermQuery) == spanTermQuery.equals(boostingFuncTermQuery)); PayloadTermQuery boostingFuncTermQuery2 = new PayloadTermQuery(new Term(PayloadHelper.MULTI_FIELD, "seventy"), new AveragePayloadFunction()); QueryUtils.checkUnequal(boostingFuncTermQuery, boostingFuncTermQuery2); }
@Test public void testGeodistSource() throws SyntaxError { Query q_home = getQuery("geodist(home_ll, 45.0, 43.0)"); Query q_work = getQuery("geodist(work_ll, 45.0, 43.0)"); Query q_home2 = getQuery("geodist(home_ll, 45.0, 43.0)"); QueryUtils.checkUnequal(q_work, q_home); QueryUtils.checkEqual(q_home, q_home2); }
void tryQueryDiffTypes(String template, String numbers, String[] types) throws SyntaxError { String s1 = template; String s2 = template; String[] numParts = numbers.split(","); for (int idx = 0; s1.contains("#"); ++idx) { String patV = "#v" + Integer.toString(idx); String patN = "#n" + Integer.toString(idx); s1 = s1.replace(patV, types[idx % types.length]).replace(patN, numParts[idx]); s2 = s2.replace(patV, types[(idx + 1) % types.length]).replace(patN, numParts[idx]); } Query q1 = getQuery(s1); Query q2 = getQuery(s2); QueryUtils.checkUnequal(q1, q2); }
/** * NOTE: defType is not only used to pick the parser, but, if non-null it is * also to record the parser being tested for coverage sanity checking * * @see QueryUtils#check * @see QueryUtils#checkEqual * @see #testParserCoverage */ protected void assertQueryEquals(final String defType, final SolrQueryRequest req, final String... inputs) throws Exception { if (null != defType) qParsersTested.add(defType); final Query[] queries = new Query[inputs.length]; try { SolrQueryResponse rsp = new SolrQueryResponse(); SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req,rsp)); for (int i = 0; i < inputs.length; i++) { queries[i] = (QParser.getParser(inputs[i], defType, req).getQuery()); } } finally { SolrRequestInfo.clearRequestInfo(); } for (int i = 0; i < queries.length; i++) { QueryUtils.check(queries[i]); // yes starting j=0 is redundent, we're making sure every query // is equal to itself, and that the quality checks work regardless // of which caller/callee is used. for (int j = 0; j < queries.length; j++) { QueryUtils.checkEqual(queries[i], queries[j]); } } }
public void testEqualsHashCode() { CommonTermsQuery query = new CommonTermsQuery(randomOccur(random()), randomOccur(random()), random().nextFloat(), random().nextBoolean()); int terms = atLeast(2); for (int i = 0; i < terms; i++) { query.add(new Term(_TestUtil.randomRealisticUnicodeString(random()), _TestUtil.randomRealisticUnicodeString(random()))); } QueryUtils.checkHashEquals(query); QueryUtils.checkUnequal(new CommonTermsQuery(randomOccur(random()), randomOccur(random()), random().nextFloat(), random().nextBoolean()), query); { final long seed = random().nextLong(); Random r = new Random(seed); CommonTermsQuery left = new CommonTermsQuery(randomOccur(r), randomOccur(r), r.nextFloat(), r.nextBoolean()); int leftTerms = atLeast(r, 2); for (int i = 0; i < leftTerms; i++) { left.add(new Term(_TestUtil.randomRealisticUnicodeString(r), _TestUtil .randomRealisticUnicodeString(r))); } r = new Random(seed); CommonTermsQuery right = new CommonTermsQuery(randomOccur(r), randomOccur(r), r.nextFloat(), r.nextBoolean()); int rightTerms = atLeast(r, 2); for (int i = 0; i < rightTerms; i++) { right.add(new Term(_TestUtil.randomRealisticUnicodeString(r), _TestUtil .randomRealisticUnicodeString(r))); } QueryUtils.checkEqual(left, right); } }