/* * SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to * this file be licensed under the Apache-2.0 license or a * compatible open source license. */ /* * Licensed to Elasticsearch under one or more contributor * license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright * ownership. Elasticsearch licenses this file to you under * the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ /* * Modifications Copyright OpenSearch Contributors. See * GitHub history for details. */ package org.opensearch.join.query; import org.apache.lucene.search.MatchNoDocsQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.join.ScoreMode; import org.opensearch.OpenSearchException; import org.opensearch.core.ParseField; import org.opensearch.core.common.ParsingException; import org.opensearch.core.common.io.stream.StreamInput; import org.opensearch.core.common.io.stream.StreamOutput; import org.opensearch.common.lucene.search.Queries; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.core.xcontent.XContentParser; import org.opensearch.index.fielddata.plain.SortedSetOrdinalsIndexFieldData; import org.opensearch.index.mapper.MappedFieldType; import org.opensearch.index.query.AbstractQueryBuilder; import org.opensearch.index.query.InnerHitBuilder; import org.opensearch.index.query.InnerHitContextBuilder; import org.opensearch.index.query.QueryBuilder; import org.opensearch.index.query.QueryRewriteContext; import org.opensearch.index.query.QueryShardContext; import org.opensearch.index.query.QueryShardException; import org.opensearch.join.mapper.ParentIdFieldMapper; import org.opensearch.join.mapper.ParentJoinFieldMapper; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Objects; import static org.opensearch.search.SearchService.ALLOW_EXPENSIVE_QUERIES; /** * Builder for the 'has_parent' query. */ public class HasParentQueryBuilder extends AbstractQueryBuilder { public static final String NAME = "has_parent"; /** * The default value for ignore_unmapped. */ public static final boolean DEFAULT_IGNORE_UNMAPPED = false; private static final ParseField QUERY_FIELD = new ParseField("query"); private static final ParseField PARENT_TYPE_FIELD = new ParseField("parent_type"); private static final ParseField SCORE_FIELD = new ParseField("score"); private static final ParseField INNER_HITS_FIELD = new ParseField("inner_hits"); private static final ParseField IGNORE_UNMAPPED_FIELD = new ParseField("ignore_unmapped"); private final QueryBuilder query; private final String type; private final boolean score; private InnerHitBuilder innerHitBuilder; private boolean ignoreUnmapped = false; public HasParentQueryBuilder(String type, QueryBuilder query, boolean score) { this(type, query, score, null); } private HasParentQueryBuilder(String type, QueryBuilder query, boolean score, InnerHitBuilder innerHitBuilder) { this.type = requireValue(type, "[" + NAME + "] requires '" + PARENT_TYPE_FIELD.getPreferredName() + "' field"); this.query = requireValue(query, "[" + NAME + "] requires '" + QUERY_FIELD.getPreferredName() + "' field"); this.score = score; this.innerHitBuilder = innerHitBuilder; } /** * Read from a stream. */ public HasParentQueryBuilder(StreamInput in) throws IOException { super(in); type = in.readString(); score = in.readBoolean(); query = in.readNamedWriteable(QueryBuilder.class); innerHitBuilder = in.readOptionalWriteable(InnerHitBuilder::new); ignoreUnmapped = in.readBoolean(); } @Override protected void doWriteTo(StreamOutput out) throws IOException { out.writeString(type); out.writeBoolean(score); out.writeNamedWriteable(query); out.writeOptionalWriteable(innerHitBuilder); out.writeBoolean(ignoreUnmapped); } /** * Returns the query to execute. */ public QueryBuilder query() { return query; } /** * Returns true if the parent score is mapped into the child documents */ public boolean score() { return score; } /** * Returns the parents type name */ public String type() { return type; } /** * Returns inner hit definition in the scope of this query and reusing the defined type and query. */ public InnerHitBuilder innerHit() { return innerHitBuilder; } public HasParentQueryBuilder innerHit(InnerHitBuilder innerHit) { this.innerHitBuilder = innerHit; innerHitBuilder.setIgnoreUnmapped(ignoreUnmapped); return this; } /** * Sets whether the query builder should ignore unmapped types (and run a * {@link MatchNoDocsQuery} in place of this query) or throw an exception if * the type is unmapped. */ public HasParentQueryBuilder ignoreUnmapped(boolean ignoreUnmapped) { this.ignoreUnmapped = ignoreUnmapped; if (innerHitBuilder != null) { innerHitBuilder.setIgnoreUnmapped(ignoreUnmapped); } return this; } /** * Gets whether the query builder will ignore unmapped types (and run a * {@link MatchNoDocsQuery} in place of this query) or throw an exception if * the type is unmapped. */ public boolean ignoreUnmapped() { return ignoreUnmapped; } @Override protected Query doToQuery(QueryShardContext context) throws IOException { if (context.allowExpensiveQueries() == false) { throw new OpenSearchException( "[joining] queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false." ); } ParentJoinFieldMapper joinFieldMapper = ParentJoinFieldMapper.getMapper(context.getMapperService()); if (joinFieldMapper == null) { if (ignoreUnmapped) { return new MatchNoDocsQuery(); } else { throw new QueryShardException(context, "[" + NAME + "] no join field has been configured"); } } ParentIdFieldMapper parentIdFieldMapper = joinFieldMapper.getParentIdFieldMapper(type, true); if (parentIdFieldMapper != null) { Query parentFilter = parentIdFieldMapper.getParentFilter(); Query innerQuery = Queries.filtered(query.toQuery(context), parentFilter); Query childFilter = parentIdFieldMapper.getChildrenFilter(); MappedFieldType fieldType = parentIdFieldMapper.fieldType(); final SortedSetOrdinalsIndexFieldData fieldData = context.getForField(fieldType); return new HasChildQueryBuilder.LateParsingQuery( childFilter, innerQuery, HasChildQueryBuilder.DEFAULT_MIN_CHILDREN, HasChildQueryBuilder.DEFAULT_MAX_CHILDREN, fieldType.name(), score ? ScoreMode.Max : ScoreMode.None, fieldData, context.getSearchSimilarity() ); } else { if (ignoreUnmapped) { return new MatchNoDocsQuery(); } else { throw new QueryShardException( context, "[" + NAME + "] join field [" + joinFieldMapper.name() + "] doesn't hold [" + type + "] as a parent" ); } } } @Override protected void doXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(NAME); builder.field(QUERY_FIELD.getPreferredName()); query.toXContent(builder, params); builder.field(PARENT_TYPE_FIELD.getPreferredName(), type); builder.field(SCORE_FIELD.getPreferredName(), score); builder.field(IGNORE_UNMAPPED_FIELD.getPreferredName(), ignoreUnmapped); printBoostAndQueryName(builder); if (innerHitBuilder != null) { builder.field(INNER_HITS_FIELD.getPreferredName(), innerHitBuilder, params); } builder.endObject(); } public static HasParentQueryBuilder fromXContent(XContentParser parser) throws IOException { float boost = AbstractQueryBuilder.DEFAULT_BOOST; String parentType = null; boolean score = false; String queryName = null; InnerHitBuilder innerHits = null; boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED; String currentFieldName = null; XContentParser.Token token; QueryBuilder iqb = null; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token == XContentParser.Token.START_OBJECT) { if (QUERY_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { iqb = parseInnerQueryBuilder(parser); } else if (INNER_HITS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { innerHits = InnerHitBuilder.fromXContent(parser); } else { throw new ParsingException(parser.getTokenLocation(), "[has_parent] query does not support [" + currentFieldName + "]"); } } else if (token.isValue()) { if (PARENT_TYPE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { parentType = parser.text(); } else if (SCORE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { score = parser.booleanValue(); } else if (IGNORE_UNMAPPED_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { ignoreUnmapped = parser.booleanValue(); } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { boost = parser.floatValue(); } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { queryName = parser.text(); } else { throw new ParsingException(parser.getTokenLocation(), "[has_parent] query does not support [" + currentFieldName + "]"); } } } HasParentQueryBuilder queryBuilder = new HasParentQueryBuilder(parentType, iqb, score).ignoreUnmapped(ignoreUnmapped) .queryName(queryName) .boost(boost); if (innerHits != null) { queryBuilder.innerHit(innerHits); } return queryBuilder; } @Override public String getWriteableName() { return NAME; } @Override protected boolean doEquals(HasParentQueryBuilder that) { return Objects.equals(query, that.query) && Objects.equals(type, that.type) && Objects.equals(score, that.score) && Objects.equals(innerHitBuilder, that.innerHitBuilder) && Objects.equals(ignoreUnmapped, that.ignoreUnmapped); } @Override protected int doHashCode() { return Objects.hash(query, type, score, innerHitBuilder, ignoreUnmapped); } @Override protected QueryBuilder doRewrite(QueryRewriteContext queryShardContext) throws IOException { QueryBuilder rewrittenQuery = query.rewrite(queryShardContext); if (rewrittenQuery != query) { HasParentQueryBuilder hasParentQueryBuilder = new HasParentQueryBuilder(type, rewrittenQuery, score, innerHitBuilder); hasParentQueryBuilder.ignoreUnmapped(ignoreUnmapped); return hasParentQueryBuilder; } return this; } @Override protected void extractInnerHitBuilders(Map innerHits) { if (innerHitBuilder != null) { String name = innerHitBuilder.getName() != null ? innerHitBuilder.getName() : type; if (innerHits.containsKey(name)) { throw new IllegalArgumentException("[inner_hits] already contains an entry for key [" + name + "]"); } Map children = new HashMap<>(); InnerHitContextBuilder.extractInnerHits(query, children); InnerHitContextBuilder innerHitContextBuilder = new ParentChildInnerHitContextBuilder( type, false, query, innerHitBuilder, children ); innerHits.put(name, innerHitContextBuilder); } } }