forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor from static utils into abstract class and add support for us…
…ing search inference IDs from field
- Loading branch information
Showing
8 changed files
with
269 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 0 additions & 54 deletions
54
...c/main/java/org/elasticsearch/xpack/inference/queries/SemanticQueryInterceptionUtils.java
This file was deleted.
Oops, something went wrong.
154 changes: 154 additions & 0 deletions
154
.../main/java/org/elasticsearch/xpack/inference/queries/SemanticQueryRewriteInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.inference.queries; | ||
|
||
import org.elasticsearch.action.ResolvedIndices; | ||
import org.elasticsearch.cluster.metadata.IndexMetadata; | ||
import org.elasticsearch.cluster.metadata.InferenceFieldMetadata; | ||
import org.elasticsearch.index.mapper.IndexFieldMapper; | ||
import org.elasticsearch.index.query.BoolQueryBuilder; | ||
import org.elasticsearch.index.query.QueryBuilder; | ||
import org.elasticsearch.index.query.QueryRewriteContext; | ||
import org.elasticsearch.index.query.TermsQueryBuilder; | ||
import org.elasticsearch.plugins.internal.rewriter.QueryRewriteInterceptor; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Intercepts and adapts a query to be rewritten to work seamlessly on a semantic_text field. | ||
*/ | ||
public abstract class SemanticQueryRewriteInterceptor implements QueryRewriteInterceptor { | ||
|
||
public SemanticQueryRewriteInterceptor() {} | ||
|
||
@Override | ||
public QueryBuilder interceptAndRewrite(QueryRewriteContext context, QueryBuilder queryBuilder) { | ||
QueryBuilder rewritten = queryBuilder; | ||
String fieldName = getFieldName(queryBuilder); | ||
InferenceIndexInformationForField indexInformation = resolveIndicesForField(fieldName, context.getResolvedIndices()); | ||
|
||
if (indexInformation == null || indexInformation.getInferenceIndices().isEmpty()) { | ||
// No inference fields were identified, so return the original query. | ||
return rewritten; | ||
} else if (indexInformation.nonInferenceIndices().isEmpty() == false) { | ||
// Combined case where the field name requested by this query contains both | ||
// semantic_text and non-inference fields, so we have to combine queries per index | ||
// containing each field type. | ||
rewritten = buildCombinedInferenceAndNonInferenceQuery(queryBuilder, indexInformation); | ||
} else { | ||
// The only fields we've identified are inference fields (e.g. semantic_text), | ||
// so rewrite the entire query to work on a semantic_text field. | ||
rewritten = buildInferenceQuery(queryBuilder, indexInformation); | ||
} | ||
|
||
return rewritten; | ||
} | ||
|
||
/** | ||
* @param queryBuilder {@link QueryBuilder} | ||
* @return The singular field name requested by the provided query builder. | ||
*/ | ||
protected abstract String getFieldName(QueryBuilder queryBuilder); | ||
|
||
/** | ||
* @param queryBuilder {@link QueryBuilder} | ||
* @return The text/query string requested by the provided query builder. | ||
*/ | ||
protected abstract String getQuery(QueryBuilder queryBuilder); | ||
|
||
/** | ||
* Builds the inference query | ||
* | ||
* @param queryBuilder {@link QueryBuilder} | ||
* @param indexInformation {@link InferenceIndexInformationForField} | ||
* @return {@link QueryBuilder} | ||
*/ | ||
protected abstract QueryBuilder buildInferenceQuery(QueryBuilder queryBuilder, InferenceIndexInformationForField indexInformation); | ||
|
||
/** | ||
* Builds a combined inference and non-inference query, | ||
* which separates the different queries into appropriate indices based on field type. | ||
* @param queryBuilder {@link QueryBuilder} | ||
* @param indexInformation {@link InferenceIndexInformationForField} | ||
* @return {@link QueryBuilder} | ||
*/ | ||
protected abstract QueryBuilder buildCombinedInferenceAndNonInferenceQuery( | ||
QueryBuilder queryBuilder, | ||
InferenceIndexInformationForField indexInformation | ||
); | ||
|
||
private InferenceIndexInformationForField resolveIndicesForField(String fieldName, ResolvedIndices resolvedIndices) { | ||
if (resolvedIndices != null) { | ||
Collection<IndexMetadata> indexMetadataCollection = resolvedIndices.getConcreteLocalIndicesMetadata().values(); | ||
Map<String, InferenceFieldMetadata> inferenceIndicesMetadata = new HashMap<>(); | ||
List<String> nonInferenceIndices = new ArrayList<>(); | ||
for (IndexMetadata indexMetadata : indexMetadataCollection) { | ||
String indexName = indexMetadata.getIndex().getName(); | ||
InferenceFieldMetadata inferenceFieldMetadata = indexMetadata.getInferenceFields().get(fieldName); | ||
if (inferenceFieldMetadata != null) { | ||
inferenceIndicesMetadata.put(indexName, inferenceFieldMetadata); | ||
} else { | ||
nonInferenceIndices.add(indexName); | ||
} | ||
} | ||
|
||
return new InferenceIndexInformationForField(fieldName, inferenceIndicesMetadata, nonInferenceIndices); | ||
} | ||
return null; | ||
} | ||
|
||
protected QueryBuilder createSubQueryForIndices(Collection<String> indices, QueryBuilder queryBuilder) { | ||
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder(); | ||
boolQueryBuilder.must(queryBuilder); | ||
boolQueryBuilder.filter(new TermsQueryBuilder(IndexFieldMapper.NAME, indices)); | ||
return boolQueryBuilder; | ||
} | ||
|
||
protected QueryBuilder createSemanticSubQuery(Collection<String> indices, String fieldName, String value) { | ||
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder(); | ||
boolQueryBuilder.must(new SemanticQueryBuilder(fieldName, value, true)); | ||
boolQueryBuilder.filter(new TermsQueryBuilder(IndexFieldMapper.NAME, indices)); | ||
return boolQueryBuilder; | ||
} | ||
|
||
/** | ||
* Represents the indices and associated inference information for a field. | ||
*/ | ||
public record InferenceIndexInformationForField( | ||
String fieldName, | ||
Map<String, InferenceFieldMetadata> inferenceIndicesMetadata, | ||
List<String> nonInferenceIndices | ||
) { | ||
|
||
public Collection<String> getInferenceIndices() { | ||
return inferenceIndicesMetadata.keySet(); | ||
} | ||
|
||
public String getSearchInferenceIdForIndex(String index) { | ||
return inferenceIndicesMetadata.get(index).getSearchInferenceId(); | ||
} | ||
|
||
public String getSearchInferenceId() { | ||
List<String> searchInferenceIds = inferenceIndicesMetadata.values() | ||
.stream() | ||
.map(InferenceFieldMetadata::getSearchInferenceId) | ||
.distinct() | ||
.toList(); | ||
if (searchInferenceIds.size() > 1) { | ||
throw new IllegalStateException( | ||
"Conflicting searchInferenceIds for field [" + fieldName + "]: Found [" + searchInferenceIds + "]" | ||
); | ||
} | ||
return searchInferenceIds.getFirst(); | ||
} | ||
} | ||
} |
Oops, something went wrong.