|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.flink.connector.elasticsearch.table.search; |
| 20 | + |
| 21 | +import org.apache.flink.api.common.serialization.DeserializationSchema; |
| 22 | +import org.apache.flink.table.data.GenericRowData; |
| 23 | +import org.apache.flink.table.data.RowData; |
| 24 | +import org.apache.flink.table.data.utils.JoinedRowData; |
| 25 | +import org.apache.flink.table.functions.FunctionContext; |
| 26 | +import org.apache.flink.table.functions.VectorSearchFunction; |
| 27 | +import org.apache.flink.util.FlinkRuntimeException; |
| 28 | + |
| 29 | +import org.slf4j.Logger; |
| 30 | +import org.slf4j.LoggerFactory; |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | +import java.util.ArrayList; |
| 34 | +import java.util.Collection; |
| 35 | +import java.util.Collections; |
| 36 | + |
| 37 | +import static org.apache.flink.util.Preconditions.checkNotNull; |
| 38 | + |
| 39 | +/** |
| 40 | + * Base {@link VectorSearchFunction} implementation for Elasticsearch. Shared retry loop, result |
| 41 | + * decoding and null-source filtering live here; version-specific subclasses only need to provide |
| 42 | + * the client initialization and the search call. |
| 43 | + */ |
| 44 | +public abstract class AbstractElasticsearchVectorSearchFunction extends VectorSearchFunction { |
| 45 | + private static final Logger LOG = |
| 46 | + LoggerFactory.getLogger(AbstractElasticsearchVectorSearchFunction.class); |
| 47 | + private static final long serialVersionUID = 1L; |
| 48 | + |
| 49 | + protected final DeserializationSchema<RowData> deserializationSchema; |
| 50 | + protected final String index; |
| 51 | + protected final String searchColumn; |
| 52 | + protected final String[] producedNames; |
| 53 | + protected final int maxRetryTimes; |
| 54 | + |
| 55 | + protected AbstractElasticsearchVectorSearchFunction( |
| 56 | + DeserializationSchema<RowData> deserializationSchema, |
| 57 | + int maxRetryTimes, |
| 58 | + String index, |
| 59 | + String searchColumn, |
| 60 | + String[] producedNames) { |
| 61 | + this.deserializationSchema = |
| 62 | + checkNotNull(deserializationSchema, "No DeserializationSchema supplied."); |
| 63 | + this.producedNames = checkNotNull(producedNames, "No fieldNames supplied."); |
| 64 | + this.maxRetryTimes = maxRetryTimes; |
| 65 | + this.index = index; |
| 66 | + this.searchColumn = searchColumn; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void open(FunctionContext context) throws Exception { |
| 71 | + doOpen(context); |
| 72 | + deserializationSchema.open(null); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public Collection<RowData> vectorSearch(int topK, RowData features) throws IOException { |
| 77 | + for (int retry = 0; retry <= maxRetryTimes; retry++) { |
| 78 | + try { |
| 79 | + SearchResult[] results = doSearch(topK, features); |
| 80 | + if (results.length > 0) { |
| 81 | + ArrayList<RowData> rows = new ArrayList<>(results.length); |
| 82 | + for (SearchResult result : results) { |
| 83 | + if (result.source == null) { |
| 84 | + continue; |
| 85 | + } |
| 86 | + RowData row = parseSearchResult(result.source); |
| 87 | + if (row == null) { |
| 88 | + continue; |
| 89 | + } |
| 90 | + GenericRowData scoreData = new GenericRowData(1); |
| 91 | + scoreData.setField(0, result.score); |
| 92 | + rows.add(new JoinedRowData(row, scoreData)); |
| 93 | + } |
| 94 | + rows.trimToSize(); |
| 95 | + return rows; |
| 96 | + } |
| 97 | + } catch (IOException e) { |
| 98 | + LOG.error(String.format("Elasticsearch search error, retry times = %d", retry), e); |
| 99 | + if (retry >= maxRetryTimes) { |
| 100 | + throw new FlinkRuntimeException("Execution of Elasticsearch search failed.", e); |
| 101 | + } |
| 102 | + try { |
| 103 | + Thread.sleep(1000L * retry); |
| 104 | + } catch (InterruptedException e1) { |
| 105 | + LOG.warn( |
| 106 | + "Interrupted while waiting to retry failed elasticsearch search, aborting"); |
| 107 | + throw new FlinkRuntimeException(e1); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + return Collections.emptyList(); |
| 112 | + } |
| 113 | + |
| 114 | + /** Version-specific initialization (e.g., creating the underlying Elasticsearch client). */ |
| 115 | + protected abstract void doOpen(FunctionContext context) throws Exception; |
| 116 | + |
| 117 | + /** Execute a single vector search call and return raw results, excluding nothing. */ |
| 118 | + protected abstract SearchResult[] doSearch(int topK, RowData features) throws IOException; |
| 119 | + |
| 120 | + private RowData parseSearchResult(String result) { |
| 121 | + try { |
| 122 | + return deserializationSchema.deserialize(result.getBytes()); |
| 123 | + } catch (IOException e) { |
| 124 | + LOG.error("Deserialize search hit failed: " + e.getMessage()); |
| 125 | + return null; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /** One hit from Elasticsearch — raw JSON source plus score. */ |
| 130 | + protected static class SearchResult { |
| 131 | + final String source; |
| 132 | + final Double score; |
| 133 | + |
| 134 | + public SearchResult(String source, Double score) { |
| 135 | + this.source = source; |
| 136 | + this.score = score; |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments