|
5 | 5 | */
|
6 | 6 | package org.elasticsearch.xpack.enrich;
|
7 | 7 |
|
| 8 | +import org.elasticsearch.action.ActionListener; |
| 9 | +import org.elasticsearch.action.search.SearchRequest; |
| 10 | +import org.elasticsearch.action.search.SearchResponse; |
| 11 | +import org.elasticsearch.client.Client; |
| 12 | +import org.elasticsearch.cluster.routing.Preference; |
8 | 13 | import org.elasticsearch.ingest.AbstractProcessor;
|
| 14 | +import org.elasticsearch.ingest.IngestDocument; |
| 15 | +import org.elasticsearch.search.SearchHit; |
| 16 | +import org.elasticsearch.search.builder.SearchSourceBuilder; |
| 17 | +import org.elasticsearch.xpack.core.enrich.EnrichPolicy; |
| 18 | +import org.elasticsearch.xpack.enrich.action.EnrichCoordinatorProxyAction; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.function.BiConsumer; |
9 | 24 |
|
10 | 25 | public abstract class AbstractEnrichProcessor extends AbstractProcessor {
|
11 | 26 |
|
12 | 27 | private final String policyName;
|
| 28 | + private final BiConsumer<SearchRequest, BiConsumer<SearchResponse, Exception>> searchRunner; |
| 29 | + private final String field; |
| 30 | + private final String targetField; |
| 31 | + private final boolean ignoreMissing; |
| 32 | + private final boolean overrideEnabled; |
| 33 | + protected final String matchField; |
| 34 | + protected final int maxMatches; |
| 35 | + |
| 36 | + |
| 37 | + protected AbstractEnrichProcessor(String tag, Client client, String policyName, String field, String targetField, |
| 38 | + boolean ignoreMissing, boolean overrideEnabled, String matchField, int maxMatches) { |
| 39 | + this(tag, createSearchRunner(client), policyName, field, targetField, ignoreMissing, overrideEnabled, matchField, maxMatches); |
| 40 | + } |
13 | 41 |
|
14 |
| - protected AbstractEnrichProcessor(String tag, String policyName) { |
| 42 | + protected AbstractEnrichProcessor(String tag, |
| 43 | + BiConsumer<SearchRequest, BiConsumer<SearchResponse, Exception>> searchRunner, |
| 44 | + String policyName, String field, String targetField, boolean ignoreMissing, boolean overrideEnabled, |
| 45 | + String matchField, int maxMatches) { |
15 | 46 | super(tag);
|
16 | 47 | this.policyName = policyName;
|
| 48 | + this.searchRunner = searchRunner; |
| 49 | + this.field = field; |
| 50 | + this.targetField = targetField; |
| 51 | + this.ignoreMissing = ignoreMissing; |
| 52 | + this.overrideEnabled = overrideEnabled; |
| 53 | + this.matchField = matchField; |
| 54 | + this.maxMatches = maxMatches; |
| 55 | + } |
| 56 | + |
| 57 | + public abstract SearchSourceBuilder getSearchSourceBuilder(Object fieldValue); |
| 58 | + |
| 59 | + @Override |
| 60 | + public void execute(IngestDocument ingestDocument, BiConsumer<IngestDocument, Exception> handler) { |
| 61 | + try { |
| 62 | + // If a document does not have the enrich key, return the unchanged document |
| 63 | + final Object value = ingestDocument.getFieldValue(field, Object.class, ignoreMissing); |
| 64 | + if (value == null) { |
| 65 | + handler.accept(ingestDocument, null); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + SearchSourceBuilder searchSourceBuilder = getSearchSourceBuilder(value); |
| 70 | + |
| 71 | + SearchRequest req = new SearchRequest(); |
| 72 | + req.indices(EnrichPolicy.getBaseName(getPolicyName())); |
| 73 | + req.preference(Preference.LOCAL.type()); |
| 74 | + req.source(searchSourceBuilder); |
| 75 | + |
| 76 | + searchRunner.accept(req, (searchResponse, e) -> { |
| 77 | + if (e != null) { |
| 78 | + handler.accept(null, e); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + // If the index is empty, return the unchanged document |
| 83 | + // If the enrich key does not exist in the index, throw an error |
| 84 | + // If no documents match the key, return the unchanged document |
| 85 | + SearchHit[] searchHits = searchResponse.getHits().getHits(); |
| 86 | + if (searchHits.length < 1) { |
| 87 | + handler.accept(ingestDocument, null); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + if (overrideEnabled || ingestDocument.hasField(targetField) == false) { |
| 92 | + List<Map<String, Object>> enrichDocuments = new ArrayList<>(searchHits.length); |
| 93 | + for (SearchHit searchHit : searchHits) { |
| 94 | + Map<String, Object> enrichDocument = searchHit.getSourceAsMap(); |
| 95 | + enrichDocuments.add(enrichDocument); |
| 96 | + } |
| 97 | + ingestDocument.setFieldValue(targetField, enrichDocuments); |
| 98 | + } |
| 99 | + handler.accept(ingestDocument, null); |
| 100 | + }); |
| 101 | + } catch (Exception e) { |
| 102 | + handler.accept(null, e); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public IngestDocument execute(IngestDocument ingestDocument) throws Exception { |
| 108 | + throw new UnsupportedOperationException("this method should not get executed"); |
17 | 109 | }
|
18 | 110 |
|
19 | 111 | public String getPolicyName() {
|
20 | 112 | return policyName;
|
21 | 113 | }
|
| 114 | + |
| 115 | + @Override |
| 116 | + public String getType() { |
| 117 | + return EnrichProcessorFactory.TYPE; |
| 118 | + } |
| 119 | + |
| 120 | + String getField() { |
| 121 | + return field; |
| 122 | + } |
| 123 | + |
| 124 | + public String getTargetField() { |
| 125 | + return targetField; |
| 126 | + } |
| 127 | + |
| 128 | + boolean isIgnoreMissing() { |
| 129 | + return ignoreMissing; |
| 130 | + } |
| 131 | + |
| 132 | + boolean isOverrideEnabled() { |
| 133 | + return overrideEnabled; |
| 134 | + } |
| 135 | + |
| 136 | + public String getMatchField() { |
| 137 | + return matchField; |
| 138 | + } |
| 139 | + |
| 140 | + int getMaxMatches() { |
| 141 | + return maxMatches; |
| 142 | + } |
| 143 | + |
| 144 | + private static BiConsumer<SearchRequest, BiConsumer<SearchResponse, Exception>> createSearchRunner(Client client) { |
| 145 | + return (req, handler) -> { |
| 146 | + client.execute(EnrichCoordinatorProxyAction.INSTANCE, req, ActionListener.wrap( |
| 147 | + resp -> { |
| 148 | + handler.accept(resp, null); |
| 149 | + }, |
| 150 | + e -> { |
| 151 | + handler.accept(null, e); |
| 152 | + })); |
| 153 | + }; |
| 154 | + } |
22 | 155 | }
|
0 commit comments