Skip to content

Commit 3486699

Browse files
committed
fix sortByText
1 parent 3889682 commit 3486699

File tree

4 files changed

+43
-40
lines changed

4 files changed

+43
-40
lines changed

ParseSwift.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1382,7 +1382,6 @@
13821382
700396E925A3892D0052CB31 /* LiveQuerySocketDelegate.swift */,
13831383
700396F725A394AE0052CB31 /* ParseLiveQueryDelegate.swift */,
13841384
700395D025A147BE0052CB31 /* QuerySubscribable.swift */,
1385-
70A98D812794AB3C009B58F2 /* ParseQueryScorable.swift */,
13861385
);
13871386
path = Protocols;
13881387
sourceTree = "<group>";
@@ -1406,6 +1405,7 @@
14061405
F97B45C524D9C6F200F4A88B /* Fetchable.swift */,
14071406
705A9A2E25991C1400B3547F /* Fileable.swift */,
14081407
70BC988F252A5B5C00FF3074 /* Objectable.swift */,
1408+
70A98D812794AB3C009B58F2 /* ParseQueryScorable.swift */,
14091409
70647E9B259E3A9A004C1004 /* ParseType.swift */,
14101410
F97B45C824D9C6F200F4A88B /* Queryable.swift */,
14111411
91BB8FCE2690BA70005A6BA5 /* QueryObservable.swift */,

Sources/ParseSwift/LiveQuery/Protocols/ParseQueryScorable.swift

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// ParseQueryScorable.swift
3+
// ParseSwift
4+
//
5+
// Created by Corey Baker on 1/16/22.
6+
// Copyright © 2022 Parse Community. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
/**
12+
Conform to this protocol to add the required properties to your `ParseObject`
13+
for using `QueryConstraint.matchesText()` and `Query.sortByTextScore()`.
14+
- note: In order to sort you must use `Query.sortByTextScore()`.
15+
To retrieve the weight/rank, access the "score" property of your `ParseObject`.
16+
*/
17+
public protocol ParseQueryScorable {
18+
/**
19+
The weight/rank of a `QueryConstraint.matchesText()`.
20+
*/
21+
var score: Double? { get }
22+
}
23+
24+
// MARK: ParseQueryScorable
25+
extension Query {
26+
/**
27+
Method to sort the full text search by text score.
28+
- parameter value: String or Object of index that should be used when executing query.
29+
- note: Your `ParseObject` should conform to `ParseQueryScorable` to retrieve
30+
the weight/rank via the "score" property of your `ParseObject`.
31+
*/
32+
public func sortByTextScore() -> Query<T> where T: ParseObject & ParseQueryScorable {
33+
var mutableQuery = self
34+
let ascendingScore = Order.ascending(QueryConstraint.Comparator.score.rawValue)
35+
if mutableQuery.order != nil {
36+
mutableQuery.order!.append(ascendingScore)
37+
} else {
38+
mutableQuery.order = [ascendingScore]
39+
}
40+
return mutableQuery.select(QueryConstraint.Comparator.score.rawValue)
41+
}
42+
}

Sources/ParseSwift/Types/Query.swift

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -172,23 +172,6 @@ public struct Query<T>: Encodable, Equatable where T: ParseObject {
172172
return mutableQuery
173173
}
174174

175-
/**
176-
Method to sort the full text search by text score.
177-
- parameter value: String or Object of index that should be used when executing query.
178-
- note: Your `ParseObject` should conform to `ParseQueryScorable` to retrieve
179-
the weight/rank via the "score" property of your `ParseObject`.
180-
*/
181-
public func sortByTextScore() -> Query<T> {
182-
var mutableQuery = self
183-
let ascendingScore = Order.ascending(QueryConstraint.Comparator.score.rawValue)
184-
if mutableQuery.order != nil {
185-
mutableQuery.order!.append(ascendingScore)
186-
} else {
187-
mutableQuery.order = [ascendingScore]
188-
}
189-
return mutableQuery.select(QueryConstraint.Comparator.score.rawValue)
190-
}
191-
192175
/**
193176
Changes the read preference that the backend will use when performing the query to the database.
194177
- parameter readPreference: The read preference for the main query.

0 commit comments

Comments
 (0)