summaryrefslogtreecommitdiff
path: root/core/src/main/java/org/elasticsearch/search/suggest/completion/context/GeoQueryContext.java
blob: 291f65091e5b2bed6dda6304c062b05437438072 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.search.suggest.completion.context;

import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.geo.GeoUtils;
import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.elasticsearch.search.suggest.completion.context.GeoContextMapping.*;

/**
 * Defines the query context for {@link GeoContextMapping}
 */
public final class GeoQueryContext implements ToXContent {
    public GeoPoint geoPoint;
    public int boost = 1;
    public int precision = -1;
    public List<Integer> neighbours = new ArrayList<>(0);

    /**
     * Creates a query context for a given geo point with a boost of 1
     * and a precision of {@value GeoContextMapping#DEFAULT_PRECISION}
     */
    public GeoQueryContext(GeoPoint geoPoint) {
        this(geoPoint.geohash());
    }

    /**
     * Creates a query context for a given geo point with a
     * provided boost
     */
    public GeoQueryContext(GeoPoint geoPoint, int boost) {
        this(geoPoint.geohash(), boost);
    }

    /**
     * Creates a query context with a given geo hash with a boost of 1
     * and a precision of {@value GeoContextMapping#DEFAULT_PRECISION}
     */
    public GeoQueryContext(CharSequence geoHash) {
        this(geoHash, 1);
    }

    /**
     * Creates a query context for a given geo hash with a
     * provided boost
     */
    public GeoQueryContext(CharSequence geoHash, int boost) {
        this(geoHash, boost, -1);
    }

    /**
     * Creates a query context for a geo point with
     * a provided boost and enables generating neighbours
     * at specified precisions
     */
    public GeoQueryContext(CharSequence geoHash, int boost, int precision, Integer... neighbours) {
        this(GeoPoint.fromGeohash(geoHash.toString()), boost, precision, neighbours);
    }

    /**
     * Creates a query context for a geo hash with
     * a provided boost and enables generating neighbours
     * at specified precisions
     */
    public GeoQueryContext(GeoPoint geoPoint, int boost, int precision, Integer... neighbours) {
        this.geoPoint = geoPoint;
        this.boost = boost;
        this.precision = precision;
        Collections.addAll(this.neighbours, neighbours);
    }

    private GeoQueryContext() {
    }

    void setBoost(int boost) {
        this.boost = boost;
    }

    void setPrecision(int precision) {
        this.precision = precision;
    }

    void setNeighbours(List<Integer> neighbours) {
        this.neighbours = neighbours;
    }

    void setGeoPoint(GeoPoint geoPoint) {
        this.geoPoint = geoPoint;
    }

    private double lat = Double.NaN;
    void setLat(double lat) {
        this.lat = lat;
    }

    private double lon = Double.NaN;
    void setLon(double lon) {
        this.lon = lon;
    }

    void finish() {
        if (geoPoint == null) {
            if (Double.isNaN(lat) == false && Double.isNaN(lon) == false) {
                geoPoint = new GeoPoint(lat, lon);
            } else {
                throw new ElasticsearchParseException("no geohash or geo point provided");
            }
        }
    }

    private static ObjectParser<GeoQueryContext, GeoContextMapping> GEO_CONTEXT_PARSER = new ObjectParser<>("geo", null);
    static {
        GEO_CONTEXT_PARSER.declareField((parser, geoQueryContext, geoContextMapping) -> geoQueryContext.setGeoPoint(GeoUtils.parseGeoPoint(parser)), new ParseField("context"), ObjectParser.ValueType.OBJECT);
        GEO_CONTEXT_PARSER.declareInt(GeoQueryContext::setBoost, new ParseField("boost"));
        // TODO : add string support for precision for GeoUtils.geoHashLevelsForPrecision()
        GEO_CONTEXT_PARSER.declareInt(GeoQueryContext::setPrecision, new ParseField("precision"));
        // TODO : add string array support for precision for GeoUtils.geoHashLevelsForPrecision()
        GEO_CONTEXT_PARSER.declareIntArray(GeoQueryContext::setNeighbours, new ParseField("neighbours"));
        GEO_CONTEXT_PARSER.declareDouble(GeoQueryContext::setLat, new ParseField("lat"));
        GEO_CONTEXT_PARSER.declareDouble(GeoQueryContext::setLon, new ParseField("lon"));
    }

    public static GeoQueryContext parse(XContentParser parser) throws IOException {
        XContentParser.Token token = parser.currentToken();
        GeoQueryContext queryContext = new GeoQueryContext();
        if (token == XContentParser.Token.START_OBJECT) {
            GEO_CONTEXT_PARSER.parse(parser, queryContext);
        } else if (token == XContentParser.Token.VALUE_STRING) {
            queryContext.setGeoPoint(GeoPoint.fromGeohash(parser.text()));
        } else {
            throw new ElasticsearchParseException("geo context must be an object or string");
        }
        queryContext.finish();
        return queryContext;
    }

    @Override
    public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
        builder.startObject();
        builder.startObject(CONTEXT_VALUE);
        builder.field("lat", geoPoint.getLat());
        builder.field("lon", geoPoint.getLon());
        builder.endObject();
        builder.field(CONTEXT_BOOST, boost);
        builder.field(CONTEXT_NEIGHBOURS, neighbours);
        builder.field(CONTEXT_PRECISION, precision);
        builder.endObject();
        return builder;
    }
}