From 7dcf0da0da1734c7cd84a84097c51c7cf45cea69 Mon Sep 17 00:00:00 2001 From: Isabel Drost-Fromm Date: Tue, 11 Aug 2015 22:00:25 +0200 Subject: [PATCH] Brings Lucene query assertions back to QB test. This brings the Lucene query assertions back to the querybuilder test that were removed in the ancient past when we tested Lucene queries through their inherent equals method. As we no longer do that it makes sense to do at least coarse sanity checking on the generated Lucene query. More such checks are being added as part of this commit. Relates to #10217 --- .../query/SimpleQueryStringBuilderTest.java | 130 ++++++++++++------ 1 file changed, 90 insertions(+), 40 deletions(-) diff --git a/core/src/test/java/org/elasticsearch/index/query/SimpleQueryStringBuilderTest.java b/core/src/test/java/org/elasticsearch/index/query/SimpleQueryStringBuilderTest.java index 69fa2888fa1..b5c3b14b30b 100644 --- a/core/src/test/java/org/elasticsearch/index/query/SimpleQueryStringBuilderTest.java +++ b/core/src/test/java/org/elasticsearch/index/query/SimpleQueryStringBuilderTest.java @@ -22,6 +22,7 @@ package org.elasticsearch.index.query; import org.apache.lucene.index.Term; import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanQuery; +import org.apache.lucene.search.MatchNoDocsQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.TermQuery; import org.elasticsearch.Version; @@ -88,38 +89,13 @@ public class SimpleQueryStringBuilderTest extends BaseQueryTestCase 1) { - assertThat(query, instanceOf(BooleanQuery.class)); - BooleanQuery booleanQuery = (BooleanQuery) query; - assertThat(booleanQuery.clauses().size(), equalTo(queryBuilder.fields().size())); - Iterator fields = queryBuilder.fields().keySet().iterator(); - for (BooleanClause booleanClause : booleanQuery) { - assertThat(booleanClause.getQuery(), instanceOf(TermQuery.class)); - TermQuery termQuery = (TermQuery) booleanClause.getQuery(); - assertThat(termQuery.getTerm(), equalTo(new Term(fields.next(), queryBuilder.value().toLowerCase(Locale.ROOT)))); - } - } else { - assertThat(query, instanceOf(TermQuery.class)); - String field; - if (queryBuilder.fields().size() == 0) { - field = MetaData.ALL; - } else { - field = queryBuilder.fields().keySet().iterator().next(); - } - TermQuery termQuery = (TermQuery) query; - assertThat(termQuery.getTerm(), equalTo(new Term(field, queryBuilder.value().toLowerCase(Locale.ROOT)))); - } - } - @Test public void testDefaults() { SimpleQueryStringBuilder qb = new SimpleQueryStringBuilder("The quick brown fox."); assertEquals("Wrong default default boost.", AbstractQueryBuilder.DEFAULT_BOOST, qb.boost(), 0.001); - assertEquals("Wrong default default boost field.", AbstractQueryBuilder.DEFAULT_BOOST, SimpleQueryStringBuilder.DEFAULT_BOOST, 0.001); + assertEquals("Wrong default default boost field.", AbstractQueryBuilder.DEFAULT_BOOST, SimpleQueryStringBuilder.DEFAULT_BOOST, + 0.001); assertEquals("Wrong default flags.", SimpleQueryStringFlag.ALL.value, qb.flags()); assertEquals("Wrong default flags field.", SimpleQueryStringFlag.ALL.value(), SimpleQueryStringBuilder.DEFAULT_FLAGS); @@ -134,7 +110,8 @@ public class SimpleQueryStringBuilderTest extends BaseQueryTestCase 1 && (!"".equals(queryBuilder.value()))) { + assertTrue("Query should have been BooleanQuery but was " + query.getClass().getName(), query instanceof BooleanQuery); + + BooleanQuery boolQuery = (BooleanQuery) query; + if (queryBuilder.lowercaseExpandedTerms()) { + for (BooleanClause clause : boolQuery.clauses()) { + if (clause.getQuery() instanceof TermQuery) { + TermQuery inner = (TermQuery) clause.getQuery(); + assertThat(inner.getTerm().bytes().toString(), is(inner.getTerm().bytes().toString().toLowerCase(Locale.ROOT))); + } + } + } + + assertThat(boolQuery.clauses().size(), equalTo(queryBuilder.fields().size())); + Iterator fields = queryBuilder.fields().keySet().iterator(); + for (BooleanClause booleanClause : boolQuery) { + assertThat(booleanClause.getQuery(), instanceOf(TermQuery.class)); + TermQuery termQuery = (TermQuery) booleanClause.getQuery(); + assertThat(termQuery.getTerm(), equalTo(new Term(fields.next(), queryBuilder.value().toLowerCase(Locale.ROOT)))); + } + + if (queryBuilder.minimumShouldMatch() != null) { + Collection minMatchAlways = Arrays.asList(new String[] { "1", "-1", "75%", "-25%" }); + Collection minMatchLarger = Arrays.asList(new String[] { "2<75%", "2<-25%" }); + + if (minMatchAlways.contains(queryBuilder.minimumShouldMatch())) { + assertThat(boolQuery.getMinimumNumberShouldMatch(), greaterThan(0)); + } else if (minMatchLarger.contains(queryBuilder.minimumShouldMatch())) { + if (shouldClauses(boolQuery) > 2) { + assertThat(boolQuery.getMinimumNumberShouldMatch(), greaterThan(0)); + } + } else { + assertEquals(0, boolQuery.getMinimumNumberShouldMatch()); + } + } + } else if (queryBuilder.fields().size() == 1 && (!"".equals(queryBuilder.value()))) { + assertTrue("Query should have been TermQuery but was " + query.getClass().getName(), query instanceof TermQuery); + + TermQuery termQuery = (TermQuery) query; + assertThat(termQuery.getTerm().field(), is(queryBuilder.fields().keySet().iterator().next())); + + String field; + if (queryBuilder.fields().size() == 0) { + field = MetaData.ALL; + } else { + field = queryBuilder.fields().keySet().iterator().next(); + } + assertThat(termQuery.getTerm(), equalTo(new Term(field, queryBuilder.value().toLowerCase(Locale.ROOT)))); + + if (queryBuilder.lowercaseExpandedTerms()) { + assertThat(termQuery.getTerm().bytes().toString(), is(termQuery.getTerm().bytes().toString().toLowerCase(Locale.ROOT))); + } + + } else if ("".equals(queryBuilder.value())) { + assertTrue("Query should have been MatchNoDocsQuery but was " + query.getClass().getName(), query instanceof MatchNoDocsQuery); + } else { + fail("Encountered lucene query type we do not have a validation implementation for in our SimpleQueryStringBuilderTest"); + } + } + + private int shouldClauses(BooleanQuery query) { int result = 0; for (BooleanClause c : query.clauses()) { if (c.getOccur() == BooleanClause.Occur.SHOULD) { @@ -277,4 +328,3 @@ public class SimpleQueryStringBuilderTest extends BaseQueryTestCase