Skip to content

Commit e91d28e

Browse files
committed
PCBC-508: Add support for FTS advanced sorting
Change-Id: I0adaf30589dc6bbdba0e96c0c791bd6f9d8cb237 Reviewed-on: http://review.couchbase.org/89259 Tested-by: Build Bot <[email protected]> Reviewed-by: Sergey Avseyev <[email protected]>
1 parent cc84556 commit e91d28e

File tree

14 files changed

+1328
-8
lines changed

14 files changed

+1328
-8
lines changed

api/couchbase.php

Lines changed: 164 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1969,7 +1969,7 @@ public function consistentWith($state) {}
19691969
* - UPSERT
19701970
* - DELETE
19711971
*
1972-
* @param boolean $readonly true if readonly should be forced, false is the default and will use the server side default.
1972+
* @param bool $readonly true if readonly should be forced, false is the default and will use the server side default.
19731973
* @return N1qlQuery
19741974
*/
19751975
public function readonly($readonly) {}
@@ -3395,6 +3395,169 @@ public function jsonSerialize() {}
33953395
public function addRange($name, $min, $max) {}
33963396
}
33973397

3398+
/**
3399+
* Base class for all FTS sort options in querying.
3400+
*/
3401+
class SearchSort {
3402+
/** @ignore */
3403+
private function __construct() {}
3404+
3405+
/**
3406+
* Sort by the document identifier.
3407+
*
3408+
* @return SearchSortId
3409+
*/
3410+
public static function id() {}
3411+
3412+
/**
3413+
* Sort by the hit score.
3414+
*
3415+
* @return SearchSortScore
3416+
*/
3417+
public static function score() {}
3418+
3419+
/**
3420+
* Sort by a field in the hits.
3421+
*
3422+
* @param string $field the field name
3423+
*
3424+
* @return SearchSortField
3425+
*/
3426+
public static function field($field) {}
3427+
3428+
/**
3429+
* Sort by geo location.
3430+
*
3431+
* @param string $field the field name
3432+
* @param float $longitude the longitude of the location
3433+
* @param float $latitude the latitude of the location
3434+
*
3435+
* @return SearchSortGeoDistance
3436+
*/
3437+
public static function geoDistance($field, $longitude, $latitude) {}
3438+
}
3439+
3440+
/**
3441+
* Sort by the document identifier.
3442+
*/
3443+
class SearchSortId extends SearchSort implements \JsonSerializable {
3444+
/** @ignore */
3445+
private function __construct() {}
3446+
3447+
/**
3448+
* Direction of the sort
3449+
*
3450+
* @param bool $descending
3451+
*
3452+
* @return SearchSortId
3453+
*/
3454+
public function descending($descending) {}
3455+
}
3456+
3457+
/**
3458+
* Sort by the hit score.
3459+
*/
3460+
class SearchSortScore extends SearchSort implements \JsonSerializable {
3461+
/** @ignore */
3462+
private function __construct() {}
3463+
3464+
/**
3465+
* Direction of the sort
3466+
*
3467+
* @param bool $descending
3468+
*
3469+
* @return SearchSortScore
3470+
*/
3471+
public function descending($descending) {}
3472+
}
3473+
3474+
/**
3475+
* Sort by a field in the hits.
3476+
*/
3477+
class SearchSortField extends SearchSort implements \JsonSerializable {
3478+
const TYPE_AUTO = "auto";
3479+
const TYPE_STRING = "string";
3480+
const TYPE_NUMBER = "number";
3481+
const TYPE_DATE = "date";
3482+
3483+
const MODE_DEFAULT = "default";
3484+
const MODE_MIN = "min";
3485+
const MODE_MAX = "max";
3486+
3487+
const MISSING_FIRST = "first";
3488+
const MISSING_LAST = "last";
3489+
3490+
/** @ignore */
3491+
private function __construct() {}
3492+
3493+
/**
3494+
* Direction of the sort
3495+
*
3496+
* @param bool $descending
3497+
*
3498+
* @return SearchSortField
3499+
*/
3500+
public function descending($descending) {}
3501+
3502+
/**
3503+
* Set type of the field
3504+
*
3505+
* @param string type the type
3506+
*
3507+
* @see SearchSortField::TYPE_AUTO
3508+
* @see SearchSortField::TYPE_STRING
3509+
* @see SearchSortField::TYPE_NUMBER
3510+
* @see SearchSortField::TYPE_DATE
3511+
*/
3512+
public function type($type) {}
3513+
3514+
/**
3515+
* Set mode of the sort
3516+
*
3517+
* @param string mode the mode
3518+
*
3519+
* @see SearchSortField::MODE_MIN
3520+
* @see SearchSortField::MODE_MAX
3521+
*/
3522+
public function mode($mode) {}
3523+
3524+
/**
3525+
* Set where the hits with missing field will be inserted
3526+
*
3527+
* @param string missing strategy for hits with missing fields
3528+
*
3529+
* @see SearchSortField::MISSING_FIRST
3530+
* @see SearchSortField::MISSING_LAST
3531+
*/
3532+
public function missing($missing) {}
3533+
}
3534+
3535+
/**
3536+
* Sort by a location and unit in the hits.
3537+
*/
3538+
class SearchSortGeoDistance extends SearchSort implements \JsonSerializable {
3539+
/** @ignore */
3540+
private function __construct() {}
3541+
3542+
/**
3543+
* Direction of the sort
3544+
*
3545+
* @param bool $descending
3546+
*
3547+
* @return SearchSortGeoDistance
3548+
*/
3549+
public function descending($descending) {}
3550+
3551+
/**
3552+
* Name of the units
3553+
*
3554+
* @param string $unit
3555+
*
3556+
* @return SearchSortGeoDistance
3557+
*/
3558+
public function unit($unit) {}
3559+
}
3560+
33983561
/**
33993562
* Represents a Analytics query (currently experimental support).
34003563
*

config.m4

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ COUCHBASE_FILES=" \
9191
src/couchbase/n1ql_query.c \
9292
src/couchbase/search_query.c \
9393
src/couchbase/search/query_part.c \
94+
src/couchbase/search/sort.c \
95+
src/couchbase/search/sort_field.c \
96+
src/couchbase/search/sort_geo.c \
97+
src/couchbase/search/sort_id.c \
98+
src/couchbase/search/sort_score.c \
9499
src/couchbase/search/boolean_field_query.c \
95100
src/couchbase/search/boolean_query.c \
96101
src/couchbase/search/conjunction_query.c \

config.w32

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ if (PHP_COUCHBASE != "no") {
6868
src_couchbase_cluster_manager_sources =
6969
"user_settings.c ";
7070
src_couchbase_search_sources =
71+
"sort.c " +
72+
"sort_field.c " +
73+
"sort_geo.c " +
74+
"sort_id.c " +
75+
"sort_score.c " +
7176
"boolean_field_query.c " +
7277
"boolean_query.c " +
7378
"conjunction_query.c " +

couchbase.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,11 @@ PHP_MINIT_FUNCTION(couchbase)
312312
PHP_MINIT(TermSearchFacet)(INIT_FUNC_ARGS_PASSTHRU);
313313
PHP_MINIT(NumericRangeSearchFacet)(INIT_FUNC_ARGS_PASSTHRU);
314314
PHP_MINIT(DateRangeSearchFacet)(INIT_FUNC_ARGS_PASSTHRU);
315+
PHP_MINIT(SearchSort)(INIT_FUNC_ARGS_PASSTHRU);
316+
PHP_MINIT(SearchSortField)(INIT_FUNC_ARGS_PASSTHRU);
317+
PHP_MINIT(SearchSortGeoDistance)(INIT_FUNC_ARGS_PASSTHRU);
318+
PHP_MINIT(SearchSortId)(INIT_FUNC_ARGS_PASSTHRU);
319+
PHP_MINIT(SearchSortScore)(INIT_FUNC_ARGS_PASSTHRU);
315320

316321
PCBC_REGISTER_CONST(PERSISTTO_MASTER);
317322
PCBC_REGISTER_CONST(PERSISTTO_ONE);

couchbase.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ PHP_MINIT_FUNCTION(SearchFacet);
163163
PHP_MINIT_FUNCTION(TermSearchFacet);
164164
PHP_MINIT_FUNCTION(DateRangeSearchFacet);
165165
PHP_MINIT_FUNCTION(NumericRangeSearchFacet);
166+
PHP_MINIT_FUNCTION(SearchSort);
167+
PHP_MINIT_FUNCTION(SearchSortField);
168+
PHP_MINIT_FUNCTION(SearchSortGeoDistance);
169+
PHP_MINIT_FUNCTION(SearchSortId);
170+
PHP_MINIT_FUNCTION(SearchSortScore);
166171

167172
zval *bop_get_return_doc(zval *return_value, const char *key, int key_len, int is_mapped TSRMLS_DC);
168173

@@ -179,6 +184,7 @@ extern zend_class_entry *pcbc_mutation_state_ce;
179184
extern zend_class_entry *pcbc_exception_ce;
180185
extern zend_class_entry *pcbc_search_query_ce;
181186
extern zend_class_entry *pcbc_search_query_part_ce;
187+
extern zend_class_entry *pcbc_search_sort_ce;
182188
extern zend_class_entry *pcbc_search_facet_ce;
183189
extern zend_class_entry *pcbc_view_query_encodable_ce;
184190
extern zend_class_entry *pcbc_json_serializable_ce;
@@ -818,6 +824,11 @@ void pcbc_term_search_facet_init(zval *return_value, char *field, int field_len,
818824
void pcbc_date_range_search_facet_init(zval *return_value, char *field, int field_len, int limit TSRMLS_DC);
819825
void pcbc_numeric_range_search_facet_init(zval *return_value, char *field, int field_len, int limit TSRMLS_DC);
820826

827+
void pcbc_search_sort_id_init(zval *return_value TSRMLS_DC);
828+
void pcbc_search_sort_score_init(zval *return_value TSRMLS_DC);
829+
void pcbc_search_sort_field_init(zval *return_value, const char *field, int field_len TSRMLS_DC);
830+
void pcbc_search_sort_geo_distance_init(zval *return_value, const char *field, int field_len, double lon, double lat TSRMLS_DC);
831+
821832
#if PHP_VERSION_ID >= 70000
822833
void pcbc_lookup_in_builder_init(zval *return_value, zval *bucket, const char *id, int id_len, zval *args,
823834
int num_args TSRMLS_DC);

integration/SearchTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public function __construct() {
1616
if ($this->testDsn === FALSE) {
1717
$this->testDsn = 'couchbase://localhost/';
1818
}
19-
$this->authenticator = \Couchbase\ClassicAuthenticator();
20-
$this->authenticator->bucket('beer-sample', getenv('CB_USER_PASSWORD'));
19+
$this->authenticator = new \Couchbase\PasswordAuthenticator();
20+
$this->authenticator->username(getenv('CB_USER'))->password(getenv('CB_PASSWORD'));
2121
}
2222

2323
protected function setUp() {
@@ -59,7 +59,7 @@ function testSearchWithNoHits() {
5959

6060
function testSearchWithConsistency() {
6161
$cluster = new \Couchbase\Cluster($this->testDsn . '?fetch_mutation_tokens=true');
62-
$this->cluster->authenticate($this->authenticator);
62+
$cluster->authenticate($this->authenticator);
6363
$bucket = $cluster->openBucket('beer-sample');
6464

6565
$id = uniqid('testSearchWithConsistency');

package2.xml renamed to package.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@
126126
<file role="src" name="src/couchbase/search/date_range_query.c" />
127127
<file role="src" name="src/couchbase/search/disjunction_query.c" />
128128
<file role="src" name="src/couchbase/search/doc_id_query.c" />
129+
<file role="src" name="src/couchbase/search/facet.c" />
129130
<file role="src" name="src/couchbase/search/geo_bounding_box_query.c" />
130131
<file role="src" name="src/couchbase/search/geo_distance_query.c" />
131-
<file role="src" name="src/couchbase/search/facet.c" />
132132
<file role="src" name="src/couchbase/search/match_all_query.c" />
133133
<file role="src" name="src/couchbase/search/match_none_query.c" />
134134
<file role="src" name="src/couchbase/search/match_phrase_query.c" />
@@ -140,6 +140,11 @@
140140
<file role="src" name="src/couchbase/search/query_part.c" />
141141
<file role="src" name="src/couchbase/search/query_string_query.c" />
142142
<file role="src" name="src/couchbase/search/regexp_query.c" />
143+
<file role="src" name="src/couchbase/search/sort.c" />
144+
<file role="src" name="src/couchbase/search/sort_field.c" />
145+
<file role="src" name="src/couchbase/search/sort_geo.c" />
146+
<file role="src" name="src/couchbase/search/sort_id.c" />
147+
<file role="src" name="src/couchbase/search/sort_score.c" />
143148
<file role="src" name="src/couchbase/search/term_facet.c" />
144149
<file role="src" name="src/couchbase/search/term_query.c" />
145150
<file role="src" name="src/couchbase/search/term_range_query.c" />

0 commit comments

Comments
 (0)