Skip to content
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

Geo shape query #939

Open
slifer2015 opened this issue Oct 13, 2018 · 3 comments
Open

Geo shape query #939

slifer2015 opened this issue Oct 13, 2018 · 3 comments

Comments

@slifer2015
Copy link

slifer2015 commented Oct 13, 2018

Hi and thanks for awesome library
u seem missing the geo shape query
can u add the support for it

@olivere
Copy link
Owner

olivere commented Oct 14, 2018

GeoShapeQuery was last discussed in #276. It turned out that it was the most complex thing we'd had to do if you had all the builders that are also supported by the Java source.

A workaround is to use e.g. RawQuery and or create your own query by implementing the Query interface.

@cmitchell
Copy link

cmitchell commented Oct 15, 2018

I noticed a while ago that geo query wasn't implemented and implemented my own. It's by no means comprehensive, but it should get you started, and it follows your code patterns It's just one file, so please excuse me for not submitting a formal PR.. We've been using it for about a year now, so it' s compatible with both ES5 and ES6.

package your_package

// GeoShapeQuery extends the elastic package in golang, which does not currently implement geo_shape querying.
type GeoShapeQuery struct {
	path    string
	shape   string
	point   []float64
	polygon [][][]float64
}

// NewGeoShapeQuery creates a new elasticsearch geo_shape query
func NewGeoShapeQuery(path string) *GeoShapeQuery {
	return &GeoShapeQuery{
		path: path,
	}
}

// Shape specifies what shape this query represents
func (q *GeoShapeQuery) Shape(shape string) *GeoShapeQuery {
	q.shape = shape
	return q
}

// AddPoint adds a point
func (q *GeoShapeQuery) AddPoint(point []float64) *GeoShapeQuery {
	q.point = point
	return q
}

// AddPolygon adds a polygon
func (q *GeoShapeQuery) AddPolygon(polygon [][][]float64) *GeoShapeQuery {
	q.polygon = polygon
	return q
}

// Source returns JSON for the query.
func (q *GeoShapeQuery) Source() (interface{}, error) {
	source := make(map[string]interface{})

	params := make(map[string]interface{})
	source["geo_shape"] = params

	shape := make(map[string]interface{})
	params[q.path] = shape

	shapeDef := make(map[string]interface{})
	shape["shape"] = shapeDef

	shapeDef["type"] = q.shape
	if q.shape == "point" {
		shapeDef["coordinates"] = q.point
	} else if q.shape == "polygon" {
		shapeDef["coordinates"] = q.polygon
	}

	return source, nil
}

Example usage:

			if feature.Geometry.IsPoint() {
				aoiQueries = append(aoiQueries, NewGeoShapeQuery(aoiPath).Shape(strings.ToLower(pointType)).AddPoint(feature.Geometry.Point))
			} else if feature.Geometry.IsPolygon() {
				aoiQueries = append(aoiQueries, NewGeoShapeQuery(aoiPath).Shape(strings.ToLower(polygonType)).AddPolygon(feature.Geometry.Polygon))
			}

@olivere
Copy link
Owner

olivere commented Oct 15, 2018

Thanks @cmitchell. That's a good starter. What I really want is a GeoShapeBuilder for points, circles, polygons etc., just like in the Java source (something like this). But that's a lot of work--though it's just a builder, so not hard in terms of complexity. That [][][]float64 is correct, but it makes me shudder ;-)

@olivere olivere added this to the 6.2.15 milestone Dec 1, 2018
@olivere olivere modified the milestones: 6.2.15, 6.2.16 Jan 3, 2019
@olivere olivere removed this from the 6.2.16 milestone Jan 27, 2019
heltonmarx added a commit to heltonmarx/elastic that referenced this issue Mar 16, 2019
Based on the elasticsearch and GeoJSON specification, this PR try to
solve the olivere#939 supporting the follow types :
 * Circle
 * Envelope
 * Linestring
 * MultiLineString
 * MultiPoint
 * MultiPolygon
 * Point
 * Polygon

Any suggestion would be appreciated.

Change-Id: If7e6f59a3833da665e31e7ec179951780d82f9c2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants