Couchbase C++ SDK 1.5.0 (rev. 380784c)
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>
29
30#include <chrono>
31#include <functional>
32#include <memory>
33#include <optional>
34#include <stdexcept>
35#include <type_traits>
36
37namespace couchbase
38{
39namespace codec
40{
42}
43
50struct search_options : public common_options<search_options> {
57 struct built : public common_options<search_options>::built {
58 std::optional<std::string> client_context_id{};
59 bool include_locations{ false };
60 bool disable_scoring{ false };
61 std::optional<bool> explain{};
62 std::optional<std::uint32_t> limit{};
63 std::optional<std::uint32_t> skip{};
64 std::vector<std::string> collections{};
65 std::vector<std::string> fields{};
66 std::vector<std::string> highlight_fields{};
67 std::optional<couchbase::highlight_style> highlight_style{};
68 std::optional<search_scan_consistency> scan_consistency{};
69 std::vector<mutation_token> mutation_state{};
70 std::map<std::string, codec::binary, std::less<>> raw{};
71 std::map<std::string, std::shared_ptr<search_facet>, std::less<>> facets{};
72 std::vector<std::shared_ptr<search_sort>> sort{};
73 std::vector<std::string> sort_string{};
74 std::optional<search_scoring::built> scoring{};
75 };
76
88 [[nodiscard]] auto build() const -> built
89 {
90 return {
92 client_context_id_,
93 include_locations_,
94 disable_scoring_,
95 explain_,
96 limit_,
97 skip_,
98 collections_,
99 fields_,
100 highlight_fields_,
101 highlight_style_,
102 scan_consistency_,
103 mutation_state_,
104 raw_,
105 facets_,
106 sort_,
107 sort_string_,
108 scoring_,
109 };
110 }
111
125 {
126 if (client_context_id.empty()) {
127 client_context_id_.reset();
128 } else {
129 client_context_id_ = std::move(client_context_id);
130 }
131 return self();
132 }
133
155 {
156 scan_consistency_ = scan_consistency;
157 mutation_state_.clear();
158 return self();
159 }
160
180 {
181 mutation_state_ = state.tokens();
182 scan_consistency_.reset();
183 return self();
184 }
185
196 template<typename Value,
197 typename Serializer = codec::tao_json_serializer,
198 std::enable_if_t<codec::is_serializer_v<Serializer>, bool> = true>
199 auto raw(std::string name, const Value& value) -> search_options&
200 {
201 raw_[std::move(name)] = std::move(Serializer::template serialize<Value>(value));
202 return self();
203 }
204
214 auto skip(std::uint32_t skip) -> search_options&
215 {
216 skip_ = skip;
217 return self();
218 }
219
230 auto limit(std::uint32_t limit) -> search_options&
231 {
232 limit_ = limit;
233 return self();
234 }
235
247 {
248 explain_ = explain;
249 return self();
250 }
251
267 [[deprecated("Use search_options::scoring(search_scoring_none{}) instead.")]]
268 auto disable_scoring(bool disable) -> search_options&
269 {
270 if (disable && scoring_.has_value()) {
271 throw std::invalid_argument("disable_scoring() cannot be used together with scoring()");
272 }
273 disable_scoring_ = disable;
274 return self();
275 }
276
299 template<typename Scoring,
300 std::enable_if_t<std::is_base_of_v<search_scoring, Scoring>, bool> = true>
301 auto scoring(const Scoring& scoring) -> search_options&
302 {
303 if (disable_scoring_) {
304 throw std::invalid_argument("scoring() cannot be used together with disable_scoring()");
305 }
306 scoring_ = scoring.build();
307 return self();
308 }
309
319 auto include_locations(bool include) -> search_options&
320 {
321 include_locations_ = include;
322 return self();
323 }
324
336 auto collections(std::vector<std::string> collections) -> search_options&
337 {
338 collections_ = std::move(collections);
339 return self();
340 }
341
356 auto fields(std::vector<std::string> fields) -> search_options&
357 {
358 fields_ = std::move(fields);
359 return self();
360 }
361
378 std::vector<std::string> fields = {}) -> search_options&
379 {
380 highlight_style_ = style;
381 highlight_fields_ = std::move(fields);
382 return self();
383 }
384
400 auto highlight(std::vector<std::string> fields) -> search_options&
401 {
402 highlight_style_ = highlight_style::html;
403 highlight_fields_ = std::move(fields);
404 return self();
405 }
406
427 auto sort(std::vector<std::string> sort_expressions) -> search_options&
428 {
429 sort_string_ = std::move(sort_expressions);
430 return self();
431 }
432
446 auto sort(std::vector<std::shared_ptr<search_sort>> sort_objects) -> search_options&
447 {
448 sort_ = std::move(sort_objects);
449 return self();
450 }
451
465 auto facets(std::map<std::string, std::shared_ptr<search_facet>> facets) -> search_options&
466 {
467 facets_.clear();
468 for (auto& [name, facet] : facets) {
469 facets_[name] = std::move(facet);
470 }
471 return self();
472 }
473
487 template<typename... Facets>
488 auto facets(const Facets&... facets) -> search_options&
489 {
490 facets_.clear();
491 encode_facet(facets...);
492 return self();
493 }
494
512 auto facet(std::string name, std::shared_ptr<search_facet> facet) -> search_options&
513 {
514 facets_[std::move(name)] = std::move(facet);
515 return self();
516 }
517
536 template<typename Facet>
537 auto facet(std::string name, Facet facet) -> search_options&
538 {
539 facets_[std::move(name)] = std::make_shared<Facet>(std::move(facet));
540 return self();
541 }
542
543private:
544 template<typename Name, typename Facet, typename... Rest>
545 void encode_facet(const std::pair<Name, Facet>& facet, Rest... args)
546 {
547 facets_[std::move(facet.first)] = std::make_shared<Facet>(std::move(facet.second));
548 if constexpr (sizeof...(args) > 0) {
549 encode_facet(args...);
550 }
551 }
552
553 std::optional<std::string> client_context_id_{};
554 bool include_locations_{ false };
555 bool disable_scoring_{ false };
556 std::optional<bool> explain_{};
557 std::optional<std::uint32_t> limit_{};
558 std::optional<std::uint32_t> skip_{};
559 std::vector<std::string> collections_{};
560 std::vector<std::string> fields_{};
561 std::vector<std::string> highlight_fields_{};
562 std::optional<highlight_style> highlight_style_{};
563 std::optional<search_scan_consistency> scan_consistency_{};
564 std::vector<mutation_token> mutation_state_{};
565 std::map<std::string, codec::binary, std::less<>> raw_{};
566 std::map<std::string, std::shared_ptr<search_facet>, std::less<>> facets_{};
567 std::vector<std::shared_ptr<search_sort>> sort_{};
568 std::vector<std::string> sort_string_{};
569 std::optional<search_scoring::built> scoring_{};
570};
571
578using search_handler = std::function<void(error, search_result)>;
579} // 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:578
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:57
std::vector< mutation_token > mutation_state
Definition search_options.hxx:69
std::vector< std::string > highlight_fields
Definition search_options.hxx:66
std::optional< bool > explain
Definition search_options.hxx:61
std::vector< std::string > collections
Definition search_options.hxx:64
std::optional< search_scoring::built > scoring
Definition search_options.hxx:74
std::map< std::string, codec::binary, std::less<> > raw
Definition search_options.hxx:70
std::optional< std::string > client_context_id
Definition search_options.hxx:58
std::optional< couchbase::highlight_style > highlight_style
Definition search_options.hxx:67
std::map< std::string, std::shared_ptr< search_facet >, std::less<> > facets
Definition search_options.hxx:71
bool include_locations
Definition search_options.hxx:59
std::optional< search_scan_consistency > scan_consistency
Definition search_options.hxx:68
std::vector< std::shared_ptr< search_sort > > sort
Definition search_options.hxx:72
std::vector< std::string > fields
Definition search_options.hxx:65
std::optional< std::uint32_t > skip
Definition search_options.hxx:63
std::optional< std::uint32_t > limit
Definition search_options.hxx:62
bool disable_scoring
Definition search_options.hxx:60
std::vector< std::string > sort_string
Definition search_options.hxx:73
Options for search() and search().
Definition search_options.hxx:50
auto skip(std::uint32_t skip) -> search_options &
Set the number of rows to skip (eg.
Definition search_options.hxx:214
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:377
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:356
auto scoring(const Scoring &scoring) -> search_options &
Selects how the server scores the hits, and how it merges the FTS and vector result sets of a hybrid ...
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:246
auto facet(std::string name, Facet facet) -> search_options &
Adds one search_facet to the query.
Definition search_options.hxx:537
auto facet(std::string name, std::shared_ptr< search_facet > facet) -> search_options &
Adds one search_facet to the query.
Definition search_options.hxx:512
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:446
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:400
auto consistent_with(const mutation_state &state) -> search_options &
Sets the mutation_tokens this query should be consistent with.
Definition search_options.hxx:179
auto scan_consistency(search_scan_consistency scan_consistency) -> search_options &
Customizes the consistency guarantees for this query.
Definition search_options.hxx:154
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:427
auto build() const -> built
Validates options and returns them as an immutable value.
Definition search_options.hxx:88
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:465
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:268
auto include_locations(bool include) -> search_options &
If set to true, will include the locations().
Definition search_options.hxx:319
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:230
auto facets(const Facets &... facets) -> search_options &
Sets list of search_facet to the query.
Definition search_options.hxx:488
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:124
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:336
auto raw(std::string name, const Value &value) -> search_options &
Definition search_options.hxx:199