|
| 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.search.TotalHits; |
| 9 | +import org.apache.lucene.util.SetOnce; |
| 10 | +import org.elasticsearch.action.search.SearchRequest; |
| 11 | +import org.elasticsearch.action.search.SearchResponse; |
| 12 | +import org.elasticsearch.action.search.SearchResponseSections; |
| 13 | +import org.elasticsearch.action.search.ShardSearchFailure; |
| 14 | +import org.elasticsearch.cluster.metadata.IndexMetaData; |
| 15 | +import org.elasticsearch.cluster.routing.Preference; |
| 16 | +import org.elasticsearch.common.bytes.BytesArray; |
| 17 | +import org.elasticsearch.common.settings.Settings; |
| 18 | +import org.elasticsearch.common.text.Text; |
| 19 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 20 | +import org.elasticsearch.common.xcontent.XContentType; |
| 21 | +import org.elasticsearch.geometry.Point; |
| 22 | +import org.elasticsearch.index.IndexSettings; |
| 23 | +import org.elasticsearch.index.VersionType; |
| 24 | +import org.elasticsearch.index.mapper.MapperService; |
| 25 | +import org.elasticsearch.index.query.ConstantScoreQueryBuilder; |
| 26 | +import org.elasticsearch.index.query.GeoShapeQueryBuilder; |
| 27 | +import org.elasticsearch.index.query.QueryShardContext; |
| 28 | +import org.elasticsearch.ingest.IngestDocument; |
| 29 | +import org.elasticsearch.search.SearchHit; |
| 30 | +import org.elasticsearch.search.SearchHits; |
| 31 | +import org.elasticsearch.search.aggregations.Aggregations; |
| 32 | +import org.elasticsearch.search.suggest.Suggest; |
| 33 | +import org.elasticsearch.test.ESTestCase; |
| 34 | + |
| 35 | +import java.io.ByteArrayOutputStream; |
| 36 | +import java.io.IOException; |
| 37 | +import java.io.UncheckedIOException; |
| 38 | +import java.util.Collections; |
| 39 | +import java.util.List; |
| 40 | +import java.util.Map; |
| 41 | +import java.util.function.BiConsumer; |
| 42 | + |
| 43 | +import static org.hamcrest.Matchers.emptyArray; |
| 44 | +import static org.hamcrest.Matchers.equalTo; |
| 45 | +import static org.hamcrest.Matchers.instanceOf; |
| 46 | +import static org.hamcrest.Matchers.notNullValue; |
| 47 | + |
| 48 | +public class GeoMatchProcessorTests extends ESTestCase { |
| 49 | + |
| 50 | + public void testBasics() throws Exception { |
| 51 | + int maxMatches = randomIntBetween(1, 8); |
| 52 | + MockSearchFunction mockSearch = mockedSearchFunction(Map.of("key", Map.of("shape", "object", "zipcode",94040))); |
| 53 | + GeoMatchProcessor processor = new GeoMatchProcessor("_tag", mockSearch, "_name", "location", "entry", |
| 54 | + "shape", false, true, maxMatches); |
| 55 | + IngestDocument ingestDocument = new IngestDocument("_index", "_type", "_id", "_routing", 1L, VersionType.INTERNAL, |
| 56 | + Map.of("location", "37.386637, -122.084110")); |
| 57 | + // Run |
| 58 | + IngestDocument[] holder = new IngestDocument[1]; |
| 59 | + processor.execute(ingestDocument, (result, e) -> holder[0] = result); |
| 60 | + assertThat(holder[0], notNullValue()); |
| 61 | + // Check request |
| 62 | + SearchRequest request = mockSearch.getCapturedRequest(); |
| 63 | + assertThat(request.indices().length, equalTo(1)); |
| 64 | + assertThat(request.indices()[0], equalTo(".enrich-_name")); |
| 65 | + assertThat(request.preference(), equalTo(Preference.LOCAL.type())); |
| 66 | + assertThat(request.source().size(), equalTo(maxMatches)); |
| 67 | + assertThat(request.source().trackScores(), equalTo(false)); |
| 68 | + assertThat(request.source().fetchSource().fetchSource(), equalTo(true)); |
| 69 | + assertThat(request.source().fetchSource().excludes(), emptyArray()); |
| 70 | + assertThat(request.source().fetchSource().includes(), emptyArray()); |
| 71 | + assertThat(request.source().query(), instanceOf(ConstantScoreQueryBuilder.class)); |
| 72 | + assertThat(((ConstantScoreQueryBuilder) request.source().query()).innerQuery(), instanceOf(GeoShapeQueryBuilder.class)); |
| 73 | + GeoShapeQueryBuilder shapeQueryBuilder = (GeoShapeQueryBuilder) ((ConstantScoreQueryBuilder) request.source().query()).innerQuery(); |
| 74 | + assertThat(shapeQueryBuilder.fieldName(), equalTo("shape")); |
| 75 | + assertThat(shapeQueryBuilder.shape(), equalTo(new Point(-122.084110, 37.386637))); |
| 76 | + o |
| 77 | + |
| 78 | + shapeQueryBuilder.toQuery() |
| 79 | + // Check result |
| 80 | + List<?> entries = ingestDocument.getFieldValue("entry", List.class); |
| 81 | + Map<?, ?> entry = (Map<?, ?>) entries.get(0); |
| 82 | + assertThat(entry.size(), equalTo(2)); |
| 83 | + assertThat(entry.get("zipcode"), equalTo(94040)); |
| 84 | + } |
| 85 | + |
| 86 | + private static final class MockSearchFunction implements BiConsumer<SearchRequest, BiConsumer<SearchResponse, Exception>> { |
| 87 | + private final SearchResponse mockResponse; |
| 88 | + private final SetOnce<SearchRequest> capturedRequest; |
| 89 | + private final Exception exception; |
| 90 | + |
| 91 | + MockSearchFunction(SearchResponse mockResponse) { |
| 92 | + this.mockResponse = mockResponse; |
| 93 | + this.exception = null; |
| 94 | + this.capturedRequest = new SetOnce<>(); |
| 95 | + } |
| 96 | + |
| 97 | + MockSearchFunction(Exception exception) { |
| 98 | + this.mockResponse = null; |
| 99 | + this.exception = exception; |
| 100 | + this.capturedRequest = new SetOnce<>(); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void accept(SearchRequest request, BiConsumer<SearchResponse, Exception> handler) { |
| 105 | + capturedRequest.set(request); |
| 106 | + if (exception != null) { |
| 107 | + handler.accept(null, exception); |
| 108 | + } else { |
| 109 | + handler.accept(mockResponse, null); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + SearchRequest getCapturedRequest() { |
| 114 | + return capturedRequest.get(); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + public MockSearchFunction mockedSearchFunction() { |
| 119 | + return new MockSearchFunction(mockResponse(Collections.emptyMap())); |
| 120 | + } |
| 121 | + |
| 122 | + public MockSearchFunction mockedSearchFunction(Exception exception) { |
| 123 | + return new MockSearchFunction(exception); |
| 124 | + } |
| 125 | + |
| 126 | + public MockSearchFunction mockedSearchFunction(Map<String, Map<String, ?>> documents) { |
| 127 | + return new MockSearchFunction(mockResponse(documents)); |
| 128 | + } |
| 129 | + |
| 130 | + public SearchResponse mockResponse(Map<String, Map<String, ?>> documents) { |
| 131 | + |
| 132 | + final QueryShardContext context = new QueryShardContext(0, new IndexSettings(new IndexMetaData(), Settings.EMPTY), null, null, null, mapperService, |
| 133 | + null, null, xContentRegistry(), writableRegistry(), client, leaf.reader(), () -> nowInMillis, null); |
| 134 | + |
| 135 | + SearchHit[] searchHits = documents.entrySet().stream().map(e -> { |
| 136 | + SearchHit searchHit = new SearchHit(randomInt(100), e.getKey(), new Text(MapperService.SINGLE_MAPPING_NAME), |
| 137 | + Collections.emptyMap()); |
| 138 | + try (XContentBuilder builder = XContentBuilder.builder(XContentType.SMILE.xContent())) { |
| 139 | + builder.map(e.getValue()); |
| 140 | + builder.flush(); |
| 141 | + ByteArrayOutputStream outputStream = (ByteArrayOutputStream) builder.getOutputStream(); |
| 142 | + searchHit.sourceRef(new BytesArray(outputStream.toByteArray())); |
| 143 | + } catch (IOException ex) { |
| 144 | + throw new UncheckedIOException(ex); |
| 145 | + } |
| 146 | + return searchHit; |
| 147 | + }).toArray(SearchHit[]::new); |
| 148 | + return new SearchResponse(new SearchResponseSections( |
| 149 | + new SearchHits(searchHits, new TotalHits(documents.size(), TotalHits.Relation.EQUAL_TO), 1.0f), |
| 150 | + new Aggregations(Collections.emptyList()), new Suggest(Collections.emptyList()), |
| 151 | + false, false, null, 1), null, 1, 1, 0, 1, ShardSearchFailure.EMPTY_ARRAY, new SearchResponse.Clusters(1, 1, 0)); |
| 152 | + } |
| 153 | +} |
0 commit comments