|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | +package org.elasticsearch.xpack.enrich; |
| 7 | + |
| 8 | +import org.apache.lucene.document.Document; |
| 9 | +import org.apache.lucene.index.LeafReader; |
| 10 | +import org.apache.lucene.index.PostingsEnum; |
| 11 | +import org.apache.lucene.index.Terms; |
| 12 | +import org.apache.lucene.index.TermsEnum; |
| 13 | +import org.apache.lucene.util.BytesRef; |
| 14 | +import org.elasticsearch.common.bytes.BytesArray; |
| 15 | +import org.elasticsearch.common.bytes.BytesReference; |
| 16 | +import org.elasticsearch.common.xcontent.XContentHelper; |
| 17 | +import org.elasticsearch.common.xcontent.XContentType; |
| 18 | +import org.elasticsearch.index.engine.Engine; |
| 19 | +import org.elasticsearch.index.mapper.SourceFieldMapper; |
| 20 | +import org.elasticsearch.ingest.AbstractProcessor; |
| 21 | +import org.elasticsearch.ingest.IngestDocument; |
| 22 | +import org.elasticsearch.xpack.core.enrich.EnrichPolicy; |
| 23 | + |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Set; |
| 27 | +import java.util.function.Function; |
| 28 | + |
| 29 | +final class ExactMatchProcessor extends AbstractProcessor { |
| 30 | + |
| 31 | + private final Function<String, EnrichPolicy> policyLookup; |
| 32 | + private final Function<String, Engine.Searcher> searchProvider; |
| 33 | + |
| 34 | + private final String policyName; |
| 35 | + private final String enrichKey; |
| 36 | + private final boolean ignoreEnrichKeyMissing; |
| 37 | + private final List<EnrichProcessorFactory.EnrichSpecification> specifications; |
| 38 | + |
| 39 | + ExactMatchProcessor(String tag, |
| 40 | + Function<String, EnrichPolicy> policyLookup, |
| 41 | + Function<String, Engine.Searcher> searchProvider, String policyName, |
| 42 | + String enrichKey, |
| 43 | + boolean ignoreEnrichKeyMissing, |
| 44 | + List<EnrichProcessorFactory.EnrichSpecification> specifications) { |
| 45 | + super(tag); |
| 46 | + this.policyLookup = policyLookup; |
| 47 | + this.searchProvider = searchProvider; |
| 48 | + this.policyName = policyName; |
| 49 | + this.enrichKey = enrichKey; |
| 50 | + this.ignoreEnrichKeyMissing = ignoreEnrichKeyMissing; |
| 51 | + this.specifications = specifications; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public IngestDocument execute(IngestDocument ingestDocument) throws Exception { |
| 56 | + final EnrichPolicy policy = policyLookup.apply(policyName); |
| 57 | + if (policy == null) { |
| 58 | + throw new IllegalArgumentException("policy [" + policyName + "] does not exists"); |
| 59 | + } |
| 60 | + |
| 61 | + final String value = ingestDocument.getFieldValue(enrichKey, String.class, ignoreEnrichKeyMissing); |
| 62 | + if (value == null) { |
| 63 | + return ingestDocument; |
| 64 | + } |
| 65 | + |
| 66 | + // TODO: re-use the engine searcher between enriching documents from the same write request |
| 67 | + try (Engine.Searcher engineSearcher = searchProvider.apply(policy.getIndexPattern())) { |
| 68 | + if (engineSearcher.getDirectoryReader().leaves().size() == 0) { |
| 69 | + return ingestDocument; |
| 70 | + } else if (engineSearcher.getDirectoryReader().leaves().size() != 1) { |
| 71 | + throw new IllegalStateException("enrich index must have exactly a single segment"); |
| 72 | + } |
| 73 | + |
| 74 | + final LeafReader leafReader = engineSearcher.getDirectoryReader().leaves().get(0).reader(); |
| 75 | + final Terms terms = leafReader.terms(policy.getEnrichKey()); |
| 76 | + if (terms == null) { |
| 77 | + throw new IllegalStateException("enrich key field [" + policy.getEnrichKey() + "] does not exist"); |
| 78 | + } |
| 79 | + |
| 80 | + final TermsEnum tenum = terms.iterator(); |
| 81 | + if (tenum.seekExact(new BytesRef(value))) { |
| 82 | + PostingsEnum penum = tenum.postings(null, PostingsEnum.NONE); |
| 83 | + final int docId = penum.nextDoc(); |
| 84 | + assert docId != PostingsEnum.NO_MORE_DOCS : "no matching doc id for [" + enrichKey + "]"; |
| 85 | + assert penum.nextDoc() == PostingsEnum.NO_MORE_DOCS : "more than one doc id matching for [" + enrichKey + "]"; |
| 86 | + |
| 87 | + // TODO: The use of _source is temporarily until enrich source field mapper has been added (see PR #41521) |
| 88 | + Document document = leafReader.document(docId, Set.of(SourceFieldMapper.NAME)); |
| 89 | + BytesRef source = document.getBinaryValue(SourceFieldMapper.NAME); |
| 90 | + assert source != null; |
| 91 | + |
| 92 | + final BytesReference encoded = new BytesArray(source); |
| 93 | + final Map<String, Object> decoded = |
| 94 | + XContentHelper.convertToMap(encoded, false, XContentType.SMILE).v2(); |
| 95 | + for (EnrichProcessorFactory.EnrichSpecification specification : specifications) { |
| 96 | + Object enrichValue = decoded.get(specification.sourceField); |
| 97 | + ingestDocument.setFieldValue(specification.targetField, enrichValue); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + return ingestDocument; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public String getType() { |
| 106 | + return EnrichProcessorFactory.TYPE; |
| 107 | + } |
| 108 | + |
| 109 | + String getPolicyName() { |
| 110 | + return policyName; |
| 111 | + } |
| 112 | + |
| 113 | + String getEnrichKey() { |
| 114 | + return enrichKey; |
| 115 | + } |
| 116 | + |
| 117 | + boolean isIgnoreEnrichKeyMissing() { |
| 118 | + return ignoreEnrichKeyMissing; |
| 119 | + } |
| 120 | + |
| 121 | + List<EnrichProcessorFactory.EnrichSpecification> getSpecifications() { |
| 122 | + return specifications; |
| 123 | + } |
| 124 | +} |
0 commit comments