@Override protected void doAssertLuceneQuery( HasChildQueryBuilder queryBuilder, Query query, QueryShardContext context) throws IOException { QueryBuilder innerQueryBuilder = queryBuilder.query(); if (innerQueryBuilder instanceof EmptyQueryBuilder) { assertNull(query); } else { assertThat(query, instanceOf(HasChildQueryBuilder.LateParsingQuery.class)); HasChildQueryBuilder.LateParsingQuery lpq = (HasChildQueryBuilder.LateParsingQuery) query; assertEquals(queryBuilder.minChildren(), lpq.getMinChildren()); assertEquals(queryBuilder.maxChildren(), lpq.getMaxChildren()); assertEquals(queryBuilder.scoreMode(), lpq.getScoreMode()); // WTF is this why do we have two? } if (queryBuilder.innerHit() != null) { assertNotNull(SearchContext.current()); if (query != null) { assertNotNull(SearchContext.current().innerHits()); assertEquals(1, SearchContext.current().innerHits().getInnerHits().size()); assertTrue( SearchContext.current().innerHits().getInnerHits().containsKey("inner_hits_name")); InnerHitsContext.BaseInnerHits innerHits = SearchContext.current().innerHits().getInnerHits().get("inner_hits_name"); assertEquals(innerHits.size(), 100); assertEquals(innerHits.sort().getSort().length, 1); assertEquals(innerHits.sort().getSort()[0].getField(), STRING_FIELD_NAME); } else { assertNull(SearchContext.current().innerHits()); } } }
public void testToQueryInnerQueryType() throws IOException { String[] searchTypes = new String[] {PARENT_TYPE}; QueryShardContext.setTypes(searchTypes); HasChildQueryBuilder hasChildQueryBuilder = new HasChildQueryBuilder(CHILD_TYPE, new IdsQueryBuilder().addIds("id")); Query query = hasChildQueryBuilder.toQuery(createShardContext()); // verify that the context types are still the same as the ones we previously set assertThat(QueryShardContext.getTypes(), equalTo(searchTypes)); assertLateParsingQuery(query, CHILD_TYPE, "id"); }
public void testParseFromJSON() throws IOException { String query = copyToStringFromClasspath("/org/elasticsearch/index/query/has-child-with-inner-hits.json"); HasChildQueryBuilder queryBuilder = (HasChildQueryBuilder) parseQuery(query); assertEquals(query, queryBuilder.maxChildren(), 1217235442); assertEquals(query, queryBuilder.minChildren(), 883170873); assertEquals(query, queryBuilder.boost(), 2.0f, 0.0f); assertEquals(query, queryBuilder.queryName(), "WNzYMJKRwePuRBh"); assertEquals(query, queryBuilder.childType(), "child"); assertEquals(query, queryBuilder.scoreMode(), ScoreMode.Avg); assertNotNull(query, queryBuilder.innerHit()); assertEquals( query, queryBuilder.innerHit(), new QueryInnerHits( "inner_hits_name", new InnerHitsBuilder.InnerHit().setSize(100).addSort("mapped_string", SortOrder.ASC))); // now assert that we actually generate the same JSON XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint(); queryBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS); logger.info(msg(query, builder.string())); assertEquals(query, builder.string()); }
public void testIllegalValues() { QueryBuilder query = RandomQueryBuilder.createQuery(random()); try { new HasChildQueryBuilder(null, query); fail("must not be null"); } catch (IllegalArgumentException ex) { } try { new HasChildQueryBuilder("foo", null); fail("must not be null"); } catch (IllegalArgumentException ex) { } HasChildQueryBuilder foo = new HasChildQueryBuilder("foo", query); // all good try { foo.scoreMode(null); fail("must not be null"); } catch (IllegalArgumentException ex) { } final int positiveValue = randomIntBetween(0, Integer.MAX_VALUE); try { foo.minChildren(randomIntBetween(Integer.MIN_VALUE, -1)); fail("must not be negative"); } catch (IllegalArgumentException ex) { } foo.minChildren(positiveValue); assertEquals(positiveValue, foo.minChildren()); try { foo.maxChildren(randomIntBetween(Integer.MIN_VALUE, -1)); fail("must not be negative"); } catch (IllegalArgumentException ex) { } foo.maxChildren(positiveValue); assertEquals(positiveValue, foo.maxChildren()); }