Class AbstractQueryTestCase<QB extends org.elasticsearch.index.query.AbstractQueryBuilder<QB>>

java.lang.Object
org.junit.Assert
org.apache.lucene.util.LuceneTestCase

public abstract class AbstractQueryTestCase<QB extends org.elasticsearch.index.query.AbstractQueryBuilder<QB>> extends AbstractBuilderTestCase
  • Constructor Details

    • AbstractQueryTestCase

      public AbstractQueryTestCase()
  • Method Details

    • createTestQueryBuilder

      public final QB createTestQueryBuilder()
    • createTestQueryBuilder

      public final QB createTestQueryBuilder(boolean supportsBoost, boolean supportsQueryName)
    • doCreateTestQueryBuilder

      protected abstract QB doCreateTestQueryBuilder()
      Create the query that is being tested
    • testNegativeBoosts

      public void testNegativeBoosts()
    • testFromXContent

      public void testFromXContent() throws IOException
      Generic test that creates new query from the test query and checks both for equality and asserts equality on the two queries.
      Throws:
      IOException
    • shuffleProtectedFields

      protected String[] shuffleProtectedFields()
      Subclasses can override this method and return an array of fieldnames which should be protected from recursive random shuffling in the testFromXContent() test case
    • testUnknownField

      public void testUnknownField() throws IOException
      Test that unknown field trigger ParsingException. To find the right position in the root query, we add a marker as `queryName` which all query builders support. The added bogus field after that should trigger the exception. Queries that allow arbitrary field names at this level need to override this test.
      Throws:
      IOException
    • testUnknownObjectException

      public void testUnknownObjectException() throws IOException
      Test that adding an additional object within each object of the otherwise correct query always triggers some kind of parse exception. Some specific objects do not cause any exception as they can hold arbitrary content; they can be declared by overriding getObjectsHoldingArbitraryContent().
      Throws:
      IOException
    • getObjectsHoldingArbitraryContent

      protected Map<String,String> getObjectsHoldingArbitraryContent()
      Returns a map where the keys are object names that won't trigger a standard exception (an exception that contains the string "unknown field [newField]") through testUnknownObjectException(). The value is a string that is contained in the thrown exception or null in the case that no exception is thrown (including their children). Default is an empty Map. Can be overridden by subclasses that test queries which contain objects that get parsed on the data nodes (e.g. score functions) or objects that can contain arbitrary content (e.g. documents for percolate or more like this query, params for scripts) and/or expect some content(e.g documents with geojson geometries).
    • testQueryWrappedInArray

      public final void testQueryWrappedInArray()
      Test that wraps the randomly generated query into an array as follows: { "query_name" : [{}]} This causes unexpected situations in parser code that may not be handled properly.
    • getAlternateVersions

      protected Map<String,QB> getAlternateVersions()
      Returns alternate string representation of the query that need to be tested as they are never used as output of ToXContent.toXContent(XContentBuilder, ToXContent.Params). By default there are no alternate versions.
    • assertParsedQuery

      protected void assertParsedQuery(String queryAsString, org.elasticsearch.index.query.QueryBuilder expectedQuery) throws IOException
      Parses the query provided as string argument and compares it with the expected result provided as argument as a QueryBuilder
      Throws:
      IOException
    • parseQuery

      protected org.elasticsearch.index.query.QueryBuilder parseQuery(org.elasticsearch.index.query.AbstractQueryBuilder<?> builder) throws IOException
      Throws:
      IOException
    • parseQuery

      protected org.elasticsearch.index.query.QueryBuilder parseQuery(String queryAsString) throws IOException
      Throws:
      IOException
    • parseQuery

      protected org.elasticsearch.index.query.QueryBuilder parseQuery(org.elasticsearch.xcontent.XContentParser parser) throws IOException
      Throws:
      IOException
    • builderGeneratesCacheableQueries

      protected boolean builderGeneratesCacheableQueries()
      Whether the queries produced by this builder are expected to be cacheable.
    • testToQuery

      public void testToQuery() throws IOException
      Test creates the Query from the QueryBuilder under test and delegates the assertions being made on the result to the implementing subclass.
      Throws:
      IOException
    • rewriteQuery

      protected org.elasticsearch.index.query.QueryBuilder rewriteQuery(QB queryBuilder, org.elasticsearch.index.query.QueryRewriteContext rewriteContext) throws IOException
      Throws:
      IOException
    • supportsBoost

      protected boolean supportsBoost()
      Few queries allow you to set the boost on the Java API, although the corresponding parser doesn't parse it as it isn't supported. This method allows to disable boost related tests for those queries. Those queries are easy to identify: their parsers don't parse boost as they don't apply to the specific query: wrapper query and match_none.
    • supportsQueryName

      protected boolean supportsQueryName()
      Few queries allow you to set the query name on the Java API, although the corresponding parser doesn't parse it as it isn't supported. This method allows to disable query name related tests for those queries. Those queries are easy to identify: their parsers don't parse _name as they don't apply to the specific query: wrapper query and match_none.
    • doAssertLuceneQuery

      protected abstract void doAssertLuceneQuery(QB queryBuilder, org.apache.lucene.search.Query query, org.elasticsearch.index.query.SearchExecutionContext context) throws IOException
      Checks the result of QueryBuilder.toQuery(SearchExecutionContext) given the original QueryBuilder and SearchExecutionContext. Contains the query specific checks to be implemented by subclasses.
      Throws:
      IOException
    • assertTermOrBoostQuery

      protected void assertTermOrBoostQuery(org.apache.lucene.search.Query query, String field, String value, float fieldBoost)
    • assertTermQuery

      protected void assertTermQuery(org.apache.lucene.search.Query query, String field, String value)
    • testSerialization

      public void testSerialization() throws IOException
      Test serialization and deserialization of the test query.
      Throws:
      IOException
    • assertSerialization

      protected org.elasticsearch.index.query.QueryBuilder assertSerialization(org.elasticsearch.index.query.QueryBuilder testQuery) throws IOException
      Throws:
      IOException
    • assertSerialization

      protected org.elasticsearch.index.query.QueryBuilder assertSerialization(org.elasticsearch.index.query.QueryBuilder testQuery, org.elasticsearch.Version version) throws IOException
      Serialize the given query builder and asserts that both are equal
      Throws:
      IOException
    • testEqualsAndHashcode

      public void testEqualsAndHashcode()
    • mutateInstance

      public QB mutateInstance(QB instance) throws IOException
      Throws:
      IOException
    • testValidOutput

      public void testValidOutput() throws IOException
      Generic test that checks that the Strings.toString() method renders the XContent correctly.
      Throws:
      IOException
    • changeNameOrBoost

      protected QB changeNameOrBoost(QB original) throws IOException
      Throws:
      IOException
    • getRandomValueForFieldName

      protected static Object getRandomValueForFieldName(String fieldName)
    • getRandomQueryText

      protected static String getRandomQueryText()
    • getRandomFieldName

      protected static String getRandomFieldName()
      Helper method to return a mapped or a random field
    • getRandomRewriteMethod

      protected static String getRandomRewriteMethod()
      Helper method to return a random rewrite method
    • randomFuzziness

      protected static org.elasticsearch.common.unit.Fuzziness randomFuzziness(String fieldName)
    • randomAnalyzer

      protected static String randomAnalyzer()
    • randomMinimumShouldMatch

      protected static String randomMinimumShouldMatch()
    • checkGeneratedJson

      public static void checkGeneratedJson(String expected, org.elasticsearch.index.query.QueryBuilder source) throws IOException
      Call this method to check a valid json string representing the query under test against it's generated json. Note: By the time of this writing (Nov 2015) all queries are taken from the query dsl reference docs mirroring examples there. Here's how the queries were generated:
      • Take a reference documentation example.
      • Stick it into the createParseableQueryJson method of the respective query test.
      • Manually check that what the QueryBuilder generates equals the input json ignoring default options.
      • Put the manual checks into the assertQueryParsedFromJson method.
      • Now copy the generated json including default options into createParseableQueryJson
      • By now the roundtrip check for the json should be happy.
      Throws:
      IOException
    • testMustRewrite

      public void testMustRewrite() throws IOException
      This test ensures that queries that need to be rewritten have dedicated tests. These queries must override this method accordingly.
      Throws:
      IOException
    • rewrite

      protected org.apache.lucene.search.Query rewrite(org.apache.lucene.search.Query query) throws IOException
      Throws:
      IOException
    • rewriteAndFetch

      protected org.elasticsearch.index.query.QueryBuilder rewriteAndFetch(org.elasticsearch.index.query.QueryBuilder builder, org.elasticsearch.index.query.QueryRewriteContext context)
    • isTextField

      public boolean isTextField(String fieldName)
    • testCacheability

      public void testCacheability() throws IOException
      Check that a query is generally cacheable. Tests for query builders that are not always cacheable should overwrite this method and make sure the different cases are always tested
      Throws:
      IOException