Skip to content

Commit 57a89f5

Browse files
dplewismontymxb
authored andcommitted
Add Android Polygon and Full Text (#499)
* Add Android Polygon and Full Text * Typos fix * typo
1 parent 3181215 commit 57a89f5

File tree

3 files changed

+125
-6
lines changed

3 files changed

+125
-6
lines changed

_includes/android/geopoints.md

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,42 @@ This point is then stored in the object as a regular field.
1616
placeObject.put("location", point);
1717
```
1818

19+
To retrieve a `ParseGeoPoint` from an object.
20+
21+
```java
22+
placeObject.getParseGeoPoint("location");
23+
```
24+
25+
## ParsePolygon
26+
27+
Parse allows you to associate polygon coordinates with an object. Adding a `ParsePolygon` to a `ParseObject` allows queries to determine whether a `ParseGeoPoint` is within a `ParsePolygon` or if a `ParsePolygon` contains a `ParseGeoPoint` .
28+
29+
* `ParsePolygon` must contain at least three coordinates.
30+
31+
For example, to create a polygon with coordinates (0, 0), (0, 1), (1, 1), (1, 0).
32+
33+
```java
34+
List<ParseGeoPoint> points = new ArrayList<ParseGeoPoint>();
35+
points.add(new ParseGeoPoint(0,0));
36+
points.add(new ParseGeoPoint(0,1));
37+
points.add(new ParseGeoPoint(1,1));
38+
points.add(new ParseGeoPoint(1,0));
39+
40+
ParsePolygon polygon = new ParsePolygon(points);
41+
```
42+
43+
This point is then stored in the object as a regular field.
44+
45+
```java
46+
placeObject.put("bounds", polygon);
47+
```
48+
49+
To retrieve a `ParsePolygon` from an object.
50+
51+
```java
52+
placeObject.getParsePolygon("bounds");
53+
```
54+
1955
## Geo Queries
2056

2157
Now that you have a bunch of objects with spatial coordinates, it would be nice to find out which objects are closest to a point. This can be done by adding another restriction to `ParseQuery` using `whereNear`. Getting a list of ten places that are closest to a user may look something like:
@@ -42,9 +78,46 @@ query.whereWithinGeoBox("location", southwestOfSF, northeastOfSF);
4278
query.findInBackground(new FindCallback<ParseObject>() { ... });
4379
```
4480

81+
You can query for whether an object lies within or on a polygon of `Parse.GeoPoint`.
82+
83+
```java
84+
ParseGeoPoint point = new ParseGeoPoint(0.5, 0.5);
85+
ParseQuery<ParseObject> query = ParseQuery.getQuery("PlaceObject");
86+
query.wherePolygonContains("location", point);
87+
```
88+
89+
You can also query for whether an object `Parse.Polygon` contains a `Parse.GeoPoint`.
90+
91+
```java
92+
ParseGeoPoint point = new ParseGeoPoint(0.5, 0.5);
93+
ParseQuery<ParseObject> query = ParseQuery.getQuery("PlaceObject");
94+
query.wherePolygonContains("location", point);
95+
```
96+
97+
To efficiently find if a `ParsePolygon` contains a `ParseGeoPoint` without querying use `containsPoint`.
98+
99+
```java
100+
List<ParseGeoPoint> points = new ArrayList<ParseGeoPoint>();
101+
points.add(new ParseGeoPoint(0, 0));
102+
points.add(new ParseGeoPoint(0, 1));
103+
points.add(new ParseGeoPoint(1, 1));
104+
points.add(new ParseGeoPoint(1, 0));
105+
106+
ParseGeoPoint inside = new ParseGeoPoint(0.5, 0.5);
107+
ParseGeoPoint outside = new ParseGeoPoint(10, 10);
108+
109+
ParsePolygon polygon = new ParsePolygon(points);
110+
111+
// Returns True
112+
polygon.containsPoint(inside);
113+
114+
// Returns False
115+
polygon.containsPoint(outside);
116+
```
117+
45118
## Parcelable
46119

47-
As most public facing components of the SDK, `ParseGeoPoint` implements the `Parcelable` interface. This means you can retain a `ParseGeoPoint` during configuration changes, or pass it to other components of the app through `Bundles`. To achieve this, depending on the context, use either `Parcel#writeParcelable(Parcelable, int)` or `Bundle#putParcelable(String, Parcelable)`. For instance, in an Activity,
120+
As most public facing components of the SDK, `ParseGeoPoint` and `ParsePolygon` implements the `Parcelable` interface. This means you can retain a `ParseGeoPoint` and `ParsePolygon` during configuration changes, or pass it to other components of the app through `Bundles`. To achieve this, depending on the context, use either `Parcel#writeParcelable(Parcelable, int)` or `Bundle#putParcelable(String, Parcelable)`. For instance, in an Activity,
48121

49122
```java
50123
private ParseGeoPoint point;
@@ -54,7 +127,7 @@ protected void onSaveInstanceState(Bundle outState) {
54127
super.onSaveInstanceState(outState);
55128
outState.putParcelable("point", point);
56129
}
57-
130+
58131
@Override
59132
protected void onCreate(@Nullable Bundle savedInstanceState) {
60133
if (savedInstanceState != null) {
@@ -63,6 +136,23 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
63136
}
64137
```
65138

139+
```java
140+
private ParsePolygon polygon;
141+
142+
@Override
143+
protected void onSaveInstanceState(Bundle outState) {
144+
super.onSaveInstanceState(outState);
145+
outState.putParcelable("polygon", polygon);
146+
}
147+
148+
@Override
149+
protected void onCreate(@Nullable Bundle savedInstanceState) {
150+
if (savedInstanceState != null) {
151+
polygon = (ParsePolygon) savedInstanceState.getParcelable("polygon");
152+
}
153+
}
154+
```
155+
66156
## Caveats
67157

68158
At the moment there are a couple of things to watch out for:

_includes/android/queries.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,35 @@ The above example will match any `BarbecueSauce` objects where the value in the
210210

211211
Queries that have regular expression constraints are very expensive. Refer to the [Performance Guide](#regular-expressions) for more details.
212212

213+
### Full Text Search
214+
215+
You can use `whereFullText` for efficient search capabilities. Text indexes are automatically created for you. Your strings are turned into tokens for fast searching.
216+
217+
* Note: Full Text Search can be resource intensive. Ensure the cost of using indexes is worth the benefit, see [storage requirements & performance costs of text indexes.](https://docs.mongodb.com/manual/core/index-text/#storage-requirements-and-performance-costs).
218+
219+
* Requires Parse Server 2.5.0+
220+
221+
```java
222+
// Finds barbecue sauces that start with 'Big Daddy's'.
223+
ParseQuery<ParseObject> query = ParseQuery.getQuery("BarbecueSauce");
224+
query.whereFullText("name", "Big Daddy's");
225+
```
226+
227+
The above example will match any `BarbecueSauce` objects where the value in the "name" String key contains "bbq". For example, both "Big Daddy's BBQ", "Big Daddy's bbq" and "Big BBQ Daddy" will match.
228+
229+
```java
230+
// You can sort by weight / rank. orderByAscending() and selectKeys()
231+
ParseQuery<ParseObject> query = ParseQuery.getQuery("BarbecueSauce");
232+
query.whereFullText("name", "Big Daddy's");
233+
query.orderByAscending("$score");
234+
query.selectKeys(Arrays.asList("$score"));
235+
// results will contain $score, results[0].getInt("$score");
236+
List<ParseObject> results = query.find();
237+
```
238+
239+
240+
For Case or Diacritic Sensitive search, please use the [REST API](http://docs.parseplatform.org/rest/guide/#queries-on-string-values).
241+
213242

214243
## Relational Queries
215244

@@ -365,7 +394,7 @@ final ParseQuery query = ...
365394
query.fromLocalDatastore().findInBackground().continueWithTask((task) -> {
366395
Exception error = task.getError();
367396
if (error instanceof ParseException && ((ParseException) error).getCode() == ParseException.CACHE_MISS) {
368-
// No results from cache. Let's query the network.
397+
// No results from cache. Let's query the network.
369398
return query.fromNetwork().findInBackground();
370399
}
371400
return task;

_includes/rest/queries.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,7 @@ curl -X GET \
10201020
-H "X-Parse-Master-Key: ${MASTER_KEY}" \
10211021
-H "X-Parse-REST-API-Key: ${REST_API_KEY}" \
10221022
-G \
1023-
--data-urlencode 'group:{objectId:null,total:{$sum:'$score'}}' \
1023+
--data-urlencode 'group:{"objectId":null,"total":{"$sum":"$score"}}' \
10241024
<span class="custom-parse-server-protocol">https</span>://<span class="custom-parse-server-url">YOUR.PARSE-SERVER.HERE</span><span class="custom-parse-server-mount">/parse/</span>aggregate/Player
10251025
</code></pre>
10261026
<pre><code class="python">
@@ -1050,7 +1050,7 @@ curl -X GET \
10501050
-H "X-Parse-Master-Key: ${MASTER_KEY}" \
10511051
-H "X-Parse-REST-API-Key: ${REST_API_KEY}" \
10521052
-G \
1053-
--data-urlencode 'project:{score:1}' \
1053+
--data-urlencode 'project={"score":1}' \
10541054
<span class="custom-parse-server-protocol">https</span>://<span class="custom-parse-server-url">YOUR.PARSE-SERVER.HERE</span><span class="custom-parse-server-mount">/parse/</span>aggregate/Player
10551055
</code></pre>
10561056
<pre><code class="python">
@@ -1077,7 +1077,7 @@ curl -X GET \
10771077
-H "X-Parse-Master-Key: ${MASTER_KEY}" \
10781078
-H "X-Parse-REST-API-Key: ${REST_API_KEY}" \
10791079
-G \
1080-
--data-urlencode 'match:{score:{$gt:15}}' \
1080+
--data-urlencode 'match={"score":{"$gt":15}}' \
10811081
<span class="custom-parse-server-protocol">https</span>://<span class="custom-parse-server-url">YOUR.PARSE-SERVER.HERE</span><span class="custom-parse-server-mount">/parse/</span>aggregate/Player
10821082
</code></pre>
10831083
<pre><code class="python">

0 commit comments

Comments
 (0)