Search in sources :

Example 1 with GeoUtils

use of org.elasticsearch.common.geo.GeoUtils in project elasticsearch by elastic.

the class GeoContextMapping method toInternalQueryContexts.

/**
     * Parse a list of {@link GeoQueryContext}
     * using <code>parser</code>. A QueryContexts accepts one of the following forms:
     *
     * <ul>
     *     <li>Object: GeoQueryContext</li>
     *     <li>String: GeoQueryContext value with boost=1  precision=PRECISION neighbours=[PRECISION]</li>
     *     <li>Array: <pre>[GeoQueryContext, ..]</pre></li>
     * </ul>
     *
     *  A GeoQueryContext has one of the following forms:
     *  <ul>
     *     <li>Object:
     *     <ul>
     *         <li><pre>GEO POINT</pre></li>
     *         <li><pre>{&quot;lat&quot;: <i>&lt;double&gt;</i>, &quot;lon&quot;: <i>&lt;double&gt;</i>, &quot;precision&quot;: <i>&lt;int&gt;</i>, &quot;neighbours&quot;: <i>&lt;[int, ..]&gt;</i>}</pre></li>
     *         <li><pre>{&quot;context&quot;: <i>&lt;string&gt;</i>, &quot;boost&quot;: <i>&lt;int&gt;</i>, &quot;precision&quot;: <i>&lt;int&gt;</i>, &quot;neighbours&quot;: <i>&lt;[int, ..]&gt;</i>}</pre></li>
     *         <li><pre>{&quot;context&quot;: <i>&lt;GEO POINT&gt;</i>, &quot;boost&quot;: <i>&lt;int&gt;</i>, &quot;precision&quot;: <i>&lt;int&gt;</i>, &quot;neighbours&quot;: <i>&lt;[int, ..]&gt;</i>}</pre></li>
     *     </ul>
     *     <li>String: <pre>GEO POINT</pre></li>
     *  </ul>
     * see {@link GeoUtils#parseGeoPoint(String, GeoPoint)} for GEO POINT
     */
@Override
public List<InternalQueryContext> toInternalQueryContexts(List<GeoQueryContext> queryContexts) {
    List<InternalQueryContext> internalQueryContextList = new ArrayList<>();
    for (GeoQueryContext queryContext : queryContexts) {
        int minPrecision = Math.min(this.precision, queryContext.getPrecision());
        GeoPoint point = queryContext.getGeoPoint();
        final Collection<String> locations = new HashSet<>();
        String geoHash = stringEncode(point.getLon(), point.getLat(), minPrecision);
        locations.add(geoHash);
        if (queryContext.getNeighbours().isEmpty() && geoHash.length() == this.precision) {
            addNeighbors(geoHash, locations);
        } else if (queryContext.getNeighbours().isEmpty() == false) {
            queryContext.getNeighbours().stream().filter(neighbourPrecision -> neighbourPrecision < geoHash.length()).forEach(neighbourPrecision -> {
                String truncatedGeoHash = geoHash.substring(0, neighbourPrecision);
                locations.add(truncatedGeoHash);
                addNeighbors(truncatedGeoHash, locations);
            });
        }
        internalQueryContextList.addAll(locations.stream().map(location -> new InternalQueryContext(location, queryContext.getBoost(), location.length() < this.precision)).collect(Collectors.toList()));
    }
    return internalQueryContextList;
}
Also used : GeoUtils(org.elasticsearch.common.geo.GeoUtils) StringField(org.apache.lucene.document.StringField) IndexableField(org.apache.lucene.index.IndexableField) Document(org.elasticsearch.index.mapper.ParseContext.Document) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ParseContext(org.elasticsearch.index.mapper.ParseContext) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Map(java.util.Map) GeoPoint(org.elasticsearch.common.geo.GeoPoint) FieldMapper(org.elasticsearch.index.mapper.FieldMapper) GeoPointFieldMapper(org.elasticsearch.index.mapper.GeoPointFieldMapper) Collection(java.util.Collection) Set(java.util.Set) IOException(java.io.IOException) GeoHashUtils.stringEncode(org.elasticsearch.common.geo.GeoHashUtils.stringEncode) Collectors(java.util.stream.Collectors) QueryParseContext(org.elasticsearch.index.query.QueryParseContext) XContentParser(org.elasticsearch.common.xcontent.XContentParser) Objects(java.util.Objects) List(java.util.List) DistanceUnit(org.elasticsearch.common.unit.DistanceUnit) DocValuesType(org.apache.lucene.index.DocValuesType) Token(org.elasticsearch.common.xcontent.XContentParser.Token) GeoHashUtils.addNeighbors(org.elasticsearch.common.geo.GeoHashUtils.addNeighbors) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) GeoPoint(org.elasticsearch.common.geo.GeoPoint) ArrayList(java.util.ArrayList) GeoPoint(org.elasticsearch.common.geo.GeoPoint) HashSet(java.util.HashSet)

Aggregations

IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 StringField (org.apache.lucene.document.StringField)1 DocValuesType (org.apache.lucene.index.DocValuesType)1 IndexableField (org.apache.lucene.index.IndexableField)1 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)1 GeoHashUtils.addNeighbors (org.elasticsearch.common.geo.GeoHashUtils.addNeighbors)1 GeoHashUtils.stringEncode (org.elasticsearch.common.geo.GeoHashUtils.stringEncode)1 GeoPoint (org.elasticsearch.common.geo.GeoPoint)1 GeoUtils (org.elasticsearch.common.geo.GeoUtils)1 DistanceUnit (org.elasticsearch.common.unit.DistanceUnit)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 XContentParser (org.elasticsearch.common.xcontent.XContentParser)1