Couchbase C++ SDK 1.4.0 (rev. 59aa900)
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
21#include <couchbase/error.hxx>
28
29#include <chrono>
30#include <functional>
31#include <memory>
32#include <optional>
33
34namespace couchbase
35{
36namespace codec
37{
39}
40
47struct search_options : public common_options<search_options> {
54 struct built : public common_options<search_options>::built {
55 std::optional<std::string> client_context_id{};
56 bool include_locations{ false };
57 bool disable_scoring{ false };
58 std::optional<bool> explain{};
59 std::optional<std::uint32_t> limit{};
60 std::optional<std::uint32_t> skip{};
61 std::vector<std::string> collections{};
62 std::vector<std::string> fields{};
63 std::vector<std::string> highlight_fields{};
64 std::optional<couchbase::highlight_style> highlight_style{};
65 std::optional<search_scan_consistency> scan_consistency{};
66 std::vector<mutation_token> mutation_state{};
67 std::map<std::string, codec::binary, std::less<>> raw{};
68 std::map<std::string, std::shared_ptr<search_facet>, std::less<>> facets{};
69 std::vector<std::shared_ptr<search_sort>> sort{};
70 std::vector<std::string> sort_string{};
71 };
72
84 [[nodiscard]] auto build() const -> built
85 {
86 return {
88 client_context_id_,
89 include_locations_,
90 disable_scoring_,
91 explain_,
92 limit_,
93 skip_,
94 collections_,
95 fields_,
96 highlight_fields_,
97 highlight_style_,
98 scan_consistency_,
99 mutation_state_,
100 raw_,
101 facets_,
102 sort_,
103 sort_string_,
104 };
105 }
106
120 {
121 if (client_context_id.empty()) {
122 client_context_id_.reset();
123 } else {
124 client_context_id_ = std::move(client_context_id);
125 }
126 return self();
127 }
128
150 {
151 scan_consistency_ = scan_consistency;
152 mutation_state_.clear();
153 return self();
154 }
155
175 {
176 mutation_state_ = state.tokens();
177 scan_consistency_.reset();
178 return self();
179 }
180
191 template<typename Value,
192 typename Serializer = codec::tao_json_serializer,
193 std::enable_if_t<codec::is_serializer_v<Serializer>, bool> = true>
194 auto raw(std::string name, const Value& value) -> search_options&
195 {
196 raw_[std::move(name)] = std::move(Serializer::template serialize<Value>(value));
197 return self();
198 }
199
209 auto skip(std::uint32_t skip) -> search_options&
210 {
211 skip_ = skip;
212 return self();
213 }
214
225 auto limit(std::uint32_t limit) -> search_options&
226 {
227 limit_ = limit;
228 return self();
229 }
230
242 {
243 explain_ = explain;
244 return self();
245 }
246
256 auto disable_scoring(bool disable) -> search_options&
257 {
258 disable_scoring_ = disable;
259 return self();
260 }
261
271 auto include_locations(bool include) -> search_options&
272 {
273 include_locations_ = include;
274 return self();
275 }
276
288 auto collections(std::vector<std::string> collections) -> search_options&
289 {
290 collections_ = std::move(collections);
291 return self();
292 }
293
308 auto fields(std::vector<std::string> fields) -> search_options&
309 {
310 fields_ = std::move(fields);
311 return self();
312 }
313
330 std::vector<std::string> fields = {}) -> search_options&
331 {
332 highlight_style_ = style;
333 highlight_fields_ = std::move(fields);
334 return self();
335 }
336
352 auto highlight(std::vector<std::string> fields) -> search_options&
353 {
354 highlight_style_ = highlight_style::html;
355 highlight_fields_ = std::move(fields);
356 return self();
357 }
358
379 auto sort(std::vector<std::string> sort_expressions) -> search_options&
380 {
381 sort_string_ = std::move(sort_expressions);
382 return self();
383 }
384
398 auto sort(std::vector<std::shared_ptr<search_sort>> sort_objects) -> search_options&
399 {
400 sort_ = std::move(sort_objects);
401 return self();
402 }
403
417 auto facets(std::map<std::string, std::shared_ptr<search_facet>> facets) -> search_options&
418 {
419 facets_.clear();
420 for (auto& [name, facet] : facets) {
421 facets_[name] = std::move(facet);
422 }
423 return self();
424 }
425
439 template<typename... Facets>
440 auto facets(const Facets&... facets) -> search_options&
441 {
442 facets_.clear();
443 encode_facet(facets...);
444 return self();
445 }
446
464 auto facet(std::string name, std::shared_ptr<search_facet> facet) -> search_options&
465 {
466 facets_[std::move(name)] = std::move(facet);
467 return self();
468 }
469
488 template<typename Facet>
489 auto facet(std::string name, Facet facet) -> search_options&
490 {
491 facets_[std::move(name)] = std::make_shared<Facet>(std::move(facet));
492 return self();
493 }
494
495private:
496 template<typename Name, typename Facet, typename... Rest>
497 void encode_facet(const std::pair<Name, Facet>& facet, Rest... args)
498 {
499 facets_[std::move(facet.first)] = std::make_shared<Facet>(std::move(facet.second));
500 if constexpr (sizeof...(args) > 0) {
501 encode_facet(args...);
502 }
503 }
504
505 std::optional<std::string> client_context_id_{};
506 bool include_locations_{ false };
507 bool disable_scoring_{ false };
508 std::optional<bool> explain_{};
509 std::optional<std::uint32_t> limit_{};
510 std::optional<std::uint32_t> skip_{};
511 std::vector<std::string> collections_{};
512 std::vector<std::string> fields_{};
513 std::vector<std::string> highlight_fields_{};
514 std::optional<highlight_style> highlight_style_{};
515 std::optional<search_scan_consistency> scan_consistency_{};
516 std::vector<mutation_token> mutation_state_{};
517 std::map<std::string, codec::binary, std::less<>> raw_{};
518 std::map<std::string, std::shared_ptr<search_facet>, std::less<>> facets_{};
519 std::vector<std::shared_ptr<search_sort>> sort_{};
520 std::vector<std::string> sort_string_{};
521};
522
529using search_handler = std::function<void(error, search_result)>;
530} // namespace couchbase
Definition tao_json_serializer.hxx:42
Common options that used by most operations.
Definition common_options.hxx:38
auto self() -> search_options &
Definition common_options.hxx:116
auto build_common_options() const -> built
Definition common_options.hxx:103
Definition error.hxx:31
Aggregation of one or more mutation_tokens for specifying consistency requirements of N1QL or FTS que...
Definition mutation_state.hxx:35
Represents result of search() and search() calls.
Definition search_result.hxx:45
Definition analytics_options.hxx:36
Represents a single item from the result of scan().
Definition allow_querying_search_index_options.hxx:28
std::function< void(error, search_result)> search_handler
The signature for the handler of the search() and search() operations.
Definition search_options.hxx:529
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.
Definition highlight_style.hxx:35
search_scan_consistency
Definition search_scan_consistency.hxx:22
Immutable value object representing consistent options.
Definition search_options.hxx:54
std::vector< mutation_token > mutation_state
Definition search_options.hxx:66
std::vector< std::string > highlight_fields
Definition search_options.hxx:63
std::optional< bool > explain
Definition search_options.hxx:58
std::vector< std::string > collections
Definition search_options.hxx:61
std::map< std::string, codec::binary, std::less<> > raw
Definition search_options.hxx:67
std::optional< std::string > client_context_id
Definition search_options.hxx:55
std::optional< couchbase::highlight_style > highlight_style
Definition search_options.hxx:64
std::map< std::string, std::shared_ptr< search_facet >, std::less<> > facets
Definition search_options.hxx:68
bool include_locations
Definition search_options.hxx:56
std::optional< search_scan_consistency > scan_consistency
Definition search_options.hxx:65
std::vector< std::shared_ptr< search_sort > > sort
Definition search_options.hxx:69
std::vector< std::string > fields
Definition search_options.hxx:62
std::optional< std::uint32_t > skip
Definition search_options.hxx:60
std::optional< std::uint32_t > limit
Definition search_options.hxx:59
bool disable_scoring
Definition search_options.hxx:57
std::vector< std::string > sort_string
Definition search_options.hxx:70
Options for search() and search().
Definition search_options.hxx:47
auto skip(std::uint32_t skip) -> search_options &
Set the number of rows to skip (eg.
Definition search_options.hxx:209
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:329
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:308
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:241
auto facet(std::string name, Facet facet) -> search_options &
Adds one search_facet to the query.
Definition search_options.hxx:489
auto facet(std::string name, std::shared_ptr< search_facet > facet) -> search_options &
Adds one search_facet to the query.
Definition search_options.hxx:464
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:398
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:352
auto consistent_with(const mutation_state &state) -> search_options &
Sets the mutation_tokens this query should be consistent with.
Definition search_options.hxx:174
auto scan_consistency(search_scan_consistency scan_consistency) -> search_options &
Customizes the consistency guarantees for this query.
Definition search_options.hxx:149
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:379
auto build() const -> built
Validates options and returns them as an immutable value.
Definition search_options.hxx:84
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:417
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:256
auto include_locations(bool include) -> search_options &
If set to true, will include the locations().
Definition search_options.hxx:271
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:225
auto facets(const Facets &... facets) -> search_options &
Sets list of search_facet to the query.
Definition search_options.hxx:440
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:119
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:288
auto raw(std::string name, const Value &value) -> search_options &
Definition search_options.hxx:194