Skip to content

Commit 9dda7c1

Browse files
[DOC-12432] Document Usage of {"ctl": { "validate" : true }, } for Search Queries (#302)
* [DOC-12432] Clarifying search_after and search_before behaviour, along with sorting and general pagination tips. Adding documentation on the new validate object - in search-request-params and as examples in the simple-search topics. Adding query results to the example in the REST API topic, and expanding on what those results mean. Fixing error in field path in the example full-request. * Add 7.6.5 version labels for validate property --------- Co-authored-by: Sarah Welton <[email protected]>
1 parent 37a4d93 commit 9dda7c1

File tree

4 files changed

+96
-32
lines changed

4 files changed

+96
-32
lines changed

modules/search/examples/run-search-full-request.jsonc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"bool": false
1010
},
1111
{
12-
"field": "ratings.Cleanliness",
12+
"field": "reviews.ratings.Cleanliness",
1313
"min": 1,
1414
"max": 3,
1515
"inclusive_min": true,
@@ -71,8 +71,9 @@
7171
// end::vectors[]
7272
"level": "at_plus",
7373
"results": "complete"
74-
}
74+
},
7575
// end::consistency[]
76+
"validate": true
7677
},
7778
// end::ctl[]
7879
"size": 10,
@@ -147,9 +148,9 @@
147148
"by": "field",
148149
"field": "field2",
149150
"desc": false,
150-
"mode": "max",
151+
"mode": "default",
151152
"missing": "last",
152-
"type": "number"
153+
"type": "string"
153154
},
154155
"-_score",
155156
"-_id"
@@ -158,8 +159,8 @@
158159
// end::sort[]
159160
"includeLocations": false,
160161
"score": "none",
161-
"search_after": ["field1Value", "5", "10.033205341869529", "1234"],
162-
"search_before": ["field1Value", "5", "10.033205341869529", "1234"],
162+
"search_after": ["field1String", "field2String", "10.033205341869529", "hotel_1234"],
163+
"search_before": ["field1String", "field2String", "10.033205341869529", "hotel_1234"],
163164
"limit": 10,
164165
"offset": 0,
165166
"collections": ["collection1", "collection2"]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"explain": true,
3+
"fields": [
4+
"content"
5+
],
6+
"highlight": {},
7+
"query": {
8+
"location": {
9+
"lon": -2.235143,
10+
"lat": 53.482358
11+
},
12+
"distance": "100mi",
13+
"field": "geo"
14+
},
15+
"ctl": {
16+
"validate": true
17+
},
18+
"size": 10,
19+
"from": 0
20+
}

modules/search/pages/search-request-params.adoc

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,13 @@ Set the total number of results to return for a single page of search results.
4848

4949
If you provide both the `size` and `limit` properties, the Search Service uses the `size` value.
5050

51-
If you do not provide a `from`, `size`, or other pagination settings in your query, the Search Service defaults to a `size` value of `10` and a `from` value of `0`.
52-
This means the Search Service does not offset results, and returns the first `10` matches to your query.
51+
The Search Service returns the `size` number of results:
52+
53+
* Starting at the offset in the `from` or `offset` property.
54+
* Starting from the key specified in the <<search_after,search_after property>>.
55+
* Starting backward from the key specified in the <<search_before,search_before property>>.
56+
57+
The `size` property is added by default to all Search requests, if not otherwise specified, with a value of `10`.
5358

5459
|from/offset |Integer |No a|
5560

@@ -59,8 +64,7 @@ For example, if you set a `size` value of `5` and a `from` value of `10`, the Se
5964

6065
If you provide both the `from` and `offset` properties, the Search Service uses the `from` value.
6166

62-
If you do not provide a `from`, `size`, or other pagination settings in your query, the Search Service defaults to a `size` value of `10` and a `from` value of `0`.
63-
This means the Search Service does not offset results, and returns the first `10` matches to your query.
67+
The `from` property is added by default to all Search requests, if not otherwise specified, with a value of `0`.
6468

6569
|highlight |Object |No a|
6670

@@ -92,7 +96,7 @@ To create an explanation for a search result's score in search results, set `exp
9296

9397
To turn off explanations for search result scoring, set `explain` to `false`.
9498

95-
|[[sort-array]]sort |Array |No a|
99+
|[[sort_arr]]sort |Array |No a|
96100

97101
Contains an array of strings or JSON objects to set how to sort search results.
98102
By default, the Search Service sorts results based on score values, from highest to lowest.
@@ -122,39 +126,45 @@ To turn off document relevancy scoring in search results, set `score` to `none`.
122126

123127
To turn on document relevancy scoring in search results, remove the `score` property.
124128

125-
|search_after |Array |No a|
129+
|[[search_after]]search_after |Array |No a|
126130

127-
NOTE: If you use `search_after` in a search request, you cannot use `search_before`. Both properties are included in the example code to show the correct syntax.
131+
NOTE: If you use `search_after` in a search request, you can't use `search_before`.
132+
Both properties are included in the example code to show the correct syntax.
128133

129134
Use `search_after` with `from/offset` and `sort` to control pagination in search results.
130135

131-
For example, if you had a set of 10 documents to sort based on `_id` values of 1-10, with `from` set to `2` and `search_after` set to `8`, documents 9-10 appear on the same page.
136+
Give a value for each string or JSON object in the <<sort_arr,sort array>> to the `search_after` array.
137+
You must provide the values in the same order that they appear in the <<sort_arr,sort array>>.
138+
Values in the `search_after` array must be strings.
139+
You cannot use `search_after` with numbers or other field data types - if your `sort` array includes fields with a `date` or `number` type, you cannot use `search_after`.
140+
Only result relevancy score values can be entered as strings in the array.
132141

133-
You must give a value for each string or JSON object in the `sort` array to the `search_after` array.
134-
The Search Service starts search result pagination after the document with those values.
142+
The Search Service starts search result pagination after the document with the values you provide in the array.
135143

136-
You must provide the values in the same order that they appear in the `sort` array.
137-
Your `sort` array must force a total order on your search results.
144+
For example, if you had a set of 10 documents to sort based on `_id` values of 1-10, with `from` set to `2` and `search_after` set to `8`, documents 9-10 appear on the same page.
138145

139-
Use `search_after` to make the memory requirements of deeper page searches more manageable, when compared to using only `from/offset`.
140-
`search_after` lets you start your search results from a specific result, rather than needing to process a number of search results to skip.
146+
To reduce the resource costs of deeper pagination on your Search queries, try to always include your document ID values as the final sort criteria in your <<sort_arr,sort array>>.
147+
Set the `search_after` property to include the values from the last result on your previous page of search results to effectively paginate.
141148

142-
|search_before |Array |No a|
149+
|[[search_before]]search_before |Array |No a|
143150

144-
NOTE: If you use `search_before` in a search request, you cannot use `search_after`. Both properties are included in the example code to show the correct syntax.
151+
NOTE: If you use `search_before` in a search request, you can't use `search_after`.
152+
Both properties are included in the example code to show the correct syntax.
145153

146154
Use `search_before` with `from/offset` and `sort` to control pagination in search results.
147155

148-
For example, if you had a set of 10 documents to sort based on `_id` values of 1-10, with `from` set to `2` and `search_before` set to `8`, documents 2-6 appear on the same page.
156+
Give a value for each string or JSON object in the `sort` array to the `search_before` array.
157+
You must provide the values in the same order that they appear in the `sort` array.
158+
Values in the `search_before` array must be strings.
159+
You cannot use `search_before` with numbers or other field data types - if your `sort` array includes fields with a `date` or `number` type, you cannot use `search_before`.
160+
Only result relevancy score values can be entered as strings in the array.
149161

150-
You must give a value for each string or JSON object in the `sort` array to the `search_before` array.
151-
The Search Service starts search result pagination before the document with those values.
162+
The Search Service starts search result pagination before the document with the values you provide in the array.
152163

153-
You must provide the values in the same order that they appear in the `sort` array.
154-
Your `sort` array must force a total order on your search results.
164+
For example, if you had a set of 10 documents to sort based on `_id` values of 1-10, with `from` set to `2` and `search_before` set to `8`, documents 2-6 appear on the same page.
155165

156-
Use `search_before` to make the memory requirements of deeper page searches more manageable, when compared to using only `from/offset`.
157-
`search_before` lets you start your search results back from a specific result, rather than needing to process a number of search results to skip.
166+
To reduce the resource costs of deeper pagination on your Search queries, try to always include your document ID values as the final sort criteria in your <<sort_arr,sort array>>.
167+
Set the `search_before` property to include the values from the last result on your previous page of search results to effectively paginate.
158168

159169
|[[collections]]collections |Array |No |Contains an array of strings that specify the collections where you want to run the query.
160170

@@ -1904,7 +1914,9 @@ The Search Service uses a consistency vector to synchronize the last document wr
19041914
include::example$run-search-full-request.jsonc[tag=ctl]
19051915
----
19061916

1907-
The `ctl` object contains the following properties:
1917+
In Couchbase Server 7.6.5 and later, you can also use the `ctl` object with the `validate` property to add an extra check to your queries and get help troubleshooting when a query does not return results.
1918+
1919+
The `ctl` object can contain the following properties:
19081920

19091921
[cols="1,1,1,4"]
19101922
|====
@@ -1923,6 +1935,18 @@ An object that contains a `vectors` object and the `level` and `results` propert
19231935

19241936
For more information, see <<consistency,>>.
19251937

1938+
|[[validate]]validate |Boolean |No a|
1939+
1940+
[.status]#Couchbase Server 7.6.5#
1941+
1942+
Add the `validate` property with a value of `true` to add extra validation checks to your Search query.
1943+
1944+
For example, the Search Service can tell you through the Web Console or the REST API that a field in your Search query is not in your Search index:
1945+
1946+
----
1947+
err: query_validate: field not indexed, field: ratings.Cleanliness, type: number
1948+
----
1949+
19261950
|====
19271951

19281952
[#consistency]
@@ -2181,7 +2205,8 @@ The following `sort` object orders search results by the values in `field1`, the
21812205
include::example$run-search-full-request.jsonc[tag=sort]
21822206
----
21832207

2184-
TIP: For the best results with sorting and page navigation in search results, always include your document ID values (`_id` or `-_id`) as the final sort criteria in your `sort` object.
2208+
This means that if 2 documents have the same value in `field1`, then the Search Service will sort them again based on their `field2` values.
2209+
If they have the same value in `field2`, then sorting will happen again based on each document's score, and then finally the documents' ID values.
21852210

21862211
The `sort` object can contain the following string values:
21872212

modules/search/pages/simple-search-ui.adoc

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ To run a simple search with the {page-ui-name}:
3131
. Press kbd:[Enter].
3232
. (Optional) To view a document and its source collection, click a document name in the search results list.
3333

34-
=== Example
34+
=== Example: Simple Text Search
3535

3636
For example, the following query searches for the strings `view`, `food`, and `beach`:
3737

@@ -45,6 +45,24 @@ It also returns all available fields in the index, and returns 10 results per pa
4545

4646
TIP: Use the xref:search-request-params.adoc#collections[`collections` parameter] in your request to specify an array of collections to search from the Search index.
4747

48+
=== Example: Validate a Search Query
49+
50+
[.status]#Couchbase Server 7.6.5#
51+
52+
For example, the following query searches a Search index, `landmark-content-index`, using a xref:search-request-params.adoc#geopoint-queries-distance[Distance/Radius-Based Geopoint Query] on the `geo` field.
53+
The query includes the xref:search-request-params.adoc#ctl[ctl object] with the `validate` property to validate the query:
54+
55+
[source,json]
56+
----
57+
include::example$run-search-validate-ui.jsonc[]
58+
----
59+
60+
Since the `landmark-content-index` does not include a mapping for the `geo` field and the `validate` property is included in the query, the Web Console returns the following error:
61+
62+
----
63+
query_validate: field not indexed, name: geo, type: geopoint
64+
----
65+
4866
== Next Steps
4967

5068
If you do not get the search results you were expecting, you can change the xref:search-request-params.adoc[JSON payload for your Search query].

0 commit comments

Comments
 (0)