Couchbase C++ SDK 1.0.1 (rev. 58d46d7)
Loading...
Searching...
No Matches
search_options.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 * Copyright 2020-Present Couchbase, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#pragma once
19
22#include <couchbase/error.hxx>
29
30#include <chrono>
31#include <functional>
32#include <memory>
33#include <optional>
34
35namespace couchbase
36{
43struct search_options : public common_options<search_options> {
50 struct built : public common_options<search_options>::built {
51 std::optional<std::string> client_context_id{};
52 bool include_locations{ false };
53 bool disable_scoring{ false };
54 std::optional<bool> explain{};
55 std::optional<std::uint32_t> limit{};
56 std::optional<std::uint32_t> skip{};
57 std::vector<std::string> collections{};
58 std::vector<std::string> fields{};
59 std::vector<std::string> highlight_fields{};
60 std::optional<couchbase::highlight_style> highlight_style{};
61 std::optional<search_scan_consistency> scan_consistency{};
62 std::vector<mutation_token> mutation_state{};
63 std::map<std::string, codec::binary, std::less<>> raw{};
64 std::map<std::string, std::shared_ptr<search_facet>, std::less<>> facets{};
65 std::vector<std::shared_ptr<search_sort>> sort{};
66 std::vector<std::string> sort_string{};
67 };
68
80 [[nodiscard]] auto build() const -> built
81 {
82 return {
84 client_context_id_,
85 include_locations_,
86 disable_scoring_,
87 explain_,
88 limit_,
89 skip_,
90 collections_,
91 fields_,
92 highlight_fields_,
93 highlight_style_,
94 scan_consistency_,
95 mutation_state_,
96 raw_,
97 facets_,
98 sort_,
99 sort_string_,
100 };
101 }
102
116 {
117 if (client_context_id.empty()) {
118 client_context_id_.reset();
119 } else {
120 client_context_id_ = std::move(client_context_id);
121 }
122 return self();
123 }
124
146 {
147 scan_consistency_ = scan_consistency;
148 mutation_state_.clear();
149 return self();
150 }
151
170 {
171 mutation_state_ = state.tokens();
172 scan_consistency_.reset();
173 return self();
174 }
175
186 template<typename Value>
187 auto raw(std::string name, const Value& value) -> search_options&
188 {
189 raw_[std::move(name)] = std::move(codec::tao_json_serializer::serialize(value));
190 return self();
191 }
192
202 auto skip(std::uint32_t skip) -> search_options&
203 {
204 skip_ = skip;
205 return self();
206 }
207
218 auto limit(std::uint32_t limit) -> search_options&
219 {
220 limit_ = limit;
221 return self();
222 }
223
235 {
236 explain_ = explain;
237 return self();
238 }
239
249 auto disable_scoring(bool disable) -> search_options&
250 {
251 disable_scoring_ = disable;
252 return self();
253 }
254
264 auto include_locations(bool include) -> search_options&
265 {
266 include_locations_ = include;
267 return self();
268 }
269
281 auto collections(std::vector<std::string> collections) -> search_options&
282 {
283 collections_ = std::move(collections);
284 return self();
285 }
286
301 auto fields(std::vector<std::string> fields) -> search_options&
302 {
303 fields_ = std::move(fields);
304 return self();
305 }
306
323 std::vector<std::string> fields = {}) -> search_options&
324 {
325 highlight_style_ = style;
326 highlight_fields_ = std::move(fields);
327 return self();
328 }
329
345 auto highlight(std::vector<std::string> fields) -> search_options&
346 {
347 highlight_style_ = highlight_style::html;
348 highlight_fields_ = std::move(fields);
349 return self();
350 }
351
372 auto sort(std::vector<std::string> sort_expressions) -> search_options&
373 {
374 sort_string_ = std::move(sort_expressions);
375 return self();
376 }
377
391 auto sort(std::vector<std::shared_ptr<search_sort>> sort_objects) -> search_options&
392 {
393 sort_ = std::move(sort_objects);
394 return self();
395 }
396
410 auto facets(std::map<std::string, std::shared_ptr<search_facet>> facets) -> search_options&
411 {
412 facets_.clear();
413 for (auto& [name, facet] : facets) {
414 facets_[name] = std::move(facet);
415 }
416 return self();
417 }
418
432 template<typename... Facets>
433 auto facets(const Facets&... facets) -> search_options&
434 {
435 facets_.clear();
436 encode_facet(facets...);
437 return self();
438 }
439
457 auto facet(std::string name, std::shared_ptr<search_facet> facet) -> search_options&
458 {
459 facets_[std::move(name)] = std::move(facet);
460 return self();
461 }
462
481 template<typename Facet>
482 auto facet(std::string name, Facet facet) -> search_options&
483 {
484 facets_[std::move(name)] = std::make_shared<Facet>(std::move(facet));
485 return self();
486 }
487
488private:
489 template<typename Name, typename Facet, typename... Rest>
490 void encode_facet(const std::pair<Name, Facet>& facet, Rest... args)
491 {
492 facets_[std::move(facet.first)] = std::make_shared<Facet>(std::move(facet.second));
493 if constexpr (sizeof...(args) > 0) {
494 encode_facet(args...);
495 }
496 }
497
498 std::optional<std::string> client_context_id_{};
499 bool include_locations_{ false };
500 bool disable_scoring_{ false };
501 std::optional<bool> explain_{};
502 std::optional<std::uint32_t> limit_{};
503 std::optional<std::uint32_t> skip_{};
504 std::vector<std::string> collections_{};
505 std::vector<std::string> fields_{};
506 std::vector<std::string> highlight_fields_{};
507 std::optional<highlight_style> highlight_style_{};
508 std::optional<search_scan_consistency> scan_consistency_{};
509 std::vector<mutation_token> mutation_state_{};
510 std::map<std::string, codec::binary, std::less<>> raw_{};
511 std::map<std::string, std::shared_ptr<search_facet>, std::less<>> facets_{};
512 std::vector<std::shared_ptr<search_sort>> sort_{};
513 std::vector<std::string> sort_string_{};
514};
515
522using search_handler = std::function<void(error, search_result)>;
523} // namespace couchbase
static auto serialize(Document document) -> binary
Definition tao_json_serializer.hxx:47
Common options that used by most operations.
Definition common_options.hxx:37
auto self() -> search_options &
Allows to return the right options builder instance for child implementations.
Definition common_options.hxx:102
auto build_common_options() const -> built
Definition common_options.hxx:89
Definition error.hxx:30
Aggregation of one or more mutation_tokens for specifying consistency requirements of N1QL or FTS que...
Definition mutation_state.hxx:35
Represents result of cluster::search() and scope::search() calls.
Definition search_result.hxx:45
Represents a single item from the result of collection::scan()
Definition allow_querying_search_index_options.hxx:28
std::function< void(error, search_result)> search_handler
The signature for the handler of the cluster::search() and scope::search() operations.
Definition search_options.hxx:522
highlight_style
Enumeration of the highlighting styles recognized by the FTS engine.
Definition highlight_style.hxx:28
@ html
Use HTML tags <mark> and </mark> to indicate matches in the fields.
search_scan_consistency
Definition search_scan_consistency.hxx:22
Immutable value object representing consistent options.
Definition search_options.hxx:50
std::vector< std::string > highlight_fields
Definition search_options.hxx:59
std::optional< bool > explain
Definition search_options.hxx:54
std::vector< std::string > collections
Definition search_options.hxx:57
std::map< std::string, codec::binary, std::less<> > raw
Definition search_options.hxx:63
std::optional< std::string > client_context_id
Definition search_options.hxx:51
std::map< std::string, std::shared_ptr< search_facet >, std::less<> > facets
Definition search_options.hxx:64
bool include_locations
Definition search_options.hxx:52
std::optional< search_scan_consistency > scan_consistency
Definition search_options.hxx:61
std::vector< std::shared_ptr< search_sort > > sort
Definition search_options.hxx:65
std::vector< std::string > fields
Definition search_options.hxx:58
std::optional< std::uint32_t > skip
Definition search_options.hxx:56
std::optional< std::uint32_t > limit
Definition search_options.hxx:55
bool disable_scoring
Definition search_options.hxx:53
std::vector< std::string > sort_string
Definition search_options.hxx:66
Options for cluster::search() and scope::search().
Definition search_options.hxx:43
auto skip(std::uint32_t skip) -> search_options &
Set the number of rows to skip (eg.
Definition search_options.hxx:202
auto highlight(highlight_style style=highlight_style::html, std::vector< std::string > fields={}) -> search_options &
Configures the highlighting of matches in the response.
Definition search_options.hxx:322
auto fields(std::vector< std::string > fields) -> search_options &
Configures the list of fields for which the whole value should be included in the response.
Definition search_options.hxx:301
auto explain(bool explain) -> search_options &
Activates or deactivates the explanation of each result hit in the response, according to the paramet...
Definition search_options.hxx:234
auto facet(std::string name, Facet facet) -> search_options &
Adds one search_facet to the query.
Definition search_options.hxx:482
auto facet(std::string name, std::shared_ptr< search_facet > facet) -> search_options &
Adds one search_facet to the query.
Definition search_options.hxx:457
auto sort(std::vector< std::shared_ptr< search_sort > > sort_objects) -> search_options &
Configures the list of search_sort instances which are used for sorting purposes.
Definition search_options.hxx:391
auto highlight(std::vector< std::string > fields) -> search_options &
Configures the highlighting of matches in the response for all fields, using the server's default hig...
Definition search_options.hxx:345
auto consistent_with(const mutation_state &state) -> search_options &
Sets the mutation_tokens this query should be consistent with.
Definition search_options.hxx:169
auto scan_consistency(search_scan_consistency scan_consistency) -> search_options &
Customizes the consistency guarantees for this query.
Definition search_options.hxx:145
auto sort(std::vector< std::string > sort_expressions) -> search_options &
Configures the list of fields (including special fields) which are used for sorting purposes.
Definition search_options.hxx:372
auto build() const -> built
Validates options and returns them as an immutable value.
Definition search_options.hxx:80
auto facets(std::map< std::string, std::shared_ptr< search_facet > > facets) -> search_options &
Sets list of search_facet to the query.
Definition search_options.hxx:410
auto disable_scoring(bool disable) -> search_options &
If set to true, thee server will not perform any scoring on the hits.
Definition search_options.hxx:249
auto include_locations(bool include) -> search_options &
If set to true, will include the search_row::locations().
Definition search_options.hxx:264
auto limit(std::uint32_t limit) -> search_options &
Add a limit to the query on the number of rows it can return.
Definition search_options.hxx:218
auto facets(const Facets &... facets) -> search_options &
Sets list of search_facet to the query.
Definition search_options.hxx:433
auto client_context_id(std::string client_context_id) -> search_options &
Supports providing a custom client context ID for this query.
Definition search_options.hxx:115
auto collections(std::vector< std::string > collections) -> search_options &
Allows to limit the search query to a specific list of collection names.
Definition search_options.hxx:281
auto raw(std::string name, const Value &value) -> search_options &
Definition search_options.hxx:187