org.elasticsearch.common.geo.ShapeRelation Java Examples

The following examples show how to use org.elasticsearch.common.geo.ShapeRelation. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GeoUtils.java    From elasticsearch-sql with MIT License 6 votes vote down vote up
public static ShapeRelation parseGeoRelation(int relation){
    switch (relation){
        case ElasticsearchParser.WITHIN:{
            return ShapeRelation.WITHIN;
        }
        case ElasticsearchParser.DISJOINT:{
            return ShapeRelation.DISJOINT;
        }
        case ElasticsearchParser.CONTAINS:{
            return ShapeRelation.CONTAINS;
        }
        default:
        case ElasticsearchParser.INTERSECTS:{
            return ShapeRelation.INTERSECTS;
        }
    }
}
 
Example #2
Source File: ElasticsearchIndex.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
protected Iterable<? extends DocumentResult> geoRelationQuery(String relation, IRI geoProperty, Shape shape,
		Var contextVar) throws MalformedQueryException, IOException {
	ShapeRelation spatialOp = toSpatialOp(relation);
	if (spatialOp == null) {
		return null;
	}
	final String fieldName = toGeoShapeFieldName(SearchFields.getPropertyField(geoProperty));
	GeoShapeQueryBuilder fb = QueryBuilders.geoShapeQuery(fieldName,
			ElasticsearchSpatialSupport.getSpatialSupport().toShapeBuilder(shape));
	fb.relation(spatialOp);
	QueryBuilder qb = QueryBuilders.matchAllQuery();
	if (contextVar != null) {
		qb = addContextTerm(qb, (Resource) contextVar.getValue());
	}

	SearchRequestBuilder request = client.prepareSearch();
	SearchHits hits = search(request, QueryBuilders.boolQuery().must(qb).filter(fb));
	return Iterables.transform(hits, new Function<SearchHit, DocumentResult>() {

		@Override
		public DocumentResult apply(SearchHit hit) {
			return new ElasticsearchDocumentResult(hit, geoContextMapper);
		}
	});
}
 
Example #3
Source File: ToMatchQuery.java    From crate with Apache License 2.0 5 votes vote down vote up
private SpatialArgs getArgs(Shape shape, ShapeRelation relation) {
    switch (relation) {
        case INTERSECTS:
            return new SpatialArgs(SpatialOperation.Intersects, shape);
        case DISJOINT:
            return new SpatialArgs(SpatialOperation.IsDisjointTo, shape);
        case WITHIN:
            return new SpatialArgs(SpatialOperation.IsWithin, shape);
        default:
            throw invalidMatchType(relation.getRelationName());
    }
}
 
Example #4
Source File: QueryBuilderHelper.java    From staccato with Apache License 2.0 5 votes vote down vote up
/**
 * Builds an Elasticsearch bbox query
 *
 * @param bbox The bbox values passed in the api request
 * @return The Elasticsearch query builder
 */

public static Optional<QueryBuilder> bboxBuilder(double[] bbox) {
    if (null == bbox || (!(bbox.length == 4 || bbox.length == 6))) {
        return Optional.empty();
    }
    Coordinate c1 = new Coordinate(bbox[WEST], bbox[NORTH]);
    Coordinate c2 = new Coordinate(bbox[EAST], bbox[SOUTH]);
    EnvelopeBuilder envelopeBuilder = new EnvelopeBuilder(c1, c2);

    return Optional.of(
            new GeoShapeQueryBuilder(FieldName.GEOMETRY, envelopeBuilder).relation(ShapeRelation.INTERSECTS));
}
 
Example #5
Source File: MappedFieldType.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Factory method for range queries.
 * @param relation the relation, nulls should be interpreted like INTERSECTS
 */
public Query rangeQuery(Object lowerTerm,
                        Object upperTerm,
                        boolean includeLower,
                        boolean includeUpper,
                        ShapeRelation relation,
                        DateTimeZone timeZone,
                        QueryShardContext context) {
    throw new IllegalArgumentException("Field [" + name + "] of type [" + typeName() + "] does not support range queries");
}
 
Example #6
Source File: SimpleMappedFieldType.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public final Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper,
                              ShapeRelation relation, DateTimeZone timeZone, QueryShardContext context) {
    if (relation == ShapeRelation.DISJOINT) {
        throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() +
                "] does not support DISJOINT ranges");
    }
    // We do not fail on non-null time zones and date parsers
    // The reasoning is that on query parsers, you might want to set a time zone or format for date fields
    // but then the API has no way to know which fields are dates and which fields are not dates
    return rangeQuery(lowerTerm, upperTerm, includeLower, includeUpper, context);
}
 
Example #7
Source File: DateFieldMapper.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query rangeQuery(Object lowerTerm,
                        Object upperTerm,
                        boolean includeLower,
                        boolean includeUpper,
                        ShapeRelation relation,
                        @Nullable DateTimeZone timeZone,
                        QueryShardContext context) {
    failIfNotIndexed();
    if (relation == ShapeRelation.DISJOINT) {
        throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() +
                "] does not support DISJOINT ranges");
    }
    long l, u;
    if (lowerTerm == null) {
        l = Long.MIN_VALUE;
    } else {
        l = (Long) lowerTerm;
        if (includeLower == false) {
            ++l;
        }
    }
    if (upperTerm == null) {
        u = Long.MAX_VALUE;
    } else {
        u = (Long) upperTerm;
        if (includeUpper == false) {
            --u;
        }
    }
    Query query = LongPoint.newRangeQuery(name(), l, u);
    if (hasDocValues()) {
        Query dvQuery = SortedNumericDocValuesField.newSlowRangeQuery(name(), l, u);
        query = new IndexOrDocValuesQuery(query, dvQuery);
    }
    return query;
}
 
Example #8
Source File: ElasticsearchIndex.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private ShapeRelation toSpatialOp(String relation) {
	if (GEOF.SF_INTERSECTS.stringValue().equals(relation)) {
		return ShapeRelation.INTERSECTS;
	}
	if (GEOF.SF_DISJOINT.stringValue().equals(relation)) {
		return ShapeRelation.DISJOINT;
	}
	if (GEOF.EH_COVERED_BY.stringValue().equals(relation)) {
		return ShapeRelation.WITHIN;
	}
	return null;
}
 
Example #9
Source File: T_IEsQueryDao.java    From AsuraFramework with Apache License 2.0 5 votes vote down vote up
@org.junit.Test
public void t_queryByEsQueryDo2()throws Exception {
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    QueryBuilder qb = QueryBuilders.matchAllQuery();
    searchSourceBuilder.query(qb);
    ShapeBuilder shapeBuilder = ShapeBuilder.newPoint(new Coordinate(116.402257, 39.914548));
    QueryBuilder qb2 = QueryBuilders.geoShapeQuery("geometry", shapeBuilder, ShapeRelation.CONTAINS);

    QueryBuilder qb3 = QueryBuilders.boolQuery().must(qb).filter(qb2);
    searchSourceBuilder.query(qb3);
   iEsQueryDao.query(searchSourceBuilder,"twitter","user","user2");
    /*logger.info(JSON.toJSONString(es));
    Assert.assertNotEquals("1", es.getStatus());*/
}
 
Example #10
Source File: GeoShapeQueryParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public static SpatialArgs getArgs(ShapeBuilder shape, ShapeRelation relation) {
    switch(relation) {
    case DISJOINT:
        return new SpatialArgs(SpatialOperation.IsDisjointTo, shape.build());
    case INTERSECTS:
        return new SpatialArgs(SpatialOperation.Intersects, shape.build());
    case WITHIN:
        return new SpatialArgs(SpatialOperation.IsWithin, shape.build());
    case CONTAINS:
        return new SpatialArgs(SpatialOperation.Contains, shape.build());
    default:
        throw new IllegalArgumentException("");

    }
}
 
Example #11
Source File: GeoShapeQueryBuilder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private GeoShapeQueryBuilder(String name, ShapeBuilder shape, String indexedShapeId, String indexedShapeType, ShapeRelation relation) {
    this.name = name;
    this.shape = shape;
    this.indexedShapeId = indexedShapeId;
    this.relation = relation;
    this.indexedShapeType = indexedShapeType;
}
 
Example #12
Source File: LuceneQueryBuilder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private SpatialArgs getArgs(Shape shape, ShapeRelation relation) {
    switch (relation) {
        case INTERSECTS:
            return new SpatialArgs(SpatialOperation.Intersects, shape);
        case DISJOINT:
            return new SpatialArgs(SpatialOperation.IsDisjointTo, shape);
        case WITHIN:
            return new SpatialArgs(SpatialOperation.IsWithin, shape);
    }
    throw invalidMatchType(relation.getRelationName());
}
 
Example #13
Source File: QueryBuilders.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public static GeoShapeQueryBuilder geoWithinQuery(String name, String indexedShapeId, String indexedShapeType) {
    return geoShapeQuery(name, indexedShapeId, indexedShapeType, ShapeRelation.WITHIN);
}
 
Example #14
Source File: QueryBuilders.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public static GeoShapeQueryBuilder geoDisjointQuery(String name, String indexedShapeId, String indexedShapeType) {
    return geoShapeQuery(name, indexedShapeId, indexedShapeType, ShapeRelation.DISJOINT);
}
 
Example #15
Source File: QueryBuilders.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public static GeoShapeQueryBuilder geoIntersectionQuery(String name, String indexedShapeId, String indexedShapeType) {
    return geoShapeQuery(name, indexedShapeId, indexedShapeType, ShapeRelation.INTERSECTS);
}
 
Example #16
Source File: Test.java    From AsuraFramework with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
   /* QueryBuilder qb1 = QueryBuilders.matchQuery("a","b");
    System.out.println(qb1.toString());*/
    String json="{\"query\":{\"match_all\":{}},\"filter\":{\"geo_shape\":{\"geometry\":{\"relation\":\"CONTAINS\",\"shape\":{\"coordinates\":[116.402257,39.914548],\"type\":\"point\"}}}}}";
    QueryBuilder qb= QueryBuilders.matchAllQuery();
    //System.out.println(qb.toString());
    SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();
    searchSourceBuilder.query(qb);
   // System.out.println(searchSourceBuilder.toString());
    ShapeBuilder shapeBuilder= ShapeBuilder.newPoint(new Coordinate(116.402257,39.914548));
    QueryBuilder qb2= QueryBuilders.geoShapeQuery("geometry",shapeBuilder, ShapeRelation.CONTAINS);
    System.out.println(qb2.toString());

    //searchSourceBuilder.postFilter(qb2);
    QueryBuilder qb3= QueryBuilders.boolQuery().must(qb).filter(qb2);
    searchSourceBuilder.query(qb3);
    System.out.println(qb3.toString());
    System.out.println(searchSourceBuilder.toString());
    QueryBuilder qb4= QueryBuilders.boolQuery().must(qb).must(qb2);
    System.out.println(qb4.toString());


    SortBuilder sort= SortBuilders.geoDistanceSort("pin.location") .point(40, -70).
            unit(DistanceUnit.fromString(DistanceUnit.KILOMETERS.toString())).order(SortOrder.DESC);
  /*  QueryBuilder qb5 = QueryBuilders.geoDistanceQuery("pin.location")
            .point(40, -70)
            .distance(400,  DistanceUnit.fromString(DistanceUnit.KILOMETERS.toString()))
            .geoDistance(GeoDistance.ARC);
             System.out.println(qb5.toString());
            */
    searchSourceBuilder.sort(sort);
    System.out.println(searchSourceBuilder.toString());
    //QueryBuilder qb3=QueryBuilders.filteredQuery(null,qb2);
    //QueryBuilder qb4=QueryBuilders.filteredQuery(qb,qb2);
    //searchSourceBuilder.query(qb3.toString());
   // searchSourceBuilder.query(qb4);
   // System.out.println(qb4.toString());
    //System.out.println(searchSourceBuilder.toString());

    // System.out.println(qb.toString());
   /* QueryBuilder qb2 = QueryBuilders.geoBoundingBoxQuery("pin.location")
            .topLeft(40.73, -74.1)
            .bottomRight(40.717, -73.99);
    //String  strstr= JSON.toJSONString(qb2);
    System.out.println(qb2.toString());
    System.out.println("1111111");*/
}
 
Example #17
Source File: QueryBuilders.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public static GeoShapeQueryBuilder geoShapeQuery(String name, String indexedShapeId, String indexedShapeType, ShapeRelation relation) {
    return new GeoShapeQueryBuilder(name, indexedShapeId, indexedShapeType, relation);
}
 
Example #18
Source File: ArrayFieldType.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, ShapeRelation relation, DateTimeZone timeZone, QueryShardContext context) {
    return innerFieldType.rangeQuery(lowerTerm, upperTerm, includeLower, includeUpper, relation, timeZone, context);
}
 
Example #19
Source File: QueryBuilders.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * A filter to filter indexed shapes that are contained by a shape
 *
 * @param name  The shape field name
 * @param shape Shape to use in the filter
 */
public static GeoShapeQueryBuilder geoWithinQuery(String name, ShapeBuilder shape) {
    return geoShapeQuery(name, shape, ShapeRelation.WITHIN);
}
 
Example #20
Source File: QueryBuilders.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * A filter to filter indexed shapes that are not intersection with the query shape
 *
 * @param name  The shape field name
 * @param shape Shape to use in the filter
 */
public static GeoShapeQueryBuilder geoDisjointQuery(String name, ShapeBuilder shape) {
    return geoShapeQuery(name, shape, ShapeRelation.DISJOINT);
}
 
Example #21
Source File: QueryBuilders.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * A filter to filter indexed shapes intersecting with shapes
 *
 * @param name  The shape field name
 * @param shape Shape to use in the filter
 */
public static GeoShapeQueryBuilder geoIntersectionQuery(String name, ShapeBuilder shape) {
    return geoShapeQuery(name, shape, ShapeRelation.INTERSECTS);
}
 
Example #22
Source File: QueryBuilders.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * A filter based on the relationship of a shape and indexed shapes
 *
 * @param name  The shape field name
 * @param shape Shape to use in the filter
 * @param relation relation of the shapes
 */
public static GeoShapeQueryBuilder geoShapeQuery(String name, ShapeBuilder shape, ShapeRelation relation) {
    return new GeoShapeQueryBuilder(name, shape, relation);
}
 
Example #23
Source File: GeoShapeQueryBuilder.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new GeoShapeQueryBuilder whose Filter will be against the given field name
 * and will use the Shape found with the given ID in the given type
 *
 * @param name             Name of the field that will be filtered
 * @param indexedShapeId   ID of the indexed Shape that will be used in the Filter
 * @param indexedShapeType Index type of the indexed Shapes
 */
public GeoShapeQueryBuilder(String name, String indexedShapeId, String indexedShapeType, ShapeRelation relation) {
    this(name, null, indexedShapeId, indexedShapeType, relation);
}
 
Example #24
Source File: GeoShapeQueryBuilder.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new GeoShapeQueryBuilder whose Filter will be against the
 * given field name using the given Shape
 *
 * @param name  Name of the field that will be filtered
 * @param relation {@link ShapeRelation} of query and indexed shape
 * @param shape Shape used in the filter
 */
public GeoShapeQueryBuilder(String name, ShapeBuilder shape, ShapeRelation relation) {
    this(name, shape, null, null, relation);
}