1212import org .elasticsearch .cluster .metadata .InferenceFieldMetadata ;
1313import org .elasticsearch .index .mapper .IndexFieldMapper ;
1414import org .elasticsearch .index .query .BoolQueryBuilder ;
15- import org .elasticsearch .index .query .MatchAllQueryBuilder ;
1615import org .elasticsearch .index .query .QueryBuilder ;
1716import org .elasticsearch .index .query .QueryRewriteContext ;
1817import org .elasticsearch .index .query .TermsQueryBuilder ;
2322import java .util .HashMap ;
2423import java .util .List ;
2524import java .util .Map ;
26- import java .util .Set ;
2725import java .util .stream .Collectors ;
2826
2927/**
@@ -43,11 +41,6 @@ public QueryBuilder interceptAndRewrite(QueryRewriteContext context, QueryBuilde
4341 return queryBuilder ;
4442 }
4543
46- if (fieldName == null && getQueryName ().equals (MatchAllQueryBuilder .NAME )) {
47- return queryBuilder ;
48- // return handleMatchAllQuery(queryBuilder, resolvedIndices);
49- }
50-
5144 InferenceIndexInformationForField indexInformation = resolveIndicesForField (fieldName , resolvedIndices );
5245 if (indexInformation .getInferenceIndices ().isEmpty ()) {
5346 // No inference fields were identified, so return the original query.
@@ -64,30 +57,6 @@ public QueryBuilder interceptAndRewrite(QueryRewriteContext context, QueryBuilde
6457 }
6558 }
6659
67- private QueryBuilder handleMatchAllQuery (QueryBuilder queryBuilder , ResolvedIndices resolvedIndices ) {
68- if (getInferenceFieldsFromResolveIndices (resolvedIndices ).isEmpty ()) {
69- // No inference fields were identified, so return the original query.
70- return queryBuilder ;
71- }
72-
73- List <String > fieldList = new ArrayList <>(getFieldsFromResolveIndices (resolvedIndices ));
74- BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder ();
75- for (String field : fieldList ) {
76- InferenceIndexInformationForField indexInformation = resolveIndicesForField (field , resolvedIndices );
77- if (indexInformation .nonInferenceIndices ().isEmpty () == false ) {
78- // Combined case where the field name requested by this query contains both
79- // semantic_text and non-inference fields, so we have to combine queries per index
80- // containing each field type.
81- boolQueryBuilder .should (buildCombinedInferenceAndNonInferenceQuery (queryBuilder , indexInformation ));
82- } else {
83- // The only fields we've identified are inference fields (e.g. semantic_text),
84- // so rewrite this semantic_text field into a semantic query
85- boolQueryBuilder .should (buildInferenceQuery (queryBuilder , indexInformation ));
86- }
87- }
88- return boolQueryBuilder .should ().isEmpty () ? queryBuilder : boolQueryBuilder ;
89- }
90-
9160 /**
9261 * @param queryBuilder {@link QueryBuilder}
9362 * @return The singular field name requested by the provided query builder.
@@ -121,36 +90,6 @@ protected abstract QueryBuilder buildCombinedInferenceAndNonInferenceQuery(
12190 InferenceIndexInformationForField indexInformation
12291 );
12392
124- private List <String > getFieldsFromResolveIndices (ResolvedIndices resolvedIndices ) {
125- Collection <IndexMetadata > indexMetadataCollection = resolvedIndices .getConcreteLocalIndicesMetadata ().values ();
126- List <String > fields = new ArrayList <>();
127- for (IndexMetadata indexMetadata : indexMetadataCollection ) {
128- if (indexMetadata .mapping () == null || indexMetadata .mapping ().sourceAsMap () == null ) {
129- // No mapping, so no fields.
130- continue ;
131- }
132- Collection <Object > mappingSource = indexMetadata .mapping ().getSourceAsMap ().values ();
133- @ SuppressWarnings ("unchecked" )
134- Set <String > fieldNames = mappingSource .stream ()
135- .filter (obj -> obj instanceof Map <?, ?>)
136- .map (obj -> (Map <String , Object >) obj )
137- .flatMap (map -> map .keySet ().stream ())
138- .collect (Collectors .toSet ());
139- fields .addAll (fieldNames );
140- }
141- return fields ;
142- }
143-
144- private List <InferenceFieldMetadata > getInferenceFieldsFromResolveIndices (ResolvedIndices resolvedIndices ) {
145- Collection <IndexMetadata > indexMetadataCollection = resolvedIndices .getConcreteLocalIndicesMetadata ().values ();
146- List <InferenceFieldMetadata > inferenceIndicesMetadata = new ArrayList <>();
147- for (IndexMetadata indexMetadata : indexMetadataCollection ) {
148- inferenceIndicesMetadata .addAll (indexMetadata .getInferenceFields ().values ());
149- }
150-
151- return inferenceIndicesMetadata ;
152- }
153-
15493 private InferenceIndexInformationForField resolveIndicesForField (String fieldName , ResolvedIndices resolvedIndices ) {
15594 Collection <IndexMetadata > indexMetadataCollection = resolvedIndices .getConcreteLocalIndicesMetadata ().values ();
15695 Map <String , InferenceFieldMetadata > inferenceIndicesMetadata = new HashMap <>();
0 commit comments