if (condition.getInnerHits() != null) { try (JsonXContentParser parser = new JsonXContentParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, new JsonFactory().createParser(condition.getInnerHits()))) { ihb = InnerHitBuilder.fromXContent(parser); } catch (IOException e) { throw new IllegalArgumentException("couldn't parse inner_hits: " + e.getMessage(), e);
@Override protected void doBuild(SearchContext parentSearchContext, InnerHitsContext innerHitsContext) throws IOException { QueryShardContext queryShardContext = parentSearchContext.getQueryShardContext(); ObjectMapper nestedObjectMapper = queryShardContext.getObjectMapper(path); if (nestedObjectMapper == null) { if (innerHitBuilder.isIgnoreUnmapped() == false) { throw new IllegalStateException("[" + query.getName() + "] no mapping found for type [" + path + "]"); } else { return; } } String name = innerHitBuilder.getName() != null ? innerHitBuilder.getName() : nestedObjectMapper.fullPath(); ObjectMapper parentObjectMapper = queryShardContext.nestedScope().nextLevel(nestedObjectMapper); NestedInnerHitSubContext nestedInnerHits = new NestedInnerHitSubContext( name, parentSearchContext, parentObjectMapper, nestedObjectMapper ); setupInnerHitsContext(queryShardContext, nestedInnerHits); queryShardContext.nestedScope().previousLevel(); innerHitsContext.addInnerHitDefinition(nestedInnerHits); } }
public static InnerHitBuilder fromXContent(XContentParser parser) throws IOException { return PARSER.parse(parser, new InnerHitBuilder(), null); }
public final void build(SearchContext parentSearchContext, InnerHitsContext innerHitsContext) throws IOException { long innerResultWindow = innerHitBuilder.getFrom() + innerHitBuilder.getSize(); int maxInnerResultWindow = parentSearchContext.mapperService().getIndexSettings().getMaxInnerResultWindow(); if (innerResultWindow > maxInnerResultWindow) { throw new IllegalArgumentException( "Inner result window is too large, the inner hit definition's [" + innerHitBuilder.getName() + "]'s from + size must be less than or equal to: [" + maxInnerResultWindow + "] but was [" + innerResultWindow + "]. This limit can be set by changing the [" + IndexSettings.MAX_INNER_RESULT_WINDOW_SETTING.getKey() + "] index level setting." ); } doBuild(parentSearchContext, innerHitsContext); }
private SearchSourceBuilder buildExpandSearchSourceBuilder(InnerHitBuilder options, CollapseBuilder innerCollapseBuilder) { SearchSourceBuilder groupSource = new SearchSourceBuilder(); groupSource.from(options.getFrom()); groupSource.size(options.getSize()); if (options.getSorts() != null) { options.getSorts().forEach(groupSource::sort); if (options.getFetchSourceContext() != null) { if (options.getFetchSourceContext().includes() == null && options.getFetchSourceContext().excludes() == null) { groupSource.fetchSource(options.getFetchSourceContext().fetchSource()); } else { groupSource.fetchSource(options.getFetchSourceContext().includes(), options.getFetchSourceContext().excludes()); if (options.getDocValueFields() != null) { options.getDocValueFields().forEach(ff -> groupSource.docValueField(ff.field, ff.format)); if (options.getStoredFieldsContext() != null && options.getStoredFieldsContext().fieldNames() != null) { options.getStoredFieldsContext().fieldNames().forEach(groupSource::storedField); if (options.getScriptFields() != null) { for (SearchSourceBuilder.ScriptField field : options.getScriptFields()) { groupSource.scriptField(field.fieldName(), field.script()); if (options.getHighlightBuilder() != null) { groupSource.highlighter(options.getHighlightBuilder()); groupSource.explain(options.isExplain()); groupSource.trackScores(options.isTrackScores()); groupSource.version(options.isVersion());
@Override public void extractInnerHitBuilders(Map<String, InnerHitContextBuilder> innerHits) { if (innerHitBuilder != null) { Map<String, InnerHitContextBuilder> children = new HashMap<>(); InnerHitContextBuilder.extractInnerHits(query, children); InnerHitContextBuilder innerHitContextBuilder = new NestedInnerHitContextBuilder(path, query, innerHitBuilder, children); String name = innerHitBuilder.getName() != null ? innerHitBuilder.getName() : path; innerHits.put(name, innerHitContextBuilder); } }
/** * Sets whether the query builder should ignore unmapped paths (and run a * {@link MatchNoDocsQuery} in place of this query) or throw an exception if * the path is unmapped. */ public NestedQueryBuilder ignoreUnmapped(boolean ignoreUnmapped) { this.ignoreUnmapped = ignoreUnmapped; if (innerHitBuilder != null) { innerHitBuilder.setIgnoreUnmapped(ignoreUnmapped); } return this; }
CollapseBuilder innerCollapseBuilder = innerHitBuilder.getInnerCollapseBuilder(); SearchSourceBuilder sourceBuilder = buildExpandSearchSourceBuilder(innerHitBuilder, innerCollapseBuilder) .query(groupQuery) hit.setInnerHits(new HashMap<>(innerHitBuilders.size())); hit.getInnerHits().put(innerHitBuilder.getName(), innerHits);
InnerHitBuilder ihb = new InnerHitBuilder(); ihb.setSize(maxURLsPerBucket); ihb.setName("urls_per_bucket"); bucketSortField).order(SortOrder.ASC); sorts.add(bucketsorter); ihb.setSorts(sorts);
@Override protected void doBuild(SearchContext parentSearchContext, InnerHitsContext innerHitsContext) throws IOException { QueryShardContext queryShardContext = parentSearchContext.getQueryShardContext(); ObjectMapper nestedObjectMapper = queryShardContext.getObjectMapper(path); if (nestedObjectMapper == null) { if (innerHitBuilder.isIgnoreUnmapped() == false) { throw new IllegalStateException("[" + query.getName() + "] no mapping found for type [" + path + "]"); } else { return; } } String name = innerHitBuilder.getName() != null ? innerHitBuilder.getName() : nestedObjectMapper.fullPath(); ObjectMapper parentObjectMapper = queryShardContext.nestedScope().nextLevel(nestedObjectMapper); NestedInnerHitSubContext nestedInnerHits = new NestedInnerHitSubContext( name, parentSearchContext, parentObjectMapper, nestedObjectMapper ); setupInnerHitsContext(queryShardContext, nestedInnerHits); queryShardContext.nestedScope().previousLevel(); innerHitsContext.addInnerHitDefinition(nestedInnerHits); } }
protected void setupInnerHitsContext(QueryShardContext queryShardContext, InnerHitsContext.InnerHitSubContext innerHitsContext) throws IOException { innerHitsContext.from(innerHitBuilder.getFrom()); innerHitsContext.size(innerHitBuilder.getSize()); innerHitsContext.explain(innerHitBuilder.isExplain()); innerHitsContext.version(innerHitBuilder.isVersion()); innerHitsContext.trackScores(innerHitBuilder.isTrackScores()); if (innerHitBuilder.getStoredFieldsContext() != null) { innerHitsContext.storedFieldsContext(innerHitBuilder.getStoredFieldsContext()); if (innerHitBuilder.getDocValueFields() != null) { innerHitsContext.docValueFieldsContext(new DocValueFieldsContext(innerHitBuilder.getDocValueFields())); if (innerHitBuilder.getScriptFields() != null) { for (SearchSourceBuilder.ScriptField field : innerHitBuilder.getScriptFields()) { QueryShardContext innerContext = innerHitsContext.getQueryShardContext(); SearchScript.Factory factory = innerContext.getScriptService().compile(field.script(), SearchScript.CONTEXT); if (innerHitBuilder.getFetchSourceContext() != null) { innerHitsContext.fetchSourceContext(innerHitBuilder.getFetchSourceContext() ); if (innerHitBuilder.getSorts() != null) { Optional<SortAndFormats> optionalSort = SortBuilder.buildSort(innerHitBuilder.getSorts(), queryShardContext); if (optionalSort.isPresent()) { innerHitsContext.sort(optionalSort.get()); if (innerHitBuilder.getHighlightBuilder() != null) { innerHitsContext.highlight(innerHitBuilder.getHighlightBuilder().build(queryShardContext));
public final void build(SearchContext parentSearchContext, InnerHitsContext innerHitsContext) throws IOException { long innerResultWindow = innerHitBuilder.getFrom() + innerHitBuilder.getSize(); int maxInnerResultWindow = parentSearchContext.mapperService().getIndexSettings().getMaxInnerResultWindow(); if (innerResultWindow > maxInnerResultWindow) { throw new IllegalArgumentException( "Inner result window is too large, the inner hit definition's [" + innerHitBuilder.getName() + "]'s from + size must be less than or equal to: [" + maxInnerResultWindow + "] but was [" + innerResultWindow + "]. This limit can be set by changing the [" + IndexSettings.MAX_INNER_RESULT_WINDOW_SETTING.getKey() + "] index level setting." ); } doBuild(parentSearchContext, innerHitsContext); }
@Override protected void extractInnerHitBuilders(Map<String, InnerHitContextBuilder> innerHits) { if (innerHitBuilder != null) { Map<String, InnerHitContextBuilder> children = new HashMap<>(); InnerHitContextBuilder.extractInnerHits(query, children); String name = innerHitBuilder.getName() != null ? innerHitBuilder.getName() : type; InnerHitContextBuilder innerHitContextBuilder = new ParentChildInnerHitContextBuilder(type, true, query, innerHitBuilder, children); innerHits.put(name, innerHitContextBuilder); } } }
public NestedQueryBuilder innerHit(InnerHitBuilder innerHitBuilder) { this.innerHitBuilder = innerHitBuilder; innerHitBuilder.setIgnoreUnmapped(ignoreUnmapped); return this; }
CollapseBuilder innerCollapseBuilder = innerHitBuilder.getInnerCollapseBuilder(); SearchSourceBuilder sourceBuilder = buildExpandSearchSourceBuilder(innerHitBuilder, innerCollapseBuilder) .query(groupQuery) hit.setInnerHits(new HashMap<>(innerHitBuilders.size())); hit.getInnerHits().put(innerHitBuilder.getName(), innerHits);
private SearchSourceBuilder buildExpandSearchSourceBuilder(InnerHitBuilder options) { SearchSourceBuilder groupSource = new SearchSourceBuilder(); groupSource.from(options.getFrom()); groupSource.size(options.getSize()); if (options.getSorts() != null) { options.getSorts().forEach(groupSource::sort); if (options.getFetchSourceContext() != null) { if (options.getFetchSourceContext().includes() == null && options.getFetchSourceContext().excludes() == null) { groupSource.fetchSource(options.getFetchSourceContext().fetchSource()); } else { groupSource.fetchSource(options.getFetchSourceContext().includes(), options.getFetchSourceContext().excludes()); if (options.getDocValueFields() != null) { options.getDocValueFields().forEach(groupSource::docValueField); if (options.getStoredFieldsContext() != null && options.getStoredFieldsContext().fieldNames() != null) { options.getStoredFieldsContext().fieldNames().forEach(groupSource::storedField); if (options.getScriptFields() != null) { for (SearchSourceBuilder.ScriptField field : options.getScriptFields()) { groupSource.scriptField(field.fieldName(), field.script()); if (options.getHighlightBuilder() != null) { groupSource.highlighter(options.getHighlightBuilder()); groupSource.explain(options.isExplain()); groupSource.trackScores(options.isTrackScores()); return groupSource;
private void handleJoinFieldInnerHits(SearchContext context, InnerHitsContext innerHitsContext) throws IOException { QueryShardContext queryShardContext = context.getQueryShardContext(); ParentJoinFieldMapper joinFieldMapper = ParentJoinFieldMapper.getMapper(context.mapperService()); if (joinFieldMapper != null) { String name = innerHitBuilder.getName() != null ? innerHitBuilder.getName() : typeName; JoinFieldInnerHitSubContext joinFieldInnerHits = new JoinFieldInnerHitSubContext(name, context, typeName, fetchChildInnerHits, joinFieldMapper); setupInnerHitsContext(queryShardContext, joinFieldInnerHits); innerHitsContext.addInnerHitDefinition(joinFieldInnerHits); } else { if (innerHitBuilder.isIgnoreUnmapped() == false) { throw new IllegalStateException("no join field has been configured"); } } }
query = parseInnerQueryBuilder(parser); } else if (INNER_HITS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { innerHitBuilder = InnerHitBuilder.fromXContent(parser); } else { throw new ParsingException(parser.getTokenLocation(), "[nested] query does not support [" + currentFieldName + "]");
public final void build(SearchContext parentSearchContext, InnerHitsContext innerHitsContext) throws IOException { long innerResultWindow = innerHitBuilder.getFrom() + innerHitBuilder.getSize(); int maxInnerResultWindow = parentSearchContext.mapperService().getIndexSettings().getMaxInnerResultWindow(); if (innerResultWindow > maxInnerResultWindow) { throw new IllegalArgumentException( "Inner result window is too large, the inner hit definition's [" + innerHitBuilder.getName() + "]'s from + size must be less than or equal to: [" + maxInnerResultWindow + "] but was [" + innerResultWindow + "]. This limit can be set by changing the [" + IndexSettings.MAX_INNER_RESULT_WINDOW_SETTING.getKey() + "] index level setting." ); } doBuild(parentSearchContext, innerHitsContext); }
@Override protected void extractInnerHitBuilders(Map<String, InnerHitContextBuilder> innerHits) { if (innerHitBuilder != null) { Map<String, InnerHitContextBuilder> children = new HashMap<>(); InnerHitContextBuilder.extractInnerHits(query, children); String name = innerHitBuilder.getName() != null ? innerHitBuilder.getName() : type; InnerHitContextBuilder innerHitContextBuilder = new ParentChildInnerHitContextBuilder(type, false, query, innerHitBuilder, children); innerHits.put(name, innerHitContextBuilder); } }