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 java.io.IOException
      Generic test that creates new query from the test query and checks both for equality and asserts equality on the two queries.
      Throws:
      java.io.IOException
    • shuffleProtectedFields

      protected java.lang.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 java.io.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:
      java.io.IOException
    • testUnknownObjectException

      public void testUnknownObjectException() throws java.io.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:
      java.io.IOException
    • getObjectsHoldingArbitraryContent

      protected java.util.Set<java.lang.String> getObjectsHoldingArbitraryContent()
      Returns a set of object names that won't trigger any exception (uncluding their children) when testing that unknown objects cause parse exceptions through testUnknownObjectException(). Default is an empty set. 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). In such cases no exception would get thrown.
    • 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 java.util.Map<java.lang.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​(java.lang.String queryAsString, org.elasticsearch.index.query.QueryBuilder expectedQuery) throws java.io.IOException
      Parses the query provided as string argument and compares it with the expected result provided as argument as a QueryBuilder
      Throws:
      java.io.IOException
    • parseQuery

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

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

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

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

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

      protected org.elasticsearch.index.query.QueryBuilder rewriteQuery​(QB queryBuilder, org.elasticsearch.index.query.QueryRewriteContext rewriteContext) throws java.io.IOException
      Throws:
      java.io.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 java.io.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:
      java.io.IOException
    • assertTermOrBoostQuery

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

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

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

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

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

      public void testEqualsAndHashcode()
    • mutateInstance

      public QB mutateInstance​(QB instance) throws java.io.IOException
      Throws:
      java.io.IOException
    • testValidOutput

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

      protected QB changeNameOrBoost​(QB original) throws java.io.IOException
      Throws:
      java.io.IOException
    • getRandomValueForFieldName

      protected static java.lang.Object getRandomValueForFieldName​(java.lang.String fieldName)
    • getRandomQueryText

      protected static java.lang.String getRandomQueryText()
    • getRandomFieldName

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

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

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

      protected static java.lang.String randomAnalyzer()
    • randomMinimumShouldMatch

      protected static java.lang.String randomMinimumShouldMatch()
    • checkGeneratedJson

      public static void checkGeneratedJson​(java.lang.String expected, org.elasticsearch.index.query.QueryBuilder source) throws java.io.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:
      java.io.IOException
    • testMustRewrite

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

      protected org.apache.lucene.search.Query rewrite​(org.apache.lucene.search.Query query) throws java.io.IOException
      Throws:
      java.io.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​(java.lang.String fieldName)
    • testCacheability

      public void testCacheability() throws java.io.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:
      java.io.IOException