-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Geo-Match Enrich Processor #47243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Geo-Match Enrich Processor #47243
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e67b11c
init
talevy 840273e
Geo-Match Enrich Processor
talevy d0bc160
Merge remote-tracking branch 'elastic/enrich' into enrich-geo
talevy cdce178
respond to review
talevy 3cc42e2
undo field mapper changes to geo_shape. unintentional
talevy 0947a8a
oh my, forgot another miscode
talevy 1c473cc
support multiple geo-points as input
talevy e733070
Merge remote-tracking branch 'elastic/enrich' into enrich-geo
talevy 5f32e31
Merge remote-tracking branch 'elastic/enrich' into enrich-geo
talevy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
91 changes: 91 additions & 0 deletions
91
x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/GeoMatchProcessor.java
This file contains hidden or 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,91 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.enrich; | ||
|
||
import org.elasticsearch.action.search.SearchRequest; | ||
import org.elasticsearch.action.search.SearchResponse; | ||
import org.elasticsearch.client.Client; | ||
import org.elasticsearch.common.geo.GeoPoint; | ||
import org.elasticsearch.common.geo.GeoUtils; | ||
import org.elasticsearch.common.geo.ShapeRelation; | ||
import org.elasticsearch.geometry.Geometry; | ||
import org.elasticsearch.geometry.MultiPoint; | ||
import org.elasticsearch.geometry.Point; | ||
import org.elasticsearch.index.query.GeoShapeQueryBuilder; | ||
import org.elasticsearch.index.query.QueryBuilder; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.BiConsumer; | ||
|
||
public final class GeoMatchProcessor extends AbstractEnrichProcessor { | ||
|
||
private ShapeRelation shapeRelation; | ||
|
||
GeoMatchProcessor(String tag, | ||
Client client, | ||
String policyName, | ||
String field, | ||
String targetField, | ||
boolean overrideEnabled, | ||
boolean ignoreMissing, | ||
String matchField, | ||
int maxMatches, | ||
ShapeRelation shapeRelation) { | ||
super(tag, client, policyName, field, targetField, ignoreMissing, overrideEnabled, matchField, maxMatches); | ||
this.shapeRelation = shapeRelation; | ||
} | ||
|
||
/** used in tests **/ | ||
GeoMatchProcessor(String tag, | ||
BiConsumer<SearchRequest, BiConsumer<SearchResponse, Exception>> searchRunner, | ||
String policyName, | ||
String field, | ||
String targetField, | ||
boolean overrideEnabled, | ||
boolean ignoreMissing, | ||
String matchField, | ||
int maxMatches, ShapeRelation shapeRelation) { | ||
super(tag, searchRunner, policyName, field, targetField, ignoreMissing, overrideEnabled, matchField, maxMatches); | ||
this.shapeRelation = shapeRelation; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public QueryBuilder getQueryBuilder(Object fieldValue) { | ||
List<Point> points = new ArrayList<>(); | ||
if (fieldValue instanceof List) { | ||
List<Object> values = (List<Object>) fieldValue; | ||
if (values.size() == 2 && values.get(0) instanceof Number) { | ||
GeoPoint geoPoint = GeoUtils.parseGeoPoint(values, true); | ||
points.add(new Point(geoPoint.lon(), geoPoint.lat())); | ||
} else { | ||
for (Object value : values) { | ||
GeoPoint geoPoint = GeoUtils.parseGeoPoint(value, true); | ||
points.add(new Point(geoPoint.lon(), geoPoint.lat())); | ||
} | ||
} | ||
} else { | ||
GeoPoint geoPoint = GeoUtils.parseGeoPoint(fieldValue, true); | ||
points.add(new Point(geoPoint.lon(), geoPoint.lat())); | ||
} | ||
final Geometry queryGeometry; | ||
if (points.isEmpty()) { | ||
throw new IllegalArgumentException("no geopoints found"); | ||
} else if (points.size() == 1) { | ||
queryGeometry = points.get(0); | ||
} else { | ||
queryGeometry = new MultiPoint(points); | ||
} | ||
GeoShapeQueryBuilder shapeQuery = new GeoShapeQueryBuilder(matchField, queryGeometry); | ||
shapeQuery.relation(shapeRelation); | ||
return shapeQuery; | ||
} | ||
|
||
public ShapeRelation getShapeRelation() { | ||
return shapeRelation; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't believe that
geo_shape
field type uses doc values by default, right?Do you know whether the default is going to change in the future?
I just want to make sure we never store more than what is needed for enrich. (since doc values are not being used for querying)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the problem is that since doc-values aren't supported, neither is
doc-values: false
. I was debating whether this should be fixed upstream so that one is allowed to set that. Currently, it errors out if you attempt to set itThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will follow-up in another PR to expose
doc-values
parsing togeo_shape
. I think it should understand that parameter, even though it only allows it to befalse
. For now, I think this is the best that can be done. Thankfully, I know when doc-values on shapes will be supported so I will be sure to update this!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 That sounds good to me.